mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
parent
e34853a82a
commit
17c0e18776
@ -27,7 +27,7 @@ from homeassistant.const import (
|
|||||||
UnitOfPower,
|
UnitOfPower,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import config_validation as cv, entity_registry as er
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
@ -397,30 +397,7 @@ class EDL21:
|
|||||||
self._OBIS_BLACKLIST.add(obis)
|
self._OBIS_BLACKLIST.add(obis)
|
||||||
|
|
||||||
if new_entities:
|
if new_entities:
|
||||||
self._hass.loop.create_task(self.add_entities(new_entities))
|
self._async_add_entities(new_entities, update_before_add=True)
|
||||||
|
|
||||||
async def add_entities(self, new_entities: list[EDL21Entity]) -> None:
|
|
||||||
"""Migrate old unique IDs, then add entities to hass."""
|
|
||||||
registry = er.async_get(self._hass)
|
|
||||||
|
|
||||||
for entity in new_entities:
|
|
||||||
old_entity_id = registry.async_get_entity_id(
|
|
||||||
"sensor", DOMAIN, entity.old_unique_id
|
|
||||||
)
|
|
||||||
if old_entity_id is not None and entity.unique_id is not None:
|
|
||||||
LOGGER.debug(
|
|
||||||
"Migrating unique_id from [%s] to [%s]",
|
|
||||||
entity.old_unique_id,
|
|
||||||
entity.unique_id,
|
|
||||||
)
|
|
||||||
if registry.async_get_entity_id("sensor", DOMAIN, entity.unique_id):
|
|
||||||
registry.async_remove(old_entity_id)
|
|
||||||
else:
|
|
||||||
registry.async_update_entity(
|
|
||||||
old_entity_id, new_unique_id=entity.unique_id
|
|
||||||
)
|
|
||||||
|
|
||||||
self._async_add_entities(new_entities, update_before_add=True)
|
|
||||||
|
|
||||||
|
|
||||||
class EDL21Entity(SensorEntity):
|
class EDL21Entity(SensorEntity):
|
||||||
@ -480,18 +457,13 @@ class EDL21Entity(SensorEntity):
|
|||||||
if self._async_remove_dispatcher:
|
if self._async_remove_dispatcher:
|
||||||
self._async_remove_dispatcher()
|
self._async_remove_dispatcher()
|
||||||
|
|
||||||
@property
|
|
||||||
def old_unique_id(self) -> str:
|
|
||||||
"""Return a less unique ID as used in the first version of edl21."""
|
|
||||||
return self._obis
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> str:
|
def native_value(self) -> str:
|
||||||
"""Return the value of the last received telegram."""
|
"""Return the value of the last received telegram."""
|
||||||
return self._telegram.get("value")
|
return self._telegram.get("value")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
||||||
"""Enumerate supported attributes."""
|
"""Enumerate supported attributes."""
|
||||||
return {
|
return {
|
||||||
self._state_attrs[k]: v
|
self._state_attrs[k]: v
|
||||||
@ -500,7 +472,7 @@ class EDL21Entity(SensorEntity):
|
|||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_unit_of_measurement(self):
|
def native_unit_of_measurement(self) -> str | None:
|
||||||
"""Return the unit of measurement."""
|
"""Return the unit of measurement."""
|
||||||
if (unit := self._telegram.get("unit")) is None or unit == 0:
|
if (unit := self._telegram.get("unit")) is None or unit == 0:
|
||||||
return None
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user