Adjust entity registry access in integrations (3) (#88948)

This commit is contained in:
epenet 2023-03-01 08:24:56 +01:00 committed by GitHub
parent f69d76702a
commit 42b74e7f56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 90 additions and 93 deletions

View file

@ -18,8 +18,11 @@ from homeassistant.components import zeroconf
from homeassistant.const import CONF_API_KEY, CONF_HOST
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import aiohttp_client, device_registry
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import (
aiohttp_client,
config_validation as cv,
device_registry as dr,
)
from homeassistant.util.network import is_ipv6_address
from .const import (
@ -306,10 +309,8 @@ class HueV2OptionsFlowHandler(config_entries.OptionsFlow):
# create a list of Hue device ID's that the user can select
# to ignore availability status
dev_reg = device_registry.async_get(self.hass)
entries = device_registry.async_entries_for_config_entry(
dev_reg, self.config_entry.entry_id
)
dev_reg = dr.async_get(self.hass)
entries = dr.async_entries_for_config_entry(dev_reg, self.config_entry.entry_id)
dev_ids = {
identifier[1]: entry.name
for entry in entries

View file

@ -16,7 +16,7 @@ from homeassistant.const import (
ATTR_VIA_DEVICE,
)
from homeassistant.core import callback
from homeassistant.helpers import device_registry
from homeassistant.helpers import device_registry as dr
from ..const import DOMAIN
@ -29,11 +29,11 @@ async def async_setup_devices(bridge: "HueBridge"):
entry = bridge.config_entry
hass = bridge.hass
api: HueBridgeV2 = bridge.api # to satisfy typing
dev_reg = device_registry.async_get(hass)
dev_reg = dr.async_get(hass)
dev_controller = api.devices
@callback
def add_device(hue_device: Device) -> device_registry.DeviceEntry:
def add_device(hue_device: Device) -> dr.DeviceEntry:
"""Register a Hue device in device registry."""
model = f"{hue_device.product_data.product_name} ({hue_device.product_data.model_id})"
params = {
@ -51,9 +51,7 @@ async def async_setup_devices(bridge: "HueBridge"):
params[ATTR_VIA_DEVICE] = (DOMAIN, api.config.bridge_device.id)
zigbee = dev_controller.get_zigbee_connectivity(hue_device.id)
if zigbee and zigbee.mac_address:
params[ATTR_CONNECTIONS] = {
(device_registry.CONNECTION_NETWORK_MAC, zigbee.mac_address)
}
params[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, zigbee.mac_address)}
return dev_reg.async_get_or_create(config_entry_id=entry.entry_id, **params)
@ -77,9 +75,7 @@ async def async_setup_devices(bridge: "HueBridge"):
known_devices = [add_device(hue_device) for hue_device in dev_controller]
# Check for nodes that no longer exist and remove them
for device in device_registry.async_entries_for_config_entry(
dev_reg, entry.entry_id
):
for device in dr.async_entries_for_config_entry(dev_reg, entry.entry_id):
if device not in known_devices:
# handle case where a virtual device was created for a Hue group
hue_dev_id = next(x[1] for x in device.identifiers if x[0] == DOMAIN)

View file

@ -9,7 +9,7 @@ from aiohue.v2.models.relative_rotary import RelativeRotary
from homeassistant.const import CONF_DEVICE_ID, CONF_ID, CONF_TYPE, CONF_UNIQUE_ID
from homeassistant.core import callback
from homeassistant.helpers import device_registry
from homeassistant.helpers import device_registry as dr
from homeassistant.util import slugify
from ..const import ATTR_HUE_EVENT, CONF_SUBTYPE, DOMAIN
@ -29,7 +29,7 @@ async def async_setup_hue_events(bridge: "HueBridge"):
hass = bridge.hass
api: HueBridgeV2 = bridge.api # to satisfy typing
conf_entry = bridge.config_entry
dev_reg = device_registry.async_get(hass)
dev_reg = dr.async_get(hass)
btn_controller = api.sensors.button
rotary_controller = api.sensors.relative_rotary