Add state class support to DSMR Reader (#53715)
This commit is contained in:
parent
e9a00ad4ce
commit
120a0bead0
2 changed files with 553 additions and 519 deletions
File diff suppressed because it is too large
Load diff
|
@ -4,39 +4,27 @@ from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
from .definitions import DEFINITIONS
|
from .definitions import SENSORS, DSMRReaderSensorEntityDescription
|
||||||
|
|
||||||
DOMAIN = "dsmr_reader"
|
DOMAIN = "dsmr_reader"
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up DSMR Reader sensors."""
|
"""Set up DSMR Reader sensors."""
|
||||||
|
async_add_entities(DSMRSensor(description) for description in SENSORS)
|
||||||
sensors = []
|
|
||||||
for topic in DEFINITIONS:
|
|
||||||
sensors.append(DSMRSensor(topic))
|
|
||||||
|
|
||||||
async_add_entities(sensors)
|
|
||||||
|
|
||||||
|
|
||||||
class DSMRSensor(SensorEntity):
|
class DSMRSensor(SensorEntity):
|
||||||
"""Representation of a DSMR sensor that is updated via MQTT."""
|
"""Representation of a DSMR sensor that is updated via MQTT."""
|
||||||
|
|
||||||
def __init__(self, topic):
|
entity_description: DSMRReaderSensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(self, description: DSMRReaderSensorEntityDescription) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
|
self.entity_description = description
|
||||||
|
|
||||||
self._definition = DEFINITIONS[topic]
|
slug = slugify(description.key.replace("/", "_"))
|
||||||
|
self.entity_id = f"sensor.{slug}"
|
||||||
self._entity_id = slugify(topic.replace("/", "_"))
|
|
||||||
self._topic = topic
|
|
||||||
|
|
||||||
self._name = self._definition.get("name", topic.split("/")[-1])
|
|
||||||
self._device_class = self._definition.get("device_class")
|
|
||||||
self._enable_default = self._definition.get("enable_default")
|
|
||||||
self._unit_of_measurement = self._definition.get("unit")
|
|
||||||
self._icon = self._definition.get("icon")
|
|
||||||
self._transform = self._definition.get("transform")
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to MQTT events."""
|
"""Subscribe to MQTT events."""
|
||||||
|
@ -44,47 +32,13 @@ class DSMRSensor(SensorEntity):
|
||||||
@callback
|
@callback
|
||||||
def message_received(message):
|
def message_received(message):
|
||||||
"""Handle new MQTT messages."""
|
"""Handle new MQTT messages."""
|
||||||
|
if self.entity_description.state is not None:
|
||||||
if self._transform is not None:
|
self._attr_state = self.entity_description.state(message.payload)
|
||||||
self._state = self._transform(message.payload)
|
|
||||||
else:
|
else:
|
||||||
self._state = message.payload
|
self._attr_state = message.payload
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
await mqtt.async_subscribe(self.hass, self._topic, message_received, 1)
|
await mqtt.async_subscribe(
|
||||||
|
self.hass, self.entity_description.key, message_received, 1
|
||||||
@property
|
)
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor supplied in constructor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def entity_id(self):
|
|
||||||
"""Return the entity ID for this sensor."""
|
|
||||||
return f"sensor.{self._entity_id}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
"""Return the current state of the entity."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device_class of this sensor."""
|
|
||||||
return self._device_class
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self):
|
|
||||||
"""Return the unit_of_measurement of this sensor."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
@property
|
|
||||||
def entity_registry_enabled_default(self) -> bool:
|
|
||||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
|
||||||
return self._enable_default
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon of this sensor."""
|
|
||||||
return self._icon
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue