def test_derivative(): """Test calculation of derivative value.""" from dsmr_parser.objects import MBusObject config = {'platform': 'dsmr'} entity = DerivativeDSMREntity('test', '1.0.0', config) yield from entity.async_update() assert entity.state is None, 'initial state not unknown' entity.telegram = { '1.0.0': MBusObject([ {'value': 1}, {'value': 1, 'unit': 'm3'}, ]) } yield from entity.async_update() assert entity.state is None, \ 'state after first update should still be unknown' entity.telegram = { '1.0.0': MBusObject([ {'value': 2}, {'value': 2, 'unit': 'm3'}, ]) } yield from entity.async_update() assert entity.state == 1, \ 'state should be difference between first and second update' assert entity.unit_of_measurement == 'm3/h'
async def test_derivative(): """Test calculation of derivative value.""" from dsmr_parser.objects import MBusObject config = {"platform": "dsmr"} entity = DerivativeDSMREntity("test", "test_device", "5678", "1.0.0", config, False) await entity.async_update() assert entity.state is None, "initial state not unknown" entity.telegram = { "1.0.0": MBusObject([ { "value": datetime.datetime.fromtimestamp(1551642213) }, { "value": Decimal(745.695), "unit": VOLUME_CUBIC_METERS }, ]) } await entity.async_update() assert entity.state is None, "state after first update should still be unknown" entity.telegram = { "1.0.0": MBusObject([ { "value": datetime.datetime.fromtimestamp(1551642543) }, { "value": Decimal(745.698), "unit": VOLUME_CUBIC_METERS }, ]) } await entity.async_update() assert ( abs(entity.state - 0.033) < 0.00001 ), "state should be hourly usage calculated from first and second update" assert entity.unit_of_measurement == VOLUME_FLOW_RATE_CUBIC_METERS_PER_HOUR
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_derivative(): """Test calculation of derivative value.""" from dsmr_parser.objects import MBusObject config = {'platform': 'dsmr'} entity = DerivativeDSMREntity('test', '1.0.0', config) yield from entity.async_update() assert entity.state is None, 'initial state not unknown' entity.telegram = { '1.0.0': MBusObject([ { 'value': datetime.datetime.fromtimestamp(1551642213) }, { 'value': Decimal(745.695), 'unit': 'm3' }, ]) } yield from entity.async_update() assert entity.state is None, \ 'state after first update should still be unknown' entity.telegram = { '1.0.0': MBusObject([ { 'value': datetime.datetime.fromtimestamp(1551642543) }, { 'value': Decimal(745.698), 'unit': 'm3' }, ]) } yield from entity.async_update() assert abs(entity.state - 0.033) < 0.00001, \ 'state should be hourly usage calculated from first and second update' assert entity.unit_of_measurement == 'm3/h'
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(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_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_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 MBusObject(self._parse(line))