Skip to content

Commit 232f853

Browse files
emontnemeryfrenck
authored andcommitted
Simplify helper_integration.async_handle_source_entity_changes (home-assistant#146516)
1 parent 91e296a commit 232f853

File tree

4 files changed

+7
-30
lines changed

4 files changed

+7
-30
lines changed

homeassistant/components/derivative/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
from __future__ import annotations
44

5-
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
65
from homeassistant.config_entries import ConfigEntry
76
from homeassistant.const import CONF_SOURCE, Platform
87
from homeassistant.core import HomeAssistant
9-
from homeassistant.helpers import entity_registry as er
108
from homeassistant.helpers.device import (
119
async_entity_id_to_device_id,
1210
async_remove_stale_devices_links_keep_entity_device,
1311
)
1412
from homeassistant.helpers.helper_integration import async_handle_source_entity_changes
1513

16-
from .const import DOMAIN
17-
1814

1915
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2016
"""Set up Derivative from a config entry."""
@@ -33,14 +29,10 @@ async def source_entity_removed() -> None:
3329
# The source entity has been removed, we need to clean the device links.
3430
async_remove_stale_devices_links_keep_entity_device(hass, entry.entry_id, None)
3531

36-
entity_registry = er.async_get(hass)
3732
entry.async_on_unload(
3833
async_handle_source_entity_changes(
3934
hass,
4035
helper_config_entry_id=entry.entry_id,
41-
get_helper_entity_id=lambda: entity_registry.async_get_entity_id(
42-
SENSOR_DOMAIN, DOMAIN, entry.entry_id
43-
),
4436
set_source_entity_id_or_uuid=set_source_entity_id_or_uuid,
4537
source_device_id=async_entity_id_to_device_id(
4638
hass, entry.options[CONF_SOURCE]

homeassistant/components/switch_as_x/__init__.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
from homeassistant.helpers import device_registry as dr, entity_registry as er
1414
from homeassistant.helpers.helper_integration import async_handle_source_entity_changes
1515

16-
from .const import CONF_INVERT, CONF_TARGET_DOMAIN, DOMAIN
17-
from .light import LightSwitch
18-
19-
__all__ = ["LightSwitch"]
16+
from .const import CONF_INVERT, CONF_TARGET_DOMAIN
2017

2118
_LOGGER = logging.getLogger(__name__)
2219

@@ -72,9 +69,6 @@ async def source_entity_removed() -> None:
7269
async_handle_source_entity_changes(
7370
hass,
7471
helper_config_entry_id=entry.entry_id,
75-
get_helper_entity_id=lambda: entity_registry.async_get_entity_id(
76-
entry.options[CONF_TARGET_DOMAIN], DOMAIN, entry.entry_id
77-
),
7872
set_source_entity_id_or_uuid=set_source_entity_id_or_uuid,
7973
source_device_id=async_add_to_device(hass, entry, entity_id),
8074
source_entity_id_or_uuid=entry.options[CONF_ENTITY_ID],

homeassistant/helpers/helper_integration.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def async_handle_source_entity_changes(
1515
hass: HomeAssistant,
1616
*,
1717
helper_config_entry_id: str,
18-
get_helper_entity_id: Callable[[], str | None],
1918
set_source_entity_id_or_uuid: Callable[[str], None],
2019
source_device_id: str | None,
2120
source_entity_id_or_uuid: str,
@@ -37,8 +36,6 @@ def async_handle_source_entity_changes(
3736
to no device, and the helper config entry removed from the old device. Then
3837
the helper config entry is reloaded.
3938
40-
:param get_helper_entity: A function which returns the helper entity's entity ID,
41-
or None if the helper entity does not exist.
4239
:param set_source_entity_id_or_uuid: A function which updates the source entity
4340
ID or UUID, e.g., in the helper config entry options.
4441
:param source_entity_removed: A function which is called when the source entity
@@ -81,13 +78,14 @@ async def async_registry_updated(
8178
return
8279

8380
# The source entity has been moved to a different device, update the helper
84-
# helper entity to link to the new device and the helper device to include
85-
# the helper config entry
86-
helper_entity_id = get_helper_entity_id()
87-
if helper_entity_id:
81+
# entities to link to the new device and the helper device to include the
82+
# helper config entry
83+
for helper_entity in entity_registry.entities.get_entries_for_config_entry_id(
84+
helper_config_entry_id
85+
):
8886
# Update the helper entity to link to the new device (or no device)
8987
entity_registry.async_update_entity(
90-
helper_entity_id, device_id=source_entity_entry.device_id
88+
helper_entity.entity_id, device_id=source_entity_entry.device_id
9189
)
9290

9391
if source_entity_entry.device_id is not None:

tests/helpers/test_helper_integration.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,11 @@ def mock_helper_integration(
156156
) -> None:
157157
"""Mock the helper integration."""
158158

159-
def get_helper_entity_id() -> str | None:
160-
"""Get the helper entity ID."""
161-
return entity_registry.async_get_entity_id(
162-
"sensor", HELPER_DOMAIN, helper_config_entry.entry_id
163-
)
164-
165159
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
166160
"""Mock setup entry."""
167161
async_handle_source_entity_changes(
168162
hass,
169163
helper_config_entry_id=helper_config_entry.entry_id,
170-
get_helper_entity_id=get_helper_entity_id,
171164
set_source_entity_id_or_uuid=set_source_entity_id_or_uuid,
172165
source_device_id=source_entity_entry.device_id,
173166
source_entity_id_or_uuid=helper_config_entry.options["source"],

0 commit comments

Comments
 (0)