async def connection_factory(*args, **kwargs): """Return mocked out Asyncio classes.""" if args[1] == "5L": protocol.telegram = { LUXEMBOURG_EQUIPMENT_IDENTIFIER: CosemObject([{ "value": "12345678", "unit": "" }]), EQUIPMENT_IDENTIFIER_GAS: CosemObject([{ "value": "123456789", "unit": "" }]), } if args[1] == "5S": protocol.telegram = { P1_MESSAGE_TIMESTAMP: CosemObject([{ "value": "12345678", "unit": "" }]), } if args[1] == "Q3D": protocol.telegram = { Q3D_EQUIPMENT_IDENTIFIER: CosemObject([{ "value": "12345678", "unit": "" }]), } return (transport, protocol)
async def dsmr_connection_send_validate_fixture(hass): """Fixture that mocks serial connection.""" transport = MagicMock(spec=asyncio.Transport) protocol = MagicMock(spec=DSMRProtocol) protocol.telegram = { EQUIPMENT_IDENTIFIER: CosemObject([{ "value": "12345678", "unit": "" }]), EQUIPMENT_IDENTIFIER_GAS: CosemObject([{ "value": "123456789", "unit": "" }]), } async def connection_factory(*args, **kwargs): """Return mocked out Asyncio classes.""" if args[1] == "5L": protocol.telegram = { LUXEMBOURG_EQUIPMENT_IDENTIFIER: CosemObject([{ "value": "12345678", "unit": "" }]), EQUIPMENT_IDENTIFIER_GAS: CosemObject([{ "value": "123456789", "unit": "" }]), } return (transport, protocol) connection_factory = MagicMock(wraps=connection_factory) async def wait_closed(): if isinstance(connection_factory.call_args_list[0][0][2], str): # TCP telegram_callback = connection_factory.call_args_list[0][0][3] else: # Serial telegram_callback = connection_factory.call_args_list[0][0][2] telegram_callback(protocol.telegram) protocol.wait_closed = wait_closed with patch( "homeassistant.components.dsmr.config_flow.create_dsmr_reader", connection_factory, ), patch( "homeassistant.components.dsmr.config_flow.create_tcp_dsmr_reader", connection_factory, ): yield (connection_factory, transport, protocol)
async def test_default_setup(hass, mock_connection_factory): """Test the default setup.""" (connection_factory, transport, protocol) = mock_connection_factory from dsmr_parser.obis_references import ( CURRENT_ELECTRICITY_USAGE, ELECTRICITY_ACTIVE_TARIFF, GAS_METER_READING, ) from dsmr_parser.objects import CosemObject, MBusObject config = {"platform": "dsmr"} telegram = { CURRENT_ELECTRICITY_USAGE: CosemObject( [{"value": Decimal("0.0"), "unit": ENERGY_KILO_WATT_HOUR}] ), ELECTRICITY_ACTIVE_TARIFF: CosemObject([{"value": "0001", "unit": ""}]), GAS_METER_READING: MBusObject( [ {"value": datetime.datetime.fromtimestamp(1551642213)}, {"value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS}, ] ), } with assert_setup_component(1): await async_setup_component(hass, "sensor", {"sensor": config}) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # make sure entities have been created and return 'unknown' state power_consumption = hass.states.get("sensor.power_consumption") assert power_consumption.state == "unknown" assert power_consumption.attributes.get("unit_of_measurement") is None # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # ensure entities have new state value after incoming telegram power_consumption = hass.states.get("sensor.power_consumption") assert power_consumption.state == "0.0" assert ( power_consumption.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR ) # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "low" assert power_tariff.attributes.get("unit_of_measurement") == "" # check if gas consumption is parsed correctly gas_consumption = hass.states.get("sensor.gas_consumption") assert gas_consumption.state == "745.695" assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
def test_default_setup(hass, monkeypatch): """Test the default setup.""" from dsmr_parser.obis_references import ( CURRENT_ELECTRICITY_USAGE, ELECTRICITY_ACTIVE_TARIFF, ) from dsmr_parser.objects import CosemObject config = {'platform': 'dsmr'} telegram = { CURRENT_ELECTRICITY_USAGE: CosemObject([{ 'value': Decimal('0.1'), 'unit': 'kWh' }]), ELECTRICITY_ACTIVE_TARIFF: CosemObject([{ 'value': '0001', 'unit': '' }]), } # mock for injecting DSMR telegram dsmr = Mock(return_value=Mock()) monkeypatch.setattr('dsmr_parser.protocol.create_dsmr_reader', dsmr) with assert_setup_component(1): yield from async_setup_component(hass, 'sensor', {'sensor': config}) telegram_callback = dsmr.call_args_list[0][0][2] # make sure entities have been created and return 'unknown' state power_consumption = hass.states.get('sensor.power_consumption') assert power_consumption.state == 'unknown' assert power_consumption.attributes.get('unit_of_measurement') is None # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update yield from asyncio.sleep(0, loop=hass.loop) # ensure entities have new state value after incoming telegram power_consumption = hass.states.get('sensor.power_consumption') assert power_consumption.state == '0.1' assert power_consumption.attributes.get('unit_of_measurement') is 'kWh' # tariff should be translated in human readable and have no unit power_tariff = hass.states.get('sensor.power_tariff') assert power_tariff.state == 'low' assert power_tariff.attributes.get('unit_of_measurement') is None
def test_default_setup(hass, mock_connection_factory): """Test the default setup.""" (connection_factory, transport, protocol) = mock_connection_factory from dsmr_parser.obis_references import ( CURRENT_ELECTRICITY_USAGE, ELECTRICITY_ACTIVE_TARIFF, ) from dsmr_parser.objects import CosemObject config = {"platform": "dsmr"} telegram = { CURRENT_ELECTRICITY_USAGE: CosemObject([{ "value": Decimal("0.0"), "unit": "kWh" }]), ELECTRICITY_ACTIVE_TARIFF: CosemObject([{ "value": "0001", "unit": "" }]), } with assert_setup_component(1): yield from async_setup_component(hass, "sensor", {"sensor": config}) telegram_callback = connection_factory.call_args_list[0][0][2] # make sure entities have been created and return 'unknown' state power_consumption = hass.states.get("sensor.power_consumption") assert power_consumption.state == "unknown" assert power_consumption.attributes.get("unit_of_measurement") is None # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update yield from asyncio.sleep(0) # ensure entities have new state value after incoming telegram power_consumption = hass.states.get("sensor.power_consumption") assert power_consumption.state == "0.0" assert power_consumption.attributes.get("unit_of_measurement") == "kWh" # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "low" assert power_tariff.attributes.get("unit_of_measurement") == ""
async def test_belgian_meter_low(hass, mock_connection_factory): """Test if Belgian meter is correctly parsed.""" (connection_factory, transport, protocol) = mock_connection_factory from dsmr_parser.obis_references import ELECTRICITY_ACTIVE_TARIFF from dsmr_parser.objects import CosemObject config = {"platform": "dsmr", "dsmr_version": "5B"} telegram = { ELECTRICITY_ACTIVE_TARIFF: CosemObject([{ "value": "0002", "unit": "" }]) } with assert_setup_component(1): await async_setup_component(hass, "sensor", {"sensor": config}) telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "low" assert power_tariff.attributes.get("unit_of_measurement") == ""
async def test_belgian_meter(hass, dsmr_connection_fixture): """Test if Belgian meter is correctly parsed.""" (connection_factory, transport, protocol) = dsmr_connection_fixture from dsmr_parser.obis_references import ( BELGIUM_HOURLY_GAS_METER_READING, ELECTRICITY_ACTIVE_TARIFF, ) from dsmr_parser.objects import CosemObject, MBusObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "5B", "precision": 4, "reconnect_interval": 30, "serial_id": "1234", "serial_id_gas": "5678", } entry_options = { "time_between_update": 0, } telegram = { BELGIUM_HOURLY_GAS_METER_READING: MBusObject( [ {"value": datetime.datetime.fromtimestamp(1551642213)}, {"value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS}, ] ), ELECTRICITY_ACTIVE_TARIFF: CosemObject([{"value": "0001", "unit": ""}]), } mock_entry = MockConfigEntry( domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data, options=entry_options ) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "normal" assert power_tariff.attributes.get("unit_of_measurement") == "" # check if gas consumption is parsed correctly gas_consumption = hass.states.get("sensor.gas_consumption") assert gas_consumption.state == "745.695" assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
async def test_belgian_meter_low(hass, dsmr_connection_fixture): """Test if Belgian meter is correctly parsed.""" (connection_factory, transport, protocol) = dsmr_connection_fixture from dsmr_parser.obis_references import ELECTRICITY_ACTIVE_TARIFF from dsmr_parser.objects import CosemObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "5B", "precision": 4, "reconnect_interval": 30, "serial_id": "1234", "serial_id_gas": "5678", } entry_options = { "time_between_update": 0, } telegram = { ELECTRICITY_ACTIVE_TARIFF: CosemObject([{ "value": "0002", "unit": "" }]) } mock_entry = MockConfigEntry(domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data, options=entry_options) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "low" assert power_tariff.attributes.get(ATTR_DEVICE_CLASS) is None assert power_tariff.attributes.get(ATTR_ICON) == "mdi:flash" assert power_tariff.attributes.get(ATTR_LAST_RESET) is None assert power_tariff.attributes.get(ATTR_STATE_CLASS) is None assert power_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ""
async def test_belgian_meter(hass, mock_connection_factory): """Test if Belgian meter is correctly parsed.""" (connection_factory, transport, protocol) = mock_connection_factory from dsmr_parser.obis_references import ( BELGIUM_HOURLY_GAS_METER_READING, ELECTRICITY_ACTIVE_TARIFF, ) from dsmr_parser.objects import CosemObject, MBusObject config = {"platform": "dsmr", "dsmr_version": "5B"} telegram = { BELGIUM_HOURLY_GAS_METER_READING: MBusObject([ { "value": datetime.datetime.fromtimestamp(1551642213) }, { "value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS }, ]), ELECTRICITY_ACTIVE_TARIFF: CosemObject([{ "value": "0001", "unit": "" }]), } with assert_setup_component(1): await async_setup_component(hass, "sensor", {"sensor": config}) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "normal" assert power_tariff.attributes.get("unit_of_measurement") == "" # check if gas consumption is parsed correctly gas_consumption = hass.states.get("sensor.gas_consumption") assert gas_consumption.state == "745.695" assert gas_consumption.attributes.get( "unit_of_measurement") == VOLUME_CUBIC_METERS
async def test_belgian_meter_low(hass, mock_connection_factory): """Test if Belgian meter is correctly parsed.""" (connection_factory, transport, protocol) = mock_connection_factory from dsmr_parser.obis_references import ELECTRICITY_ACTIVE_TARIFF from dsmr_parser.objects import CosemObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "5B", "precision": 4, "reconnect_interval": 30, } telegram = {ELECTRICITY_ACTIVE_TARIFF: CosemObject([{"value": "0002", "unit": ""}])} mock_entry = MockConfigEntry( domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data ) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "low" assert power_tariff.attributes.get("unit_of_measurement") == "" await hass.config_entries.async_unload(mock_entry.entry_id) assert mock_entry.state == "not_loaded"
async def test_default_setup(hass, dsmr_connection_fixture): """Test the default setup.""" (connection_factory, transport, protocol) = dsmr_connection_fixture from dsmr_parser.obis_references import ( CURRENT_ELECTRICITY_USAGE, ELECTRICITY_ACTIVE_TARIFF, GAS_METER_READING, ) from dsmr_parser.objects import CosemObject, MBusObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "2.2", "precision": 4, "reconnect_interval": 30, "serial_id": "1234", "serial_id_gas": "5678", } entry_options = { "time_between_update": 0, } telegram = { CURRENT_ELECTRICITY_USAGE: CosemObject([{ "value": Decimal("0.0"), "unit": ENERGY_KILO_WATT_HOUR }]), ELECTRICITY_ACTIVE_TARIFF: CosemObject([{ "value": "0001", "unit": "" }]), GAS_METER_READING: MBusObject([ { "value": datetime.datetime.fromtimestamp(1551642213) }, { "value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS }, ]), } mock_entry = MockConfigEntry(domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data, options=entry_options) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() registry = er.async_get(hass) entry = registry.async_get("sensor.power_consumption") assert entry assert entry.unique_id == "1234_Power_Consumption" entry = registry.async_get("sensor.gas_consumption") assert entry assert entry.unique_id == "5678_Gas_Consumption" telegram_callback = connection_factory.call_args_list[0][0][2] # make sure entities have been created and return 'unknown' state power_consumption = hass.states.get("sensor.power_consumption") assert power_consumption.state == "unknown" assert power_consumption.attributes.get("unit_of_measurement") is None # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # ensure entities have new state value after incoming telegram power_consumption = hass.states.get("sensor.power_consumption") assert power_consumption.state == "0.0" assert (power_consumption.attributes.get("unit_of_measurement") == ENERGY_KILO_WATT_HOUR) # tariff should be translated in human readable and have no unit power_tariff = hass.states.get("sensor.power_tariff") assert power_tariff.state == "low" assert power_tariff.attributes.get("unit_of_measurement") == "" # check if gas consumption is parsed correctly gas_consumption = hass.states.get("sensor.gas_consumption") assert gas_consumption.state == "745.695" assert gas_consumption.attributes.get( "unit_of_measurement") == VOLUME_CUBIC_METERS
async def test_luxembourg_meter(hass, dsmr_connection_fixture): """Test if v5 meter is correctly parsed.""" (connection_factory, transport, protocol) = dsmr_connection_fixture from dsmr_parser.obis_references import ( HOURLY_GAS_METER_READING, LUXEMBOURG_ELECTRICITY_DELIVERED_TARIFF_GLOBAL, LUXEMBOURG_ELECTRICITY_USED_TARIFF_GLOBAL, ) from dsmr_parser.objects import CosemObject, MBusObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "5L", "precision": 4, "reconnect_interval": 30, "serial_id": "1234", "serial_id_gas": "5678", } entry_options = { "time_between_update": 0, } telegram = { HOURLY_GAS_METER_READING: MBusObject([ { "value": datetime.datetime.fromtimestamp(1551642213) }, { "value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS }, ]), LUXEMBOURG_ELECTRICITY_USED_TARIFF_GLOBAL: CosemObject([{ "value": Decimal(123.456), "unit": ENERGY_KILO_WATT_HOUR }]), LUXEMBOURG_ELECTRICITY_DELIVERED_TARIFF_GLOBAL: CosemObject([{ "value": Decimal(654.321), "unit": ENERGY_KILO_WATT_HOUR }]), } mock_entry = MockConfigEntry(domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data, options=entry_options) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) power_tariff = hass.states.get("sensor.energy_consumption_total") assert power_tariff.state == "123.456" assert power_tariff.attributes.get( "unit_of_measurement") == ENERGY_KILO_WATT_HOUR power_tariff = hass.states.get("sensor.energy_production_total") assert power_tariff.state == "654.321" assert power_tariff.attributes.get( "unit_of_measurement") == ENERGY_KILO_WATT_HOUR # check if gas consumption is parsed correctly gas_consumption = hass.states.get("sensor.gas_consumption") assert gas_consumption.state == "745.695" assert gas_consumption.attributes.get( "unit_of_measurement") == VOLUME_CUBIC_METERS
async def test_easymeter(hass, dsmr_connection_fixture): """Test if Q3D meter is correctly parsed.""" (connection_factory, transport, protocol) = dsmr_connection_fixture from dsmr_parser.obis_references import ( ELECTRICITY_EXPORTED_TOTAL, ELECTRICITY_IMPORTED_TOTAL, ) from dsmr_parser.objects import CosemObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "Q3D", "precision": 4, "reconnect_interval": 30, "serial_id": None, "serial_id_gas": None, } entry_options = { "time_between_update": 0, } telegram = { ELECTRICITY_IMPORTED_TOTAL: CosemObject([{ "value": Decimal(54184.6316), "unit": ENERGY_KILO_WATT_HOUR }]), ELECTRICITY_EXPORTED_TOTAL: CosemObject([{ "value": Decimal(19981.1069), "unit": ENERGY_KILO_WATT_HOUR }]), } mock_entry = MockConfigEntry( domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data, options=entry_options, ) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) active_tariff = hass.states.get( "sensor.electricity_meter_energy_consumption_total") assert active_tariff.state == "54184.6316" assert active_tariff.attributes.get( ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert active_tariff.attributes.get(ATTR_ICON) is None assert (active_tariff.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING) assert (active_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR) active_tariff = hass.states.get( "sensor.electricity_meter_energy_production_total") assert active_tariff.state == "19981.1069" assert (active_tariff.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING) assert (active_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR)
async def test_v5_meter(hass, dsmr_connection_fixture): """Test if v5 meter is correctly parsed.""" (connection_factory, transport, protocol) = dsmr_connection_fixture from dsmr_parser.obis_references import ( ELECTRICITY_ACTIVE_TARIFF, HOURLY_GAS_METER_READING, ) from dsmr_parser.objects import CosemObject, MBusObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "5", "precision": 4, "reconnect_interval": 30, "serial_id": "1234", "serial_id_gas": "5678", } entry_options = { "time_between_update": 0, } telegram = { HOURLY_GAS_METER_READING: MBusObject([ { "value": datetime.datetime.fromtimestamp(1551642213) }, { "value": Decimal(745.695), "unit": "m3" }, ]), ELECTRICITY_ACTIVE_TARIFF: CosemObject([{ "value": "0001", "unit": "" }]), } mock_entry = MockConfigEntry(domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data, options=entry_options) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) # tariff should be translated in human readable and have no unit active_tariff = hass.states.get("sensor.electricity_meter_active_tariff") assert active_tariff.state == "low" assert active_tariff.attributes.get(ATTR_DEVICE_CLASS) is None assert active_tariff.attributes.get(ATTR_ICON) == "mdi:flash" assert active_tariff.attributes.get(ATTR_STATE_CLASS) is None assert active_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "" # check if gas consumption is parsed correctly gas_consumption = hass.states.get("sensor.gas_meter_gas_consumption") assert gas_consumption.state == "745.695" assert gas_consumption.attributes.get( ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS assert (gas_consumption.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING) assert (gas_consumption.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS)
def parse(self, line): return CosemObject(self._parse(line))
async def test_swedish_meter(hass, dsmr_connection_fixture): """Test if v5 meter is correctly parsed.""" (connection_factory, transport, protocol) = dsmr_connection_fixture from dsmr_parser.obis_references import ( SWEDEN_ELECTRICITY_DELIVERED_TARIFF_GLOBAL, SWEDEN_ELECTRICITY_USED_TARIFF_GLOBAL, ) from dsmr_parser.objects import CosemObject entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "5S", "precision": 4, "reconnect_interval": 30, "serial_id": None, "serial_id_gas": None, } entry_options = { "time_between_update": 0, } telegram = { SWEDEN_ELECTRICITY_USED_TARIFF_GLOBAL: CosemObject([{ "value": Decimal(123.456), "unit": ENERGY_KILO_WATT_HOUR }]), SWEDEN_ELECTRICITY_DELIVERED_TARIFF_GLOBAL: CosemObject([{ "value": Decimal(654.321), "unit": ENERGY_KILO_WATT_HOUR }]), } mock_entry = MockConfigEntry(domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data, options=entry_options) mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() telegram_callback = connection_factory.call_args_list[0][0][2] # simulate a telegram pushed from the smartmeter and parsed by dsmr_parser telegram_callback(telegram) # after receiving telegram entities need to have the chance to update await asyncio.sleep(0) power_tariff = hass.states.get("sensor.energy_consumption_total") assert power_tariff.state == "123.456" assert power_tariff.attributes.get( ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY assert power_tariff.attributes.get(ATTR_ICON) is None assert power_tariff.attributes.get( ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING assert (power_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR) power_tariff = hass.states.get("sensor.energy_production_total") assert power_tariff.state == "654.321" assert power_tariff.attributes.get( ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING assert (power_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR)