mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
AirNow sensors should share device identifier (#103279)
* Entities from the same config entry should have the same device identifier * Clean up unused device entries with no entities after sensor setup Co-authored-by: James Pan <32176676+jzpan1@users.noreply.github.com> Co-authored-by: Theo Ma <62950302+t1an-xyz@users.noreply.github.com> Co-authored-by: Jonathan McDevitt <69861492+Jonmcd1@users.noreply.github.com> Co-authored-by: Jadon Yack <86989502+jadonyack@users.noreply.github.com> Co-authored-by: Ashton Foley <121987068+foleyash@users.noreply.github.com> Co-authored-by: Leon Yan <138124222+leony7@users.noreply.github.com>
This commit is contained in:
parent
39c97f5b14
commit
37757f777f
@ -11,6 +11,7 @@ from homeassistant.const import (
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN
|
||||
@ -50,6 +51,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
# Clean up unused device entries with no entities
|
||||
device_registry = dr.async_get(hass)
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
device_entries = dr.async_entries_for_config_entry(
|
||||
device_registry, config_entry_id=entry.entry_id
|
||||
)
|
||||
for dev in device_entries:
|
||||
dev_entities = er.async_entries_for_device(
|
||||
entity_registry, dev.id, include_disabled_entities=True
|
||||
)
|
||||
if not dev_entities:
|
||||
device_registry.async_remove_device(dev.id)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
@ -148,13 +148,14 @@ class AirNowSensor(CoordinatorEntity[AirNowDataUpdateCoordinator], SensorEntity)
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
_device_id = f"{coordinator.latitude}-{coordinator.longitude}"
|
||||
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.latitude}-{coordinator.longitude}-{description.key.lower()}"
|
||||
)
|
||||
self._attr_unique_id = f"{_device_id}-{description.key.lower()}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
identifiers={(DOMAIN, self._attr_unique_id)},
|
||||
identifiers={(DOMAIN, _device_id)},
|
||||
manufacturer=DEFAULT_NAME,
|
||||
name=DEFAULT_NAME,
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user