init home-assistant
This commit is contained in:
1
home-assistant/tests/components/dsmr_reader/__init__.py
Normal file
1
home-assistant/tests/components/dsmr_reader/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the dsmr_reader component."""
|
||||
@@ -0,0 +1,25 @@
|
||||
# serializer version: 1
|
||||
# name: test_get_config_entry_diagnostics
|
||||
dict({
|
||||
'entities': dict({
|
||||
}),
|
||||
'entry': dict({
|
||||
'data': dict({
|
||||
}),
|
||||
'disabled_by': None,
|
||||
'discovery_keys': dict({
|
||||
}),
|
||||
'domain': 'dsmr_reader',
|
||||
'entry_id': 'TEST_ENTRY_ID',
|
||||
'minor_version': 1,
|
||||
'options': dict({
|
||||
}),
|
||||
'pref_disable_new_entities': False,
|
||||
'pref_disable_polling': False,
|
||||
'source': 'user',
|
||||
'title': 'dsmr_reader',
|
||||
'unique_id': 'UNIQUE_TEST_ID',
|
||||
'version': 1,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Tests for the config flow."""
|
||||
|
||||
from homeassistant.components.dsmr_reader.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
|
||||
async def test_user_step(hass: HomeAssistant) -> None:
|
||||
"""Test the user step call."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "confirm"
|
||||
assert result["errors"] is None
|
||||
|
||||
config_result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
|
||||
assert config_result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert config_result["title"] == "DSMR Reader"
|
||||
|
||||
duplicate_result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert duplicate_result["type"] is FlowResultType.ABORT
|
||||
assert duplicate_result["reason"] == "single_instance_allowed"
|
||||
109
home-assistant/tests/components/dsmr_reader/test_definitions.py
Normal file
109
home-assistant/tests/components/dsmr_reader/test_definitions.py
Normal file
@@ -0,0 +1,109 @@
|
||||
"""Test the DSMR Reader definitions."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.dsmr_reader.const import DOMAIN
|
||||
from homeassistant.components.dsmr_reader.definitions import (
|
||||
DSMRReaderSensorEntityDescription,
|
||||
dsmr_transform,
|
||||
tariff_transform,
|
||||
)
|
||||
from homeassistant.components.dsmr_reader.sensor import DSMRSensor
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_mqtt_message
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("input", "expected"),
|
||||
[
|
||||
("20", 2.0),
|
||||
("version 5", "version 5"),
|
||||
],
|
||||
)
|
||||
async def test_dsmr_transform(input, expected) -> None:
|
||||
"""Test the dsmr_transform function."""
|
||||
assert dsmr_transform(input) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("input", "expected"),
|
||||
[
|
||||
("1", "low"),
|
||||
("0", "high"),
|
||||
],
|
||||
)
|
||||
async def test_tariff_transform(input, expected) -> None:
|
||||
"""Test the tariff_transform function."""
|
||||
assert tariff_transform(input) == expected
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mqtt_mock")
|
||||
async def test_entity_tariff(hass: HomeAssistant) -> None:
|
||||
"""Test the state attribute of DSMRReaderSensorEntityDescription when a tariff transform is needed."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=DOMAIN,
|
||||
options={},
|
||||
entry_id="TEST_ENTRY_ID",
|
||||
unique_id="UNIQUE_TEST_ID",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test if the payload is empty
|
||||
async_fire_mqtt_message(hass, "dsmr/meter-stats/electricity_tariff", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
electricity_tariff = "sensor.dsmr_meter_stats_electricity_tariff"
|
||||
assert hass.states.get(electricity_tariff).state == STATE_UNKNOWN
|
||||
|
||||
# Test high tariff
|
||||
async_fire_mqtt_message(hass, "dsmr/meter-stats/electricity_tariff", "0")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(electricity_tariff).state == "high"
|
||||
|
||||
# Test low tariff
|
||||
async_fire_mqtt_message(hass, "dsmr/meter-stats/electricity_tariff", "1")
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(electricity_tariff).state == "low"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mqtt_mock")
|
||||
async def test_entity_dsmr_transform(hass: HomeAssistant) -> None:
|
||||
"""Test the state attribute of DSMRReaderSensorEntityDescription when a dsmr transform is needed."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=DOMAIN,
|
||||
options={},
|
||||
entry_id="TEST_ENTRY_ID",
|
||||
unique_id="UNIQUE_TEST_ID",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Create the entity, since it's not by default
|
||||
description = DSMRReaderSensorEntityDescription(
|
||||
key="dsmr/meter-stats/dsmr_version",
|
||||
name="version_test",
|
||||
state=dsmr_transform,
|
||||
)
|
||||
sensor = DSMRSensor(description, config_entry)
|
||||
sensor.hass = hass
|
||||
await sensor.async_added_to_hass()
|
||||
|
||||
# Test dsmr version, if it's a digit
|
||||
async_fire_mqtt_message(hass, "dsmr/meter-stats/dsmr_version", "42")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
dsmr_version = "sensor.dsmr_meter_stats_dsmr_version"
|
||||
assert hass.states.get(dsmr_version).state == "4.2"
|
||||
|
||||
# Test dsmr version, if it's not a digit
|
||||
async_fire_mqtt_message(hass, "dsmr/meter-stats/dsmr_version", "version 5")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(dsmr_version).state == "version 5"
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Test the DSMR Reader component diagnostics."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
from syrupy.filters import props
|
||||
|
||||
from homeassistant.components.dsmr_reader.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
hass_client: ClientSessionGenerator,
|
||||
) -> None:
|
||||
"""Test if get_config_entry_diagnostics returns the correct data."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=DOMAIN,
|
||||
options={},
|
||||
entry_id="TEST_ENTRY_ID",
|
||||
unique_id="UNIQUE_TEST_ID",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.dsmr_reader.async_setup_entry", return_value=True
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
diagnostics = await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, config_entry
|
||||
)
|
||||
assert diagnostics == snapshot(exclude=props("created_at", "modified_at"))
|
||||
66
home-assistant/tests/components/dsmr_reader/test_sensor.py
Normal file
66
home-assistant/tests/components/dsmr_reader/test_sensor.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""Tests for DSMR Reader sensor."""
|
||||
|
||||
from homeassistant.components.dsmr_reader.const import DOMAIN
|
||||
from homeassistant.components.dsmr_reader.definitions import (
|
||||
DSMRReaderSensorEntityDescription,
|
||||
)
|
||||
from homeassistant.components.dsmr_reader.sensor import DSMRSensor
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_mqtt_message
|
||||
from tests.typing import MqttMockHAClient
|
||||
|
||||
|
||||
async def test_dsmr_sensor_mqtt(
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock: MqttMockHAClient,
|
||||
) -> None:
|
||||
"""Test the DSMRSensor class, via an emluated MQTT message."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=DOMAIN,
|
||||
options={},
|
||||
entry_id="TEST_ENTRY_ID",
|
||||
unique_id="UNIQUE_TEST_ID",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
electricity_delivered_1 = "sensor.dsmr_reading_electricity_delivered_1"
|
||||
assert hass.states.get(electricity_delivered_1).state == STATE_UNKNOWN
|
||||
|
||||
electricity_delivered_2 = "sensor.dsmr_reading_electricity_delivered_2"
|
||||
assert hass.states.get(electricity_delivered_2).state == STATE_UNKNOWN
|
||||
|
||||
# Test if the payload is empty
|
||||
async_fire_mqtt_message(hass, "dsmr/reading/electricity_delivered_1", "")
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "dsmr/reading/electricity_delivered_2", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(electricity_delivered_1).state == STATE_UNKNOWN
|
||||
assert hass.states.get(electricity_delivered_2).state == STATE_UNKNOWN
|
||||
|
||||
# Test if the payload is not empty
|
||||
async_fire_mqtt_message(hass, "dsmr/reading/electricity_delivered_1", "1050.39")
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "dsmr/reading/electricity_delivered_2", "2001.12")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(electricity_delivered_1).state == "1050.39"
|
||||
assert hass.states.get(electricity_delivered_2).state == "2001.12"
|
||||
|
||||
# Create a test entity to ensure the entity_description.state is not None
|
||||
description = DSMRReaderSensorEntityDescription(
|
||||
key="DSMR_TEST_KEY",
|
||||
name="DSMR_TEST_NAME",
|
||||
state=lambda x: x,
|
||||
)
|
||||
sensor = DSMRSensor(description, config_entry)
|
||||
sensor.hass = hass
|
||||
await sensor.async_added_to_hass()
|
||||
async_fire_mqtt_message(hass, "DSMR_TEST_KEY", "192.8")
|
||||
await hass.async_block_till_done()
|
||||
assert sensor.native_value == "192.8"
|
||||
Reference in New Issue
Block a user