Skip to content

Commit 2ab3222

Browse files
emontnemeryfrenck
authored andcommitted
Handle changes to source entity in utility_meter (home-assistant#146526)
1 parent 273ccb3 commit 2ab3222

File tree

2 files changed

+393
-1
lines changed

2 files changed

+393
-1
lines changed

homeassistant/components/utility_meter/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
entity_registry as er,
1818
)
1919
from homeassistant.helpers.device import (
20+
async_entity_id_to_device_id,
2021
async_remove_stale_devices_links_keep_entity_device,
2122
)
2223
from homeassistant.helpers.dispatcher import async_dispatcher_send
24+
from homeassistant.helpers.helper_integration import async_handle_source_entity_changes
2325
from homeassistant.helpers.typing import ConfigType
2426

2527
from .const import (
@@ -217,6 +219,29 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
217219
)
218220
return False
219221

222+
def set_source_entity_id_or_uuid(source_entity_id: str) -> None:
223+
hass.config_entries.async_update_entry(
224+
entry,
225+
options={**entry.options, CONF_SOURCE_SENSOR: source_entity_id},
226+
)
227+
228+
async def source_entity_removed() -> None:
229+
# The source entity has been removed, we need to clean the device links.
230+
async_remove_stale_devices_links_keep_entity_device(hass, entry.entry_id, None)
231+
232+
entry.async_on_unload(
233+
async_handle_source_entity_changes(
234+
hass,
235+
helper_config_entry_id=entry.entry_id,
236+
set_source_entity_id_or_uuid=set_source_entity_id_or_uuid,
237+
source_device_id=async_entity_id_to_device_id(
238+
hass, entry.options[CONF_SOURCE_SENSOR]
239+
),
240+
source_entity_id_or_uuid=entry.options[CONF_SOURCE_SENSOR],
241+
source_entity_removed=source_entity_removed,
242+
)
243+
)
244+
220245
if not entry.options.get(CONF_TARIFFS):
221246
# Only a single meter sensor is required
222247
hass.data[DATA_UTILITY][entry.entry_id][CONF_TARIFF_ENTITY] = None

0 commit comments

Comments
 (0)