1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00

Get the registry using the callback method (#58542)

This commit is contained in:
Paulus Schoutsen 2021-10-27 15:58:14 -07:00 committed by GitHub
parent b34eb53914
commit e1e864d2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 84 additions and 73 deletions

View File

@ -35,7 +35,7 @@ async def async_setup_entry(hass, config_entry):
hass.data[AGENT_DOMAIN][config_entry.entry_id] = {CONNECTION: agent_client} hass.data[AGENT_DOMAIN][config_entry.entry_id] = {CONNECTION: agent_client}
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,

View File

@ -317,7 +317,7 @@ class AppleTVManager:
self._dispatch_send(SIGNAL_CONNECTED, self.atv) self._dispatch_send(SIGNAL_CONNECTED, self.atv)
self._address_updated(str(conf.address)) self._address_updated(str(conf.address))
await self._async_setup_device_registry() self._async_setup_device_registry()
self._connection_attempts = 0 self._connection_attempts = 0
if self._connection_was_lost: if self._connection_was_lost:
@ -327,7 +327,8 @@ class AppleTVManager:
) )
self._connection_was_lost = False self._connection_was_lost = False
async def _async_setup_device_registry(self): @callback
def _async_setup_device_registry(self):
attrs = { attrs = {
ATTR_IDENTIFIERS: {(DOMAIN, self.config_entry.unique_id)}, ATTR_IDENTIFIERS: {(DOMAIN, self.config_entry.unique_id)},
ATTR_MANUFACTURER: "Apple", ATTR_MANUFACTURER: "Apple",
@ -351,7 +352,7 @@ class AppleTVManager:
if dev_info.mac: if dev_info.mac:
attrs[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, dev_info.mac)} attrs[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, dev_info.mac)}
device_registry = await dr.async_get_registry(self.hass) device_registry = dr.async_get(self.hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id, **attrs config_entry_id=self.config_entry.entry_id, **attrs
) )

View File

@ -69,7 +69,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
assert hub.bond_id is not None assert hub.bond_id is not None
hub_name = hub.name or hub.bond_id hub_name = hub.name or hub.bond_id
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry_id, config_entry_id=config_entry_id,
identifiers={(DOMAIN, hub.bond_id)}, identifiers={(DOMAIN, hub.bond_id)},

View File

@ -62,7 +62,7 @@ class BroadlinkDevice:
Triggered when the device is renamed on the frontend. Triggered when the device is renamed on the frontend.
""" """
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_entry = device_registry.async_get_device({(DOMAIN, entry.unique_id)}) device_entry = device_registry.async_get_device({(DOMAIN, entry.unique_id)})
device_registry.async_update_device(device_entry.id, name=entry.title) device_registry.async_update_device(device_entry.id, name=entry.title)
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)

View File

@ -86,7 +86,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_, model, mac_address = controller_unique_id.split("_", 3) _, model, mac_address = controller_unique_id.split("_", 3)
entry_data[CONF_DIRECTOR_MODEL] = model.upper() entry_data[CONF_DIRECTOR_MODEL] = model.upper()
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, controller_unique_id)}, identifiers={(DOMAIN, controller_unique_id)},

View File

@ -72,7 +72,7 @@ async def async_unload_entry(
hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]() hass.data[DOMAIN][config_entry.entry_id][UNDO_UPDATE_LISTENER]()
# Remove zone2 and zone3 entities if needed # Remove zone2 and zone3 entities if needed
entity_registry = await er.async_get_registry(hass) entity_registry = er.async_get(hass)
entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id) entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
unique_id = config_entry.unique_id or config_entry.entry_id unique_id = config_entry.unique_id or config_entry.entry_id
zone2_id = f"{unique_id}-Zone2" zone2_id = f"{unique_id}-Zone2"

View File

@ -267,7 +267,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
assert cli.api_version is not None assert cli.api_version is not None
entry_data.api_version = cli.api_version entry_data.api_version = cli.api_version
entry_data.available = True entry_data.available = True
device_id = await _async_setup_device_registry( device_id = _async_setup_device_registry(
hass, entry, entry_data.device_info hass, entry, entry_data.device_info
) )
entry_data.async_update_device_state(hass) entry_data.async_update_device_state(hass)
@ -320,14 +320,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True return True
async def _async_setup_device_registry( @callback
def _async_setup_device_registry(
hass: HomeAssistant, entry: ConfigEntry, device_info: EsphomeDeviceInfo hass: HomeAssistant, entry: ConfigEntry, device_info: EsphomeDeviceInfo
) -> str: ) -> str:
"""Set up device registry feature for a particular config entry.""" """Set up device registry feature for a particular config entry."""
sw_version = device_info.esphome_version sw_version = device_info.esphome_version
if device_info.compilation_time: if device_info.compilation_time:
sw_version += f" ({device_info.compilation_time})" sw_version += f" ({device_info.compilation_time})"
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_entry = device_registry.async_get_or_create( device_entry = device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, device_info.mac_address)}, connections={(dr.CONNECTION_NETWORK_MAC, device_info.mac_address)},

View File

@ -189,7 +189,7 @@ async def async_setup_entry(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, handle_shutdown) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, handle_shutdown)
) )
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={}, connections={},

View File

@ -4,7 +4,7 @@ import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, EVENT_HOMEASSISTANT_STOP from homeassistant.const import CONF_NAME, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_registry import async_entries_for_config_entry from homeassistant.helpers.entity_registry import async_entries_for_config_entry
@ -85,7 +85,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return False return False
await async_setup_services(hass) await async_setup_services(hass)
await async_remove_obsolete_entities(hass, entry, hap) _async_remove_obsolete_entities(hass, entry, hap)
# Register on HA stop event to gracefully shutdown HomematicIP Cloud connection # Register on HA stop event to gracefully shutdown HomematicIP Cloud connection
hap.reset_connection_listener = hass.bus.async_listen_once( hap.reset_connection_listener = hass.bus.async_listen_once(
@ -93,7 +93,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) )
# Register hap as device in registry. # Register hap as device in registry.
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
home = hap.home home = hap.home
hapname = home.label if home.label != entry.unique_id else f"Home-{home.label}" hapname = home.label if home.label != entry.unique_id else f"Home-{home.label}"
@ -118,7 +118,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return await hap.async_reset() return await hap.async_reset()
async def async_remove_obsolete_entities( @callback
def _async_remove_obsolete_entities(
hass: HomeAssistant, entry: ConfigEntry, hap: HomematicipHAP hass: HomeAssistant, entry: ConfigEntry, hap: HomematicipHAP
): ):
"""Remove obsolete entities from entity registry.""" """Remove obsolete entities from entity registry."""
@ -126,7 +127,7 @@ async def async_remove_obsolete_entities(
if hap.home.currentAPVersion < "2.2.12": if hap.home.currentAPVersion < "2.2.12":
return return
entity_registry = await er.async_get_registry(hass) entity_registry = er.async_get(hass)
er_entries = async_entries_for_config_entry(entity_registry, entry.entry_id) er_entries = async_entries_for_config_entry(entity_registry, entry.entry_id)
for er_entry in er_entries: for er_entry in er_entries:
if er_entry.unique_id.startswith("HomematicipAccesspointStatus"): if er_entry.unique_id.startswith("HomematicipAccesspointStatus"):

View File

@ -139,13 +139,13 @@ class HomematicipGenericEntity(Entity):
if self.hmip_device_removed: if self.hmip_device_removed:
try: try:
del self._hap.hmip_device_by_entity_id[self.entity_id] del self._hap.hmip_device_by_entity_id[self.entity_id]
await self.async_remove_from_registries() self.async_remove_from_registries()
except KeyError as err: except KeyError as err:
_LOGGER.debug("Error removing HMIP device from registry: %s", err) _LOGGER.debug("Error removing HMIP device from registry: %s", err)
async def async_remove_from_registries(self) -> None: @callback
def async_remove_from_registries(self) -> None:
"""Remove entity/device from registry.""" """Remove entity/device from registry."""
# Remove callback from device. # Remove callback from device.
self._device.remove_callback(self._async_device_changed) self._device.remove_callback(self._async_device_changed)
self._device.remove_callback(self._async_device_removed) self._device.remove_callback(self._async_device_removed)
@ -155,7 +155,7 @@ class HomematicipGenericEntity(Entity):
if device_id := self.registry_entry.device_id: if device_id := self.registry_entry.device_id:
# Remove from device registry. # Remove from device registry.
device_registry = await dr.async_get_registry(self.hass) device_registry = dr.async_get(self.hass)
if device_id in device_registry.devices: if device_id in device_registry.devices:
# This will also remove associated entities from entity registry. # This will also remove associated entities from entity registry.
device_registry.async_remove_device(device_id) device_registry.async_remove_device(device_id)
@ -163,7 +163,7 @@ class HomematicipGenericEntity(Entity):
# Remove from entity registry. # Remove from entity registry.
# Only relevant for entities that do not belong to a device. # Only relevant for entities that do not belong to a device.
if entity_id := self.registry_entry.entity_id: if entity_id := self.registry_entry.entity_id:
entity_registry = await er.async_get_registry(self.hass) entity_registry = er.async_get(self.hass)
if entity_id in entity_registry.entities: if entity_id in entity_registry.entities:
entity_registry.async_remove(entity_id) entity_registry.async_remove(entity_id)

View File

@ -447,7 +447,7 @@ async def async_setup_entry( # noqa: C901
) )
if sw_version: if sw_version:
device_info[ATTR_SW_VERSION] = sw_version device_info[ATTR_SW_VERSION] = sw_version
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
**device_info, **device_info,

View File

@ -100,7 +100,7 @@ async def async_setup_entry(
hass.async_create_task(hass.config_entries.async_remove(entry.entry_id)) hass.async_create_task(hass.config_entries.async_remove(entry.entry_id))
return False return False
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, config.mac)}, connections={(dr.CONNECTION_NETWORK_MAC, config.mac)},

View File

@ -200,7 +200,7 @@ async def async_setup_entry(
_LOGGER.info(repr(isy.clock)) _LOGGER.info(repr(isy.clock))
hass_isy_data[ISY994_ISY] = isy hass_isy_data[ISY994_ISY] = isy
await _async_get_or_create_isy_device_in_registry(hass, entry, isy) _async_get_or_create_isy_device_in_registry(hass, entry, isy)
# Load platforms for the devices in the ISY controller that we support. # Load platforms for the devices in the ISY controller that we support.
hass.config_entries.async_setup_platforms(entry, PLATFORMS) hass.config_entries.async_setup_platforms(entry, PLATFORMS)
@ -259,10 +259,11 @@ def _async_isy_to_configuration_url(isy: ISY) -> str:
return f"{proto}://{connection_info['addr']}:{connection_info['port']}" return f"{proto}://{connection_info['addr']}:{connection_info['port']}"
async def _async_get_or_create_isy_device_in_registry( @callback
def _async_get_or_create_isy_device_in_registry(
hass: HomeAssistant, entry: config_entries.ConfigEntry, isy hass: HomeAssistant, entry: config_entries.ConfigEntry, isy
) -> None: ) -> None:
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
url = _async_isy_to_configuration_url(isy) url = _async_isy_to_configuration_url(isy)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,

View File

@ -274,9 +274,10 @@ def async_setup_services(hass: HomeAssistant): # noqa: C901
return return
_LOGGER.error("Could not set variable value; not found or enabled on the ISY") _LOGGER.error("Could not set variable value; not found or enabled on the ISY")
async def async_cleanup_registry_entries(service) -> None: @callback
def async_cleanup_registry_entries(service) -> None:
"""Remove extra entities that are no longer part of the integration.""" """Remove extra entities that are no longer part of the integration."""
entity_registry = await er.async_get_registry(hass) entity_registry = er.async_get(hass)
config_ids = [] config_ids = []
current_unique_ids = [] current_unique_ids = []

View File

@ -151,7 +151,7 @@ class AlarmPanel:
self.port, self.port,
) )
device_registry = await dr.async_get_registry(self.hass) device_registry = dr.async_get(self.hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id, config_entry_id=self.config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, self.status.get("mac"))}, connections={(dr.CONNECTION_NETWORK_MAC, self.status.get("mac"))},

View File

@ -123,7 +123,7 @@ async def async_setup_entry(hass, config_entry):
devices = bridge.get_devices() devices = bridge.get_devices()
bridge_device = devices[BRIDGE_DEVICE_ID] bridge_device = devices[BRIDGE_DEVICE_ID]
await _async_register_bridge_device(hass, config_entry.entry_id, bridge_device) _async_register_bridge_device(hass, config_entry.entry_id, bridge_device)
# Store this bridge (keyed by entry_id) so it can be retrieved by the # Store this bridge (keyed by entry_id) so it can be retrieved by the
# platforms we're setting up. # platforms we're setting up.
hass.data[DOMAIN][config_entry.entry_id] = { hass.data[DOMAIN][config_entry.entry_id] = {
@ -164,7 +164,7 @@ async def async_setup_lip(hass, config_entry, lip_devices):
_LOGGER.debug("Connected to Lutron Caseta bridge via LIP at %s:23", host) _LOGGER.debug("Connected to Lutron Caseta bridge via LIP at %s:23", host)
button_devices_by_lip_id = _async_merge_lip_leap_data(lip_devices, bridge) button_devices_by_lip_id = _async_merge_lip_leap_data(lip_devices, bridge)
button_devices_by_dr_id = await _async_register_button_devices( button_devices_by_dr_id = _async_register_button_devices(
hass, config_entry_id, bridge_device, button_devices_by_lip_id hass, config_entry_id, bridge_device, button_devices_by_lip_id
) )
_async_subscribe_pico_remote_events(hass, lip, button_devices_by_lip_id) _async_subscribe_pico_remote_events(hass, lip, button_devices_by_lip_id)
@ -200,9 +200,10 @@ def _async_merge_lip_leap_data(lip_devices, bridge):
return button_devices_by_id return button_devices_by_id
async def _async_register_bridge_device(hass, config_entry_id, bridge_device): @callback
def _async_register_bridge_device(hass, config_entry_id, bridge_device):
"""Register the bridge device in the device registry.""" """Register the bridge device in the device registry."""
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
name=bridge_device["name"], name=bridge_device["name"],
manufacturer=MANUFACTURER, manufacturer=MANUFACTURER,
@ -212,11 +213,12 @@ async def _async_register_bridge_device(hass, config_entry_id, bridge_device):
) )
async def _async_register_button_devices( @callback
def _async_register_button_devices(
hass, config_entry_id, bridge_device, button_devices_by_id hass, config_entry_id, bridge_device, button_devices_by_id
): ):
"""Register button devices (Pico Remotes) in the device registry.""" """Register button devices (Pico Remotes) in the device registry."""
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
button_devices_by_dr_id = {} button_devices_by_dr_id = {}
for device in button_devices_by_id.values(): for device in button_devices_by_id.values():

View File

@ -77,7 +77,7 @@ async def async_setup_entry(hass, entry):
hass.data[DOMAIN][DATA_CONFIG_ENTRIES][webhook_id] = entry hass.data[DOMAIN][DATA_CONFIG_ENTRIES][webhook_id] = entry
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device = device_registry.async_get_or_create( device = device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,

View File

@ -27,7 +27,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
webhook_id = config_entry.data[CONF_WEBHOOK_ID] webhook_id = config_entry.data[CONF_WEBHOOK_ID]
entity_registry = await er.async_get_registry(hass) entity_registry = er.async_get(hass)
entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id) entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
for entry in entries: for entry in entries:
if entry.domain != ENTITY_TYPE or entry.disabled_by: if entry.domain != ENTITY_TYPE or entry.disabled_by:

View File

@ -35,7 +35,7 @@ class MobileAppFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
user_input[ATTR_DEVICE_ID] = str(uuid.uuid4()).replace("-", "") user_input[ATTR_DEVICE_ID] = str(uuid.uuid4()).replace("-", "")
# Register device tracker entity and add to person registering app # Register device tracker entity and add to person registering app
entity_registry = await er.async_get_registry(self.hass) entity_registry = er.async_get(self.hass)
devt_entry = entity_registry.async_get_or_create( devt_entry = entity_registry.async_get_or_create(
"device_tracker", "device_tracker",
DOMAIN, DOMAIN,

View File

@ -31,7 +31,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
webhook_id = config_entry.data[CONF_WEBHOOK_ID] webhook_id = config_entry.data[CONF_WEBHOOK_ID]
entity_registry = await er.async_get_registry(hass) entity_registry = er.async_get(hass)
entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id) entries = er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
for entry in entries: for entry in entries:
if entry.domain != ENTITY_TYPE or entry.disabled_by: if entry.domain != ENTITY_TYPE or entry.disabled_by:

View File

@ -344,7 +344,7 @@ async def webhook_update_registration(hass, config_entry, data):
"""Handle an update registration webhook.""" """Handle an update registration webhook."""
new_registration = {**config_entry.data, **data} new_registration = {**config_entry.data, **data}
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
@ -433,7 +433,7 @@ async def webhook_register_sensor(hass, config_entry, data):
device_name = config_entry.data[ATTR_DEVICE_NAME] device_name = config_entry.data[ATTR_DEVICE_NAME]
unique_store_key = f"{config_entry.data[CONF_WEBHOOK_ID]}_{unique_id}" unique_store_key = f"{config_entry.data[CONF_WEBHOOK_ID]}_{unique_id}"
entity_registry = await er.async_get_registry(hass) entity_registry = er.async_get(hass)
existing_sensor = entity_registry.async_get_entity_id( existing_sensor = entity_registry.async_get_entity_id(
entity_type, DOMAIN, unique_store_key entity_type, DOMAIN, unique_store_key
) )
@ -498,7 +498,7 @@ async def webhook_update_sensor_states(hass, config_entry, data):
unique_store_key = f"{config_entry.data[CONF_WEBHOOK_ID]}_{unique_id}" unique_store_key = f"{config_entry.data[CONF_WEBHOOK_ID]}_{unique_id}"
entity_registry = await er.async_get_registry(hass) entity_registry = er.async_get(hass)
if not entity_registry.async_get_entity_id( if not entity_registry.async_get_entity_id(
entity_type, DOMAIN, unique_store_key entity_type, DOMAIN, unique_store_key
): ):

View File

@ -157,7 +157,7 @@ async def async_setup_entry(
else: else:
version = f"Protocol: {motion_gateway.protocol}" version = f"Protocol: {motion_gateway.protocol}"
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, motion_gateway.mac)}, connections={(dr.CONNECTION_NETWORK_MAC, motion_gateway.mac)},

View File

@ -325,7 +325,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
} }
current_cameras: set[tuple[str, str]] = set() current_cameras: set[tuple[str, str]] = set()
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
@callback @callback
def _async_process_motioneye_cameras() -> None: def _async_process_motioneye_cameras() -> None:

View File

@ -23,7 +23,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
entry.async_on_unload(entry.add_update_listener(update_listener)) entry.async_on_unload(entry.add_update_listener(update_listener))
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, entry.unique_id)}, identifiers={(DOMAIN, entry.unique_id)},

View File

@ -32,7 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = api hass.data[DOMAIN][entry.entry_id] = api
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, server_url)}, identifiers={(DOMAIN, server_url)},

View File

@ -85,7 +85,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
for item in result: for item in result:
if attr == "bridges" and item["id"] not in data["bridges"]: if attr == "bridges" and item["id"] not in data["bridges"]:
# If a new bridge is discovered, register it: # If a new bridge is discovered, register it:
hass.async_create_task(async_register_new_bridge(hass, item, entry)) _async_register_new_bridge(hass, item, entry)
data[attr][item["id"]] = item data[attr][item["id"]] = item
return data return data
@ -115,11 +115,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok return unload_ok
async def async_register_new_bridge( @callback
def _async_register_new_bridge(
hass: HomeAssistant, bridge: dict, entry: ConfigEntry hass: HomeAssistant, bridge: dict, entry: ConfigEntry
) -> None: ) -> None:
"""Register a new bridge.""" """Register a new bridge."""
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, bridge["hardware_id"])}, identifiers={(DOMAIN, bridge["hardware_id"])},
@ -175,7 +176,8 @@ class NotionEntity(CoordinatorEntity):
and self._task_id in self.coordinator.data["tasks"] and self._task_id in self.coordinator.data["tasks"]
) )
async def _async_update_bridge_id(self) -> None: @callback
def _async_update_bridge_id(self) -> None:
"""Update the entity's bridge ID if it has changed. """Update the entity's bridge ID if it has changed.
Sensors can move to other bridges based on signal strength, etc. Sensors can move to other bridges based on signal strength, etc.
@ -193,7 +195,7 @@ class NotionEntity(CoordinatorEntity):
self._bridge_id = sensor["bridge"]["id"] self._bridge_id = sensor["bridge"]["id"]
device_registry = await dr.async_get_registry(self.hass) device_registry = dr.async_get(self.hass)
this_device = device_registry.async_get_device( this_device = device_registry.async_get_device(
{(DOMAIN, sensor["hardware_id"])} {(DOMAIN, sensor["hardware_id"])}
) )
@ -218,7 +220,7 @@ class NotionEntity(CoordinatorEntity):
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
"""Respond to a DataUpdateCoordinator update.""" """Respond to a DataUpdateCoordinator update."""
if self._task_id in self.coordinator.data["tasks"]: if self._task_id in self.coordinator.data["tasks"]:
self.hass.async_create_task(self._async_update_bridge_id()) self._async_update_bridge_id()
self._async_update_from_latest_data() self._async_update_from_latest_data()
self.async_write_ha_state() self.async_write_ha_state()

View File

@ -123,7 +123,7 @@ async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool:
UNDO_UPDATE_LISTENER: undo_listener, UNDO_UPDATE_LISTENER: undo_listener,
} }
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, api.gateway_id)}, identifiers={(DOMAIN, api.gateway_id)},

View File

@ -16,7 +16,7 @@ async def async_setup_entry(hass, entry):
return False return False
hass.data[DOMAIN][entry.entry_id] = roonserver hass.data[DOMAIN][entry.entry_id] = roonserver
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, entry.entry_id)}, identifiers={(DOMAIN, entry.entry_id)},

View File

@ -11,7 +11,7 @@ from .hub import SIAHub
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up sia from a config entry.""" """Set up sia from a config entry."""
hub: SIAHub = SIAHub(hass, entry) hub: SIAHub = SIAHub(hass, entry)
await hub.async_setup_hub() hub.async_setup_hub()
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = hub hass.data[DOMAIN][entry.entry_id] = hub

View File

@ -9,7 +9,7 @@ from pysiaalarm.aio import CommunicationsProtocol, SIAAccount, SIAClient, SIAEve
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PORT, CONF_PROTOCOL, EVENT_HOMEASSISTANT_STOP from homeassistant.const import CONF_PORT, CONF_PROTOCOL, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -50,10 +50,11 @@ class SIAHub:
self.sia_accounts: list[SIAAccount] | None = None self.sia_accounts: list[SIAAccount] | None = None
self.sia_client: SIAClient = None self.sia_client: SIAClient = None
async def async_setup_hub(self) -> None: @callback
def async_setup_hub(self) -> None:
"""Add a device to the device_registry, register shutdown listener, load reactions.""" """Add a device to the device_registry, register shutdown listener, load reactions."""
self.update_accounts() self.update_accounts()
device_registry = await dr.async_get_registry(self._hass) device_registry = dr.async_get(self._hass)
for acc in self._accounts: for acc in self._accounts:
account = acc[CONF_ACCOUNT] account = acc[CONF_ACCOUNT]
device_registry.async_get_or_create( device_registry.async_get_or_create(

View File

@ -201,11 +201,12 @@ def _async_standardize_config_entry(hass: HomeAssistant, entry: ConfigEntry) ->
hass.config_entries.async_update_entry(entry, **entry_updates) hass.config_entries.async_update_entry(entry, **entry_updates)
async def async_register_base_station( @callback
def _async_register_base_station(
hass: HomeAssistant, entry: ConfigEntry, system: SystemV2 | SystemV3 hass: HomeAssistant, entry: ConfigEntry, system: SystemV2 | SystemV3
) -> None: ) -> None:
"""Register a new bridge.""" """Register a new bridge."""
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, system.system_id)}, identifiers={(DOMAIN, system.system_id)},
@ -473,9 +474,7 @@ class SimpliSafe:
for system in self.systems.values(): for system in self.systems.values():
self._system_notifications[system.system_id] = set() self._system_notifications[system.system_id] = set()
self._hass.async_create_task( _async_register_base_station(self._hass, self.entry, system)
async_register_base_station(self._hass, self.entry, system)
)
# Future events will come from the websocket, but since subscription to the # Future events will come from the websocket, but since subscription to the
# websocket doesn't provide the most recent event, we grab it from the REST # websocket doesn't provide the most recent event, we grab it from the REST

View File

@ -10,6 +10,7 @@ from smarttub import APIError, LoginFailed, SmartTub
from smarttub.api import Account from smarttub.api import Account
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -75,7 +76,7 @@ class SmartTubController:
await self.coordinator.async_refresh() await self.coordinator.async_refresh()
await self.async_register_devices(entry) self.async_register_devices(entry)
return True return True
@ -107,9 +108,10 @@ class SmartTubController:
ATTR_ERRORS: errors, ATTR_ERRORS: errors,
} }
async def async_register_devices(self, entry): @callback
def async_register_devices(self, entry):
"""Register devices with the device registry for all spas.""" """Register devices with the device registry for all spas."""
device_registry = await dr.async_get_registry(self._hass) device_registry = dr.async_get(self._hass)
for spa in self.spas: for spa in self.spas:
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,

View File

@ -97,7 +97,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) )
coordinator.update_interval = SCAN_INTERVAL_ALL_ASSUMED_STATE coordinator.update_interval = SCAN_INTERVAL_ALL_ASSUMED_STATE
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
hubs = [ hubs = [
device device

View File

@ -66,7 +66,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# and the config should simply be discarded # and the config should simply be discarded
return False return False
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
connections=device_connections(printer), connections=device_connections(printer),

View File

@ -102,7 +102,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id] = coordinator hass.data[DOMAIN][entry.entry_id] = coordinator
# Register device for the Meter Adapter, since it will have no entities. # Register device for the Meter Adapter, since it will have no entities.
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={ identifiers={

View File

@ -153,7 +153,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) )
# Create device registry entry. # Create device registry entry.
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
connections={(dr.CONNECTION_UPNP, device.udn)}, connections={(dr.CONNECTION_UPNP, device.udn)},

View File

@ -172,7 +172,7 @@ async def async_setup_entry(
entry.data[CONF_HOST], entry.data[CONF_HOST],
) )
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
identifiers={(DOMAIN, entry.unique_id)}, identifiers={(DOMAIN, entry.unique_id)},

View File

@ -377,7 +377,7 @@ async def async_setup_gateway_entry(
gateway_model = f"{gateway_info.model}-{gateway_info.hardware_version}" gateway_model = f"{gateway_info.model}-{gateway_info.hardware_version}"
device_registry = await dr.async_get_registry(hass) device_registry = dr.async_get(hass)
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=entry.entry_id, config_entry_id=entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, gateway_info.mac_address)}, connections={(dr.CONNECTION_NETWORK_MAC, gateway_info.mac_address)},