Add unique ID to dsmr_reader sensors (#79101)

This commit is contained in:
Tom Puttemans 2022-09-27 14:46:18 +02:00 committed by GitHub
parent 88500145d2
commit a58f919972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,12 +40,12 @@ async def async_setup_platform(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, _: HomeAssistant,
config: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up DSMR Reader sensors from config entry.""" """Set up DSMR Reader sensors from config entry."""
async_add_entities(DSMRSensor(description) for description in SENSORS) async_add_entities(DSMRSensor(description, config_entry) for description in SENSORS)
class DSMRSensor(SensorEntity): class DSMRSensor(SensorEntity):
@ -53,12 +53,15 @@ class DSMRSensor(SensorEntity):
entity_description: DSMRReaderSensorEntityDescription entity_description: DSMRReaderSensorEntityDescription
def __init__(self, description: DSMRReaderSensorEntityDescription) -> None: def __init__(
self, description: DSMRReaderSensorEntityDescription, config_entry: ConfigEntry
) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description self.entity_description = description
slug = slugify(description.key.replace("/", "_")) slug = slugify(description.key.replace("/", "_"))
self.entity_id = f"sensor.{slug}" self.entity_id = f"sensor.{slug}"
self._attr_unique_id = f"{config_entry.entry_id}-{slug}"
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Subscribe to MQTT events.""" """Subscribe to MQTT events."""