Use device/state class enums in DSMR (#60791)

pull/60816/head
Franck Nijhof 2021-12-02 04:15:17 +01:00 committed by GitHub
parent 8e715064cc
commit 58fdcfb6b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 85 deletions

View File

@ -5,17 +5,7 @@ import logging
from dsmr_parser import obis_references
from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
)
from homeassistant.const import (
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_GAS,
DEVICE_CLASS_POWER,
DEVICE_CLASS_VOLTAGE,
)
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from .models import DSMRSensorEntityDescription
@ -50,16 +40,16 @@ SENSORS: tuple[DSMRSensorEntityDescription, ...] = (
DSMRSensorEntityDescription(
key=obis_references.CURRENT_ELECTRICITY_USAGE,
name="Power Consumption",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
force_update=True,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.CURRENT_ELECTRICITY_DELIVERY,
name="Power Production",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
force_update=True,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.ELECTRICITY_ACTIVE_TARIFF,
@ -71,75 +61,75 @@ SENSORS: tuple[DSMRSensorEntityDescription, ...] = (
key=obis_references.ELECTRICITY_USED_TARIFF_1,
name="Energy Consumption (tarif 1)",
dsmr_versions={"2.2", "4", "5", "5B", "5L"},
device_class=DEVICE_CLASS_ENERGY,
device_class=SensorDeviceClass.ENERGY,
force_update=True,
state_class=STATE_CLASS_TOTAL_INCREASING,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.ELECTRICITY_USED_TARIFF_2,
name="Energy Consumption (tarif 2)",
dsmr_versions={"2.2", "4", "5", "5B", "5L"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.ELECTRICITY_DELIVERED_TARIFF_1,
name="Energy Production (tarif 1)",
dsmr_versions={"2.2", "4", "5", "5B", "5L"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.ELECTRICITY_DELIVERED_TARIFF_2,
name="Energy Production (tarif 2)",
dsmr_versions={"2.2", "4", "5", "5B", "5L"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE,
name="Power Consumption Phase L1",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE,
name="Power Consumption Phase L2",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE,
name="Power Consumption Phase L3",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE,
name="Power Production Phase L1",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE,
name="Power Production Phase L2",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE,
name="Power Production Phase L3",
device_class=DEVICE_CLASS_POWER,
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.SHORT_POWER_FAILURE_COUNT,
@ -197,84 +187,84 @@ SENSORS: tuple[DSMRSensorEntityDescription, ...] = (
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_VOLTAGE_L1,
name="Voltage Phase L1",
device_class=DEVICE_CLASS_VOLTAGE,
device_class=SensorDeviceClass.VOLTAGE,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_VOLTAGE_L2,
name="Voltage Phase L2",
device_class=DEVICE_CLASS_VOLTAGE,
device_class=SensorDeviceClass.VOLTAGE,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_VOLTAGE_L3,
name="Voltage Phase L3",
device_class=DEVICE_CLASS_VOLTAGE,
device_class=SensorDeviceClass.VOLTAGE,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_CURRENT_L1,
name="Current Phase L1",
device_class=DEVICE_CLASS_CURRENT,
device_class=SensorDeviceClass.CURRENT,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_CURRENT_L2,
name="Current Phase L2",
device_class=DEVICE_CLASS_CURRENT,
device_class=SensorDeviceClass.CURRENT,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.INSTANTANEOUS_CURRENT_L3,
name="Current Phase L3",
device_class=DEVICE_CLASS_CURRENT,
device_class=SensorDeviceClass.CURRENT,
entity_registry_enabled_default=False,
state_class=STATE_CLASS_MEASUREMENT,
state_class=SensorStateClass.MEASUREMENT,
),
DSMRSensorEntityDescription(
key=obis_references.LUXEMBOURG_ELECTRICITY_USED_TARIFF_GLOBAL,
name="Energy Consumption (total)",
dsmr_versions={"5L"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.LUXEMBOURG_ELECTRICITY_DELIVERED_TARIFF_GLOBAL,
name="Energy Production (total)",
dsmr_versions={"5L"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.SWEDEN_ELECTRICITY_USED_TARIFF_GLOBAL,
name="Energy Consumption (total)",
dsmr_versions={"5S"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.SWEDEN_ELECTRICITY_DELIVERED_TARIFF_GLOBAL,
name="Energy Production (total)",
dsmr_versions={"5S"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.ELECTRICITY_IMPORTED_TOTAL,
name="Energy Consumption (total)",
dsmr_versions={"2.2", "4", "5", "5B"},
force_update=True,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.HOURLY_GAS_METER_READING,
@ -282,8 +272,8 @@ SENSORS: tuple[DSMRSensorEntityDescription, ...] = (
dsmr_versions={"4", "5", "5L"},
is_gas=True,
force_update=True,
device_class=DEVICE_CLASS_GAS,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.BELGIUM_HOURLY_GAS_METER_READING,
@ -291,8 +281,8 @@ SENSORS: tuple[DSMRSensorEntityDescription, ...] = (
dsmr_versions={"5B"},
is_gas=True,
force_update=True,
device_class=DEVICE_CLASS_GAS,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING,
),
DSMRSensorEntityDescription(
key=obis_references.GAS_METER_READING,
@ -300,7 +290,7 @@ SENSORS: tuple[DSMRSensorEntityDescription, ...] = (
dsmr_versions={"2.2"},
is_gas=True,
force_update=True,
device_class=DEVICE_CLASS_GAS,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=SensorDeviceClass.GAS,
state_class=SensorStateClass.TOTAL_INCREASING,
),
)

View File

@ -16,16 +16,13 @@ from homeassistant.components.dsmr.const import DOMAIN
from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
DOMAIN as SENSOR_DOMAIN,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_GAS,
DEVICE_CLASS_POWER,
ENERGY_KILO_WATT_HOUR,
STATE_UNKNOWN,
VOLUME_CUBIC_METERS,
@ -134,9 +131,14 @@ async def test_default_setup(hass, dsmr_connection_fixture):
# make sure entities have been created and return 'unknown' state
power_consumption = hass.states.get("sensor.power_consumption")
assert power_consumption.state == STATE_UNKNOWN
assert power_consumption.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER
assert (
power_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
)
assert power_consumption.attributes.get(ATTR_ICON) is None
assert power_consumption.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
assert (
power_consumption.attributes.get(ATTR_STATE_CLASS)
== SensorStateClass.MEASUREMENT
)
assert power_consumption.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
# simulate a telegram pushed from the smartmeter and parsed by dsmr_parser
@ -163,9 +165,10 @@ async def test_default_setup(hass, dsmr_connection_fixture):
# 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(ATTR_DEVICE_CLASS) == DEVICE_CLASS_GAS
assert gas_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS
assert (
gas_consumption.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
gas_consumption.attributes.get(ATTR_STATE_CLASS)
== SensorStateClass.TOTAL_INCREASING
)
assert (
gas_consumption.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS
@ -261,10 +264,11 @@ async def test_v4_meter(hass, dsmr_connection_fixture):
# 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(ATTR_DEVICE_CLASS) == DEVICE_CLASS_GAS
assert gas_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS
assert gas_consumption.attributes.get("unit_of_measurement") == VOLUME_CUBIC_METERS
assert (
gas_consumption.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
gas_consumption.attributes.get(ATTR_STATE_CLASS)
== SensorStateClass.TOTAL_INCREASING
)
assert (
gas_consumption.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS
@ -331,9 +335,10 @@ async def test_v5_meter(hass, dsmr_connection_fixture):
# 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(ATTR_DEVICE_CLASS) == DEVICE_CLASS_GAS
assert gas_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS
assert (
gas_consumption.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
gas_consumption.attributes.get(ATTR_STATE_CLASS)
== SensorStateClass.TOTAL_INCREASING
)
assert (
gas_consumption.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS
@ -397,9 +402,12 @@ async def test_luxembourg_meter(hass, dsmr_connection_fixture):
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_DEVICE_CLASS) == SensorDeviceClass.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_STATE_CLASS)
== SensorStateClass.TOTAL_INCREASING
)
assert (
power_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
)
@ -411,9 +419,10 @@ async def test_luxembourg_meter(hass, dsmr_connection_fixture):
# 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(ATTR_DEVICE_CLASS) == DEVICE_CLASS_GAS
assert gas_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS
assert (
gas_consumption.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
gas_consumption.attributes.get(ATTR_STATE_CLASS)
== SensorStateClass.TOTAL_INCREASING
)
assert (
gas_consumption.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS
@ -480,9 +489,10 @@ async def test_belgian_meter(hass, dsmr_connection_fixture):
# 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(ATTR_DEVICE_CLASS) is DEVICE_CLASS_GAS
assert gas_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS
assert (
gas_consumption.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
gas_consumption.attributes.get(ATTR_STATE_CLASS)
== SensorStateClass.TOTAL_INCREASING
)
assert (
gas_consumption.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == VOLUME_CUBIC_METERS
@ -586,16 +596,22 @@ async def test_swedish_meter(hass, dsmr_connection_fixture):
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_DEVICE_CLASS) == SensorDeviceClass.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_STATE_CLASS)
== SensorStateClass.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_STATE_CLASS)
== SensorStateClass.TOTAL_INCREASING
)
assert (
power_tariff.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
)