mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Exclude disabled entities from async_entries_for_device (#43665)
This commit is contained in:
parent
f9fa24950b
commit
a5e5c5c7e6
@ -207,5 +207,12 @@ async def async_remove_orphaned_entries_service(hass, data):
|
|||||||
|
|
||||||
# Remove devices that don't belong to any entity
|
# Remove devices that don't belong to any entity
|
||||||
for device_id in devices_to_be_removed:
|
for device_id in devices_to_be_removed:
|
||||||
if len(async_entries_for_device(entity_registry, device_id)) == 0:
|
if (
|
||||||
|
len(
|
||||||
|
async_entries_for_device(
|
||||||
|
entity_registry, device_id, include_disabled_entities=True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
== 0
|
||||||
|
):
|
||||||
device_registry.async_remove_device(device_id)
|
device_registry.async_remove_device(device_id)
|
||||||
|
@ -1248,7 +1248,7 @@ async def cleanup_device_registry(hass, device_id):
|
|||||||
if (
|
if (
|
||||||
device_id
|
device_id
|
||||||
and not hass.helpers.entity_registry.async_entries_for_device(
|
and not hass.helpers.entity_registry.async_entries_for_device(
|
||||||
entity_registry, device_id
|
entity_registry, device_id, include_disabled_entities=True
|
||||||
)
|
)
|
||||||
and not await device_trigger.async_get_triggers(hass, device_id)
|
and not await device_trigger.async_get_triggers(hass, device_id)
|
||||||
and not tag.async_has_tags(hass, device_id)
|
and not tag.async_has_tags(hass, device_id)
|
||||||
|
@ -123,7 +123,7 @@ async def info_for_device(hass, device_id):
|
|||||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||||
|
|
||||||
entries = hass.helpers.entity_registry.async_entries_for_device(
|
entries = hass.helpers.entity_registry.async_entries_for_device(
|
||||||
entity_registry, device_id
|
entity_registry, device_id, include_disabled_entities=True
|
||||||
)
|
)
|
||||||
mqtt_debug_info = hass.data.setdefault(
|
mqtt_debug_info = hass.data.setdefault(
|
||||||
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}}
|
||||||
|
@ -144,7 +144,9 @@ async def async_start(
|
|||||||
|
|
||||||
orphaned_entities = {
|
orphaned_entities = {
|
||||||
entry.unique_id
|
entry.unique_id
|
||||||
for entry in async_entries_for_device(entity_registry, device.id)
|
for entry in async_entries_for_device(
|
||||||
|
entity_registry, device.id, include_disabled_entities=True
|
||||||
|
)
|
||||||
if entry.domain == sensor.DOMAIN and entry.platform == DOMAIN
|
if entry.domain == sensor.DOMAIN and entry.platform == DOMAIN
|
||||||
}
|
}
|
||||||
for (tasmota_sensor_config, discovery_hash) in sensors:
|
for (tasmota_sensor_config, discovery_hash) in sensors:
|
||||||
|
@ -269,7 +269,7 @@ async def cleanup_device_registry(hass: HomeAssistant, device_id):
|
|||||||
device_registry = await hass.helpers.device_registry.async_get_registry()
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
||||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||||
if device_id and not hass.helpers.entity_registry.async_entries_for_device(
|
if device_id and not hass.helpers.entity_registry.async_entries_for_device(
|
||||||
entity_registry, device_id
|
entity_registry, device_id, include_disabled_entities=True
|
||||||
):
|
):
|
||||||
device_registry.async_remove_device(device_id)
|
device_registry.async_remove_device(device_id)
|
||||||
|
|
||||||
|
@ -95,7 +95,16 @@ class UniFiBase(Entity):
|
|||||||
entity_registry.async_remove(self.entity_id)
|
entity_registry.async_remove(self.entity_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
if len(async_entries_for_device(entity_registry, entity_entry.device_id)) == 1:
|
if (
|
||||||
|
len(
|
||||||
|
async_entries_for_device(
|
||||||
|
entity_registry,
|
||||||
|
entity_entry.device_id,
|
||||||
|
include_disabled_entities=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
== 1
|
||||||
|
):
|
||||||
device_registry.async_remove_device(device_entry.id)
|
device_registry.async_remove_device(device_entry.id)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -243,7 +243,9 @@ class GroupProbe:
|
|||||||
if member.device.is_coordinator:
|
if member.device.is_coordinator:
|
||||||
continue
|
continue
|
||||||
entities = async_entries_for_device(
|
entities = async_entries_for_device(
|
||||||
zha_gateway.ha_entity_registry, member.device.device_id
|
zha_gateway.ha_entity_registry,
|
||||||
|
member.device.device_id,
|
||||||
|
include_disabled_entities=True,
|
||||||
)
|
)
|
||||||
all_domain_occurrences.extend(
|
all_domain_occurrences.extend(
|
||||||
[
|
[
|
||||||
|
@ -400,7 +400,9 @@ class ZHAGateway:
|
|||||||
|
|
||||||
# then we get all group entity entries tied to the coordinator
|
# then we get all group entity entries tied to the coordinator
|
||||||
all_group_entity_entries = async_entries_for_device(
|
all_group_entity_entries = async_entries_for_device(
|
||||||
self.ha_entity_registry, self.coordinator_zha_device.device_id
|
self.ha_entity_registry,
|
||||||
|
self.coordinator_zha_device.device_id,
|
||||||
|
include_disabled_entities=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# then we get the entity entries for this specific group by getting the entries that match
|
# then we get the entity entries for this specific group by getting the entries that match
|
||||||
|
@ -195,7 +195,9 @@ class ZHAGroup(LogMixin):
|
|||||||
domain_entity_ids: List[str] = []
|
domain_entity_ids: List[str] = []
|
||||||
for member in self.members:
|
for member in self.members:
|
||||||
entities = async_entries_for_device(
|
entities = async_entries_for_device(
|
||||||
self._zha_gateway.ha_entity_registry, member.device.device_id
|
self._zha_gateway.ha_entity_registry,
|
||||||
|
member.device.device_id,
|
||||||
|
include_disabled_entities=True,
|
||||||
)
|
)
|
||||||
domain_entity_ids.extend(
|
domain_entity_ids.extend(
|
||||||
[entity.entity_id for entity in entities if entity.domain == domain]
|
[entity.entity_id for entity in entities if entity.domain == domain]
|
||||||
|
@ -298,7 +298,9 @@ class EntityRegistry:
|
|||||||
the device is disabled.
|
the device is disabled.
|
||||||
"""
|
"""
|
||||||
if event.data["action"] == "remove":
|
if event.data["action"] == "remove":
|
||||||
entities = async_entries_for_device(self, event.data["device_id"])
|
entities = async_entries_for_device(
|
||||||
|
self, event.data["device_id"], include_disabled_entities=True
|
||||||
|
)
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
self.async_remove(entity.entity_id)
|
self.async_remove(entity.entity_id)
|
||||||
return
|
return
|
||||||
@ -311,7 +313,9 @@ class EntityRegistry:
|
|||||||
if not device.disabled:
|
if not device.disabled:
|
||||||
return
|
return
|
||||||
|
|
||||||
entities = async_entries_for_device(self, event.data["device_id"])
|
entities = async_entries_for_device(
|
||||||
|
self, event.data["device_id"], include_disabled_entities=True
|
||||||
|
)
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
self.async_update_entity( # type: ignore
|
self.async_update_entity( # type: ignore
|
||||||
entity.entity_id, disabled_by=DISABLED_DEVICE
|
entity.entity_id, disabled_by=DISABLED_DEVICE
|
||||||
@ -548,11 +552,14 @@ async def async_get_registry(hass: HomeAssistantType) -> EntityRegistry:
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_entries_for_device(
|
def async_entries_for_device(
|
||||||
registry: EntityRegistry, device_id: str
|
registry: EntityRegistry, device_id: str, include_disabled_entities: bool = False
|
||||||
) -> List[RegistryEntry]:
|
) -> List[RegistryEntry]:
|
||||||
"""Return entries that match a device."""
|
"""Return entries that match a device."""
|
||||||
return [
|
return [
|
||||||
entry for entry in registry.entities.values() if entry.device_id == device_id
|
entry
|
||||||
|
for entry in registry.entities.values()
|
||||||
|
if entry.device_id == device_id
|
||||||
|
and (not entry.disabled_by or include_disabled_entities)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -260,7 +260,7 @@ async def async_extract_entity_ids(
|
|||||||
entry.entity_id
|
entry.entity_id
|
||||||
for device in devices
|
for device in devices
|
||||||
for entry in hass.helpers.entity_registry.async_entries_for_device(
|
for entry in hass.helpers.entity_registry.async_entries_for_device(
|
||||||
ent_reg, device.id
|
ent_reg, device.id, include_disabled_entities=True
|
||||||
)
|
)
|
||||||
if not entry.area_id
|
if not entry.area_id
|
||||||
)
|
)
|
||||||
|
@ -736,3 +736,39 @@ async def test_disable_device_disables_entities(hass, registry):
|
|||||||
entry = registry.async_get(entry.entity_id)
|
entry = registry.async_get(entry.entity_id)
|
||||||
assert entry.disabled
|
assert entry.disabled
|
||||||
assert entry.disabled_by == "device"
|
assert entry.disabled_by == "device"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_disabled_entities_excluded_from_entity_list(hass, registry):
|
||||||
|
"""Test that disabled entities are exclduded from async_entries_for_device."""
|
||||||
|
device_registry = mock_device_registry(hass)
|
||||||
|
config_entry = MockConfigEntry(domain="light")
|
||||||
|
|
||||||
|
device_entry = device_registry.async_get_or_create(
|
||||||
|
config_entry_id=config_entry.entry_id,
|
||||||
|
connections={("mac", "12:34:56:AB:CD:EF")},
|
||||||
|
)
|
||||||
|
|
||||||
|
entry1 = registry.async_get_or_create(
|
||||||
|
"light",
|
||||||
|
"hue",
|
||||||
|
"5678",
|
||||||
|
config_entry=config_entry,
|
||||||
|
device_id=device_entry.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
entry2 = registry.async_get_or_create(
|
||||||
|
"light",
|
||||||
|
"hue",
|
||||||
|
"ABCD",
|
||||||
|
config_entry=config_entry,
|
||||||
|
device_id=device_entry.id,
|
||||||
|
disabled_by="user",
|
||||||
|
)
|
||||||
|
|
||||||
|
entries = entity_registry.async_entries_for_device(registry, device_entry.id)
|
||||||
|
assert entries == [entry1]
|
||||||
|
|
||||||
|
entries = entity_registry.async_entries_for_device(
|
||||||
|
registry, device_entry.id, include_disabled_entities=True
|
||||||
|
)
|
||||||
|
assert entries == [entry1, entry2]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user