Skip to content

Commit

Permalink
Support renaming of source entity_id
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jun 16, 2023
1 parent 41ffe82 commit 3563ef5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions custom_components/powercalc/__init__.py
Expand Up @@ -251,6 +251,7 @@ async def _create_standby_group(event: None) -> None:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Powercalc integration from a config entry."""
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

entry.async_on_unload(entry.add_update_listener(async_update_entry))
return True

Expand Down
30 changes: 28 additions & 2 deletions custom_components/powercalc/sensor.py
Expand Up @@ -25,11 +25,11 @@
CONF_NAME,
CONF_UNIQUE_ID,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity_registry import RegistryEntryDisabler
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED, RegistryEntryDisabler
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

Expand Down Expand Up @@ -279,6 +279,24 @@ async def async_setup_entry(
if CONF_UNIQUE_ID not in sensor_config:
sensor_config[CONF_UNIQUE_ID] = entry.unique_id

@callback
async def _entity_rename_listener(event: Event) -> None:
await handle_entity_id_rename(hass, entry, event)

@callback
def _filter_entity_id(event: Event) -> None:
return (

Check failure on line 288 in custom_components/powercalc/sensor.py

View workflow job for this annotation

GitHub Actions / mypy

No return value expected [return-value]

Check failure on line 288 in custom_components/powercalc/sensor.py

View workflow job for this annotation

GitHub Actions / mypy

No return value expected [return-value]
event.data["action"] == "update" and
"old_entity_id" in event.data
and event.data["old_entity_id"] == sensor_config.get(CONF_ENTITY_ID)
)

hass.bus.async_listen(
EVENT_ENTITY_REGISTRY_UPDATED,
_entity_rename_listener,
event_filter=_filter_entity_id,
)

await _async_setup_entities(
hass,
sensor_config,
Expand All @@ -290,6 +308,14 @@ async def async_setup_entry(
await add_to_associated_group(hass, entry)


async def handle_entity_id_rename(hass: HomeAssistant, config_entry: ConfigEntry, event: Event) -> None:
"""Handle entity registry updated."""
old_entity_id = event.data["old_entity_id"]
new_entity_id = event.data[CONF_ENTITY_ID]
_LOGGER.debug(f"Entity id has been changed, updating powercalc config. old_id={old_entity_id}, new_id={new_entity_id}")
hass.config_entries.async_update_entry(config_entry, data={**config_entry.data, CONF_ENTITY_ID: new_entity_id})


async def _async_setup_entities(
hass: HomeAssistant,
config: dict[str, Any],
Expand Down
47 changes: 47 additions & 0 deletions tests/test_sensor.py
Expand Up @@ -746,3 +746,50 @@ async def test_create_config_entry_without_energy_sensor(

assert hass.states.get("sensor.testentry_power")
assert not hass.states.get("sensor.testentry_energy")


async def test_rename_source_entity_id(hass: HomeAssistant) -> None:
light_id = "sensor.my_light"
entity_reg = mock_registry(
hass,
{
light_id: er.RegistryEntry(
entity_id=light_id,
disabled_by=er.RegistryEntryDisabler.DEVICE,
unique_id="1234",
platform="light",
),
},
)

entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_CREATE_ENERGY_SENSOR: False,
CONF_NAME: "testentry",
CONF_ENTITY_ID: light_id,
CONF_FIXED: {CONF_POWER: 50},
},
unique_id="abcd",
)
entry.add_to_hass(hass)

await run_powercalc_setup(
hass,
{},
)

new_light_id = "sensor.my_light_new"
entity_reg.async_update_entity(entity_id=light_id, new_entity_id=new_light_id)
await hass.async_block_till_done()

changed_entry = hass.config_entries.async_get_entry(entry.entry_id)
assert changed_entry.data.get(CONF_ENTITY_ID) == new_light_id

hass.states.async_set(new_light_id, STATE_ON)
await hass.async_block_till_done()

power_state = hass.states.get("sensor.testentry_power")
assert power_state.state == "50.00"
assert power_state.attributes.get(ATTR_SOURCE_ENTITY) == new_light_id

0 comments on commit 3563ef5

Please sign in to comment.