From e7ce01e649f08cd692c821d8ec41a5aae0d42129 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 27 May 2024 12:50:11 +0200 Subject: [PATCH] Enforce namespace import in components (#118218) --- .../components/airthings_ble/sensor.py | 12 ++++------ homeassistant/components/bosch_shc/entity.py | 5 ++-- homeassistant/components/bthome/__init__.py | 9 +++----- homeassistant/components/bthome/logbook.py | 6 ++--- homeassistant/components/deconz/services.py | 8 ++----- .../components/epson/media_player.py | 16 ++++++------- .../components/eq3btsmart/climate.py | 12 ++++------ .../components/geo_json_events/__init__.py | 9 +++----- .../components/homematicip_cloud/__init__.py | 10 ++++---- homeassistant/components/hue/migration.py | 23 ++++++++----------- homeassistant/components/hue/v2/entity.py | 4 ++-- homeassistant/components/ibeacon/__init__.py | 7 ++++-- homeassistant/components/netatmo/sensor.py | 7 ++---- homeassistant/components/nina/config_flow.py | 9 +++----- homeassistant/components/sabnzbd/__init__.py | 5 ++-- homeassistant/components/shelly/__init__.py | 21 ++++++++--------- homeassistant/components/shelly/climate.py | 12 ++++------ .../components/shelly/coordinator.py | 13 ++++------- homeassistant/components/shelly/entity.py | 15 +++++------- homeassistant/components/shelly/utils.py | 20 ++++++++-------- .../components/squeezebox/config_flow.py | 4 ++-- homeassistant/components/tasmota/__init__.py | 8 ++----- homeassistant/components/tibber/sensor.py | 13 ++++------- .../components/unifi/hub/entity_loader.py | 3 +-- .../components/unifiprotect/repairs.py | 4 ++-- homeassistant/components/wemo/coordinator.py | 9 +++----- .../components/xiaomi_ble/__init__.py | 9 +++----- 27 files changed, 112 insertions(+), 161 deletions(-) diff --git a/homeassistant/components/airthings_ble/sensor.py b/homeassistant/components/airthings_ble/sensor.py index 2883c2b351e..b1ae7d533d8 100644 --- a/homeassistant/components/airthings_ble/sensor.py +++ b/homeassistant/components/airthings_ble/sensor.py @@ -23,16 +23,12 @@ from homeassistant.const import ( UnitOfTemperature, ) from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.device_registry import ( - CONNECTION_BLUETOOTH, - DeviceInfo, - async_get as device_async_get, -) +from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_registry import ( RegistryEntry, async_entries_for_device, - async_get as entity_async_get, ) from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -115,13 +111,13 @@ SENSORS_MAPPING_TEMPLATE: dict[str, SensorEntityDescription] = { @callback def async_migrate(hass: HomeAssistant, address: str, sensor_name: str) -> None: """Migrate entities to new unique ids (with BLE Address).""" - ent_reg = entity_async_get(hass) + ent_reg = er.async_get(hass) unique_id_trailer = f"_{sensor_name}" new_unique_id = f"{address}{unique_id_trailer}" if ent_reg.async_get_entity_id(DOMAIN, Platform.SENSOR, new_unique_id): # New unique id already exists return - dev_reg = device_async_get(hass) + dev_reg = dr.async_get(hass) if not ( device := dev_reg.async_get_device( connections={(CONNECTION_BLUETOOTH, address)} diff --git a/homeassistant/components/bosch_shc/entity.py b/homeassistant/components/bosch_shc/entity.py index b7697191d27..06ce45cdb3a 100644 --- a/homeassistant/components/bosch_shc/entity.py +++ b/homeassistant/components/bosch_shc/entity.py @@ -5,7 +5,8 @@ from __future__ import annotations from boschshcpy import SHCDevice, SHCIntrusionSystem from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceInfo, async_get as get_dev_reg +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import Entity from .const import DOMAIN @@ -15,7 +16,7 @@ async def async_remove_devices( hass: HomeAssistant, entity: SHCBaseEntity, entry_id: str ) -> None: """Get item that is removed from session.""" - dev_registry = get_dev_reg(hass) + dev_registry = dr.async_get(hass) device = dev_registry.async_get_device(identifiers={(DOMAIN, entity.device_id)}) if device is not None: dev_registry.async_update_device(device.id, remove_config_entry_id=entry_id) diff --git a/homeassistant/components/bthome/__init__.py b/homeassistant/components/bthome/__init__.py index dab7a7db158..6f17adeeca7 100644 --- a/homeassistant/components/bthome/__init__.py +++ b/homeassistant/components/bthome/__init__.py @@ -15,11 +15,8 @@ from homeassistant.components.bluetooth import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import ( - CONNECTION_BLUETOOTH, - DeviceRegistry, - async_get, -) +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceRegistry from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.util.signal_type import SignalType @@ -130,7 +127,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: kwargs[CONF_BINDKEY] = bytes.fromhex(bindkey) data = BTHomeBluetoothDeviceData(**kwargs) - device_registry = async_get(hass) + device_registry = dr.async_get(hass) coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = ( BTHomePassiveBluetoothProcessorCoordinator( hass, diff --git a/homeassistant/components/bthome/logbook.py b/homeassistant/components/bthome/logbook.py index 23976e368ad..be5e156e99c 100644 --- a/homeassistant/components/bthome/logbook.py +++ b/homeassistant/components/bthome/logbook.py @@ -6,7 +6,7 @@ from collections.abc import Callable from homeassistant.components.logbook import LOGBOOK_ENTRY_MESSAGE, LOGBOOK_ENTRY_NAME from homeassistant.core import Event, HomeAssistant, callback -from homeassistant.helpers.device_registry import async_get +from homeassistant.helpers import device_registry as dr from .const import BTHOME_BLE_EVENT, DOMAIN, BTHomeBleEvent @@ -19,13 +19,13 @@ def async_describe_events( ], ) -> None: """Describe logbook events.""" - dr = async_get(hass) + dev_reg = dr.async_get(hass) @callback def async_describe_bthome_event(event: Event[BTHomeBleEvent]) -> dict[str, str]: """Describe bthome logbook event.""" data = event.data - device = dr.async_get(data["device_id"]) + device = dev_reg.async_get(data["device_id"]) name = device and device.name or f'BTHome {data["address"]}' if properties := data["event_properties"]: message = f"{data['event_class']} {data['event_type']}: {properties}" diff --git a/homeassistant/components/deconz/services.py b/homeassistant/components/deconz/services.py index 31648708b73..e10195d86bc 100644 --- a/homeassistant/components/deconz/services.py +++ b/homeassistant/components/deconz/services.py @@ -10,10 +10,6 @@ from homeassistant.helpers import ( entity_registry as er, ) from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC -from homeassistant.helpers.entity_registry import ( - async_entries_for_config_entry, - async_entries_for_device, -) from homeassistant.util.read_only_dict import ReadOnlyDict from .config_flow import get_master_hub @@ -146,7 +142,7 @@ async def async_remove_orphaned_entries_service(hub: DeconzHub) -> None: device_registry = dr.async_get(hub.hass) entity_registry = er.async_get(hub.hass) - entity_entries = async_entries_for_config_entry( + entity_entries = er.async_entries_for_config_entry( entity_registry, hub.config_entry.entry_id ) @@ -196,7 +192,7 @@ async def async_remove_orphaned_entries_service(hub: DeconzHub) -> None: for device_id in devices_to_be_removed: if ( len( - async_entries_for_device( + er.async_entries_for_device( entity_registry, device_id, include_disabled_entities=True ) ) diff --git a/homeassistant/components/epson/media_player.py b/homeassistant/components/epson/media_player.py index a962b94b5e0..a901e9df216 100644 --- a/homeassistant/components/epson/media_player.py +++ b/homeassistant/components/epson/media_player.py @@ -36,14 +36,14 @@ from homeassistant.components.media_player import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_platform -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.device_registry import ( - DeviceInfo, - async_get as async_get_device_registry, +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + entity_platform, + entity_registry as er, ) +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry from .const import ATTR_CMODE, DOMAIN, SERVICE_SELECT_CMODE @@ -110,13 +110,13 @@ class EpsonProjectorMediaPlayer(MediaPlayerEntity): return False if uid := await self._projector.get_serial_number(): self.hass.config_entries.async_update_entry(self._entry, unique_id=uid) - ent_reg = async_get_entity_registry(self.hass) + ent_reg = er.async_get(self.hass) old_entity_id = ent_reg.async_get_entity_id( "media_player", DOMAIN, self._entry.entry_id ) if old_entity_id is not None: ent_reg.async_update_entity(old_entity_id, new_unique_id=uid) - dev_reg = async_get_device_registry(self.hass) + dev_reg = dr.async_get(self.hass) device = dev_reg.async_get_device({(DOMAIN, self._entry.entry_id)}) if device is not None: dev_reg.async_update_device(device.id, new_identifiers={(DOMAIN, uid)}) diff --git a/homeassistant/components/eq3btsmart/climate.py b/homeassistant/components/eq3btsmart/climate.py index 326655d4e59..7b8ccb6c990 100644 --- a/homeassistant/components/eq3btsmart/climate.py +++ b/homeassistant/components/eq3btsmart/climate.py @@ -19,12 +19,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_TEMPERATURE, PRECISION_HALVES, UnitOfTemperature from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ServiceValidationError -from homeassistant.helpers.device_registry import ( - CONNECTION_BLUETOOTH, - DeviceInfo, - async_get, - format_mac, -) +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util import slugify @@ -88,7 +84,7 @@ class Eq3Climate(Eq3Entity, ClimateEntity): """Initialize the climate entity.""" super().__init__(eq3_config, thermostat) - self._attr_unique_id = format_mac(eq3_config.mac_address) + self._attr_unique_id = dr.format_mac(eq3_config.mac_address) self._attr_device_info = DeviceInfo( name=slugify(self._eq3_config.mac_address), manufacturer=MANUFACTURER, @@ -158,7 +154,7 @@ class Eq3Climate(Eq3Entity, ClimateEntity): def _async_on_device_updated(self) -> None: """Handle updated device data from the thermostat.""" - device_registry = async_get(self.hass) + device_registry = dr.async_get(self.hass) if device := device_registry.async_get_device( connections={(CONNECTION_BLUETOOTH, self._eq3_config.mac_address)}, ): diff --git a/homeassistant/components/geo_json_events/__init__.py b/homeassistant/components/geo_json_events/__init__.py index a50f7e432d9..d55fe6e3ee6 100644 --- a/homeassistant/components/geo_json_events/__init__.py +++ b/homeassistant/components/geo_json_events/__init__.py @@ -7,10 +7,7 @@ import logging from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from homeassistant.helpers.entity_registry import ( - async_entries_for_config_entry, - async_get, -) +from homeassistant.helpers import entity_registry as er from .const import DOMAIN, PLATFORMS from .manager import GeoJsonFeedEntityManager @@ -40,8 +37,8 @@ async def remove_orphaned_entities(hass: HomeAssistant, entry_id: str) -> None: has no previous data to compare against, and thus all entities managed by this integration are removed after startup. """ - entity_registry = async_get(hass) - orphaned_entries = async_entries_for_config_entry(entity_registry, entry_id) + entity_registry = er.async_get(hass) + orphaned_entries = er.async_entries_for_config_entry(entity_registry, entry_id) if orphaned_entries is not None: for entry in orphaned_entries: if entry.domain == Platform.GEO_LOCATION: diff --git a/homeassistant/components/homematicip_cloud/__init__.py b/homeassistant/components/homematicip_cloud/__init__.py index 2b2ddb64700..08002bc551a 100644 --- a/homeassistant/components/homematicip_cloud/__init__.py +++ b/homeassistant/components/homematicip_cloud/__init__.py @@ -6,9 +6,11 @@ from homeassistant import config_entries from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME, EVENT_HOMEASSISTANT_STOP from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import device_registry as dr, entity_registry as er -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.entity_registry import async_entries_for_config_entry +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + entity_registry as er, +) from homeassistant.helpers.typing import ConfigType from .const import ( @@ -129,7 +131,7 @@ def _async_remove_obsolete_entities( return entity_registry = er.async_get(hass) - er_entries = async_entries_for_config_entry(entity_registry, entry.entry_id) + er_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id) for er_entry in er_entries: if er_entry.unique_id.startswith("HomematicipAccesspointStatus"): entity_registry.async_remove(er_entry.entity_id) diff --git a/homeassistant/components/hue/migration.py b/homeassistant/components/hue/migration.py index f4bf6366d61..1214f39d146 100644 --- a/homeassistant/components/hue/migration.py +++ b/homeassistant/components/hue/migration.py @@ -12,15 +12,10 @@ from homeassistant.components.binary_sensor import BinarySensorDeviceClass from homeassistant.components.sensor import SensorDeviceClass from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_KEY, CONF_API_VERSION, CONF_HOST, CONF_USERNAME -from homeassistant.helpers import aiohttp_client -from homeassistant.helpers.device_registry import ( - async_entries_for_config_entry as devices_for_config_entries, - async_get as async_get_device_registry, -) -from homeassistant.helpers.entity_registry import ( - async_entries_for_config_entry as entities_for_config_entry, - async_entries_for_device, - async_get as async_get_entity_registry, +from homeassistant.helpers import ( + aiohttp_client, + device_registry as dr, + entity_registry as er, ) from .const import DOMAIN @@ -75,15 +70,15 @@ async def handle_v2_migration(hass: core.HomeAssistant, entry: ConfigEntry) -> N """Perform migration of devices and entities to V2 Id's.""" host = entry.data[CONF_HOST] api_key = entry.data[CONF_API_KEY] - dev_reg = async_get_device_registry(hass) - ent_reg = async_get_entity_registry(hass) + dev_reg = dr.async_get(hass) + ent_reg = er.async_get(hass) LOGGER.info("Start of migration of devices and entities to support API schema 2") # Create mapping of mac address to HA device id's. # Identifier in dev reg should be mac-address, # but in some cases it has a postfix like `-0b` or `-01`. dev_ids = {} - for hass_dev in devices_for_config_entries(dev_reg, entry.entry_id): + for hass_dev in dr.async_entries_for_config_entry(dev_reg, entry.entry_id): for domain, mac in hass_dev.identifiers: if domain != DOMAIN: continue @@ -128,7 +123,7 @@ async def handle_v2_migration(hass: core.HomeAssistant, entry: ConfigEntry) -> N LOGGER.info("Migrated device %s (%s)", hue_dev.metadata.name, hass_dev_id) # loop through all entities for device and find match - for ent in async_entries_for_device(ent_reg, hass_dev_id, True): + for ent in er.async_entries_for_device(ent_reg, hass_dev_id, True): if ent.entity_id.startswith("light"): # migrate light # should always return one lightid here @@ -179,7 +174,7 @@ async def handle_v2_migration(hass: core.HomeAssistant, entry: ConfigEntry) -> N ) # migrate entities that are not connected to a device (groups) - for ent in entities_for_config_entry(ent_reg, entry.entry_id): + for ent in er.async_entries_for_config_entry(ent_reg, entry.entry_id): if ent.device_id is not None: continue if "-" in ent.unique_id: diff --git a/homeassistant/components/hue/v2/entity.py b/homeassistant/components/hue/v2/entity.py index a7861ebd7b4..6575d7f4702 100644 --- a/homeassistant/components/hue/v2/entity.py +++ b/homeassistant/components/hue/v2/entity.py @@ -10,9 +10,9 @@ from aiohue.v2.models.resource import ResourceTypes from aiohue.v2.models.zigbee_connectivity import ConnectivityServiceStatus from homeassistant.core import callback +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import Entity -from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry from ..bridge import HueBridge from ..const import CONF_IGNORE_AVAILABILITY, DOMAIN @@ -128,7 +128,7 @@ class HueBaseEntity(Entity): if event_type == EventType.RESOURCE_DELETED: # cleanup entities that are not strictly device-bound and have the bridge as parent if self.device is None and resource.id == self.resource.id: - ent_reg = async_get_entity_registry(self.hass) + ent_reg = er.async_get(self.hass) ent_reg.async_remove(self.entity_id) return diff --git a/homeassistant/components/ibeacon/__init__.py b/homeassistant/components/ibeacon/__init__.py index 45561d8d964..14d5bbca17f 100644 --- a/homeassistant/components/ibeacon/__init__.py +++ b/homeassistant/components/ibeacon/__init__.py @@ -4,7 +4,8 @@ from __future__ import annotations from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceEntry, async_get +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.device_registry import DeviceEntry from .const import DOMAIN, PLATFORMS from .coordinator import IBeaconCoordinator @@ -14,7 +15,9 @@ type IBeaconConfigEntry = ConfigEntry[IBeaconCoordinator] async def async_setup_entry(hass: HomeAssistant, entry: IBeaconConfigEntry) -> bool: """Set up Bluetooth LE Tracker from a config entry.""" - entry.runtime_data = coordinator = IBeaconCoordinator(hass, entry, async_get(hass)) + entry.runtime_data = coordinator = IBeaconCoordinator( + hass, entry, dr.async_get(hass) + ) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await coordinator.async_start() return True diff --git a/homeassistant/components/netatmo/sensor.py b/homeassistant/components/netatmo/sensor.py index 7d99ef9d32c..c762666e041 100644 --- a/homeassistant/components/netatmo/sensor.py +++ b/homeassistant/components/netatmo/sensor.py @@ -33,10 +33,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr -from homeassistant.helpers.device_registry import ( - DeviceInfo, - async_entries_for_config_entry, -) +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send, @@ -459,7 +456,7 @@ async def async_setup_entry( """Retrieve Netatmo public weather entities.""" entities = { device.name: device.id - for device in async_entries_for_config_entry( + for device in dr.async_entries_for_config_entry( device_registry, entry.entry_id ) if device.model == "Public Weather station" diff --git a/homeassistant/components/nina/config_flow.py b/homeassistant/components/nina/config_flow.py index 221a9202ae4..3a665bfe987 100644 --- a/homeassistant/components/nina/config_flow.py +++ b/homeassistant/components/nina/config_flow.py @@ -14,12 +14,9 @@ from homeassistant.config_entries import ( OptionsFlow, ) from homeassistant.core import callback +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.entity_registry import ( - async_entries_for_config_entry, - async_get, -) from .const import ( _LOGGER, @@ -213,9 +210,9 @@ class OptionsFlowHandler(OptionsFlow): user_input, self._all_region_codes_sorted ) - entity_registry = async_get(self.hass) + entity_registry = er.async_get(self.hass) - entries = async_entries_for_config_entry( + entries = er.async_entries_for_config_entry( entity_registry, self.config_entry.entry_id ) diff --git a/homeassistant/components/sabnzbd/__init__.py b/homeassistant/components/sabnzbd/__init__.py index ebb9284a7f2..a827e9a36a4 100644 --- a/homeassistant/components/sabnzbd/__init__.py +++ b/homeassistant/components/sabnzbd/__init__.py @@ -20,8 +20,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.device_registry import async_get +from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries from homeassistant.helpers.typing import ConfigType @@ -121,7 +120,7 @@ def async_get_entry_id_for_service_call(hass: HomeAssistant, call: ServiceCall) def update_device_identifiers(hass: HomeAssistant, entry: ConfigEntry): """Update device identifiers to new identifiers.""" - device_registry = async_get(hass) + device_registry = dr.async_get(hass) device_entry = device_registry.async_get_device(identifiers={(DOMAIN, DOMAIN)}) if device_entry and entry.entry_id in device_entry.config_entries: new_identifiers = {(DOMAIN, entry.entry_id)} diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index ad03414e0ca..1bcd9c7c1e4 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -19,14 +19,13 @@ import voluptuous as vol from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import issue_registry as ir -from homeassistant.helpers.aiohttp_client import async_get_clientsession -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.device_registry import ( - CONNECTION_NETWORK_MAC, - async_get as dr_async_get, - format_mac, +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + issue_registry as ir, ) +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.typing import ConfigType from .const import ( @@ -151,11 +150,11 @@ async def _async_setup_block_entry( options, ) - dev_reg = dr_async_get(hass) + dev_reg = dr.async_get(hass) device_entry = None if entry.unique_id is not None: device_entry = dev_reg.async_get_device( - connections={(CONNECTION_NETWORK_MAC, format_mac(entry.unique_id))}, + connections={(CONNECTION_NETWORK_MAC, dr.format_mac(entry.unique_id))}, ) # https://github.com/home-assistant/core/pull/48076 if device_entry and entry.entry_id not in device_entry.config_entries: @@ -237,11 +236,11 @@ async def _async_setup_rpc_entry(hass: HomeAssistant, entry: ShellyConfigEntry) options, ) - dev_reg = dr_async_get(hass) + dev_reg = dr.async_get(hass) device_entry = None if entry.unique_id is not None: device_entry = dev_reg.async_get_device( - connections={(CONNECTION_NETWORK_MAC, format_mac(entry.unique_id))}, + connections={(CONNECTION_NETWORK_MAC, dr.format_mac(entry.unique_id))}, ) # https://github.com/home-assistant/core/pull/48076 if device_entry and entry.entry_id not in device_entry.config_entries: diff --git a/homeassistant/components/shelly/climate.py b/homeassistant/components/shelly/climate.py index 084ec11fd4a..a4dc71f870c 100644 --- a/homeassistant/components/shelly/climate.py +++ b/homeassistant/components/shelly/climate.py @@ -21,14 +21,10 @@ from homeassistant.components.climate import ( from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant, State, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import issue_registry as ir +from homeassistant.helpers import entity_registry as er, issue_registry as ir from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.entity_registry import ( - RegistryEntry, - async_entries_for_config_entry, - async_get as er_async_get, -) +from homeassistant.helpers.entity_registry import RegistryEntry from homeassistant.helpers.restore_state import ExtraStoredData, RestoreEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util.unit_conversion import TemperatureConverter @@ -104,8 +100,8 @@ def async_restore_climate_entities( ) -> None: """Restore sleeping climate devices.""" - ent_reg = er_async_get(hass) - entries = async_entries_for_config_entry(ent_reg, config_entry.entry_id) + ent_reg = er.async_get(hass) + entries = er.async_entries_for_config_entry(ent_reg, config_entry.entry_id) for entry in entries: if entry.domain != CLIMATE_DOMAIN: diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index d6aa77539f9..9d8416d64d9 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -22,12 +22,9 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback -from homeassistant.helpers import issue_registry as ir +from homeassistant.helpers import device_registry as dr, issue_registry as ir from homeassistant.helpers.debounce import Debouncer -from homeassistant.helpers.device_registry import ( - CONNECTION_NETWORK_MAC, - async_get as dr_async_get, -) +from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .bluetooth import async_connect_scanner @@ -141,7 +138,7 @@ class ShellyCoordinatorBase[_DeviceT: BlockDevice | RpcDevice]( def async_setup(self, pending_platforms: list[Platform] | None = None) -> None: """Set up the coordinator.""" self._pending_platforms = pending_platforms - dev_reg = dr_async_get(self.hass) + dev_reg = dr.async_get(self.hass) device_entry = dev_reg.async_get_or_create( config_entry_id=self.entry.entry_id, name=self.name, @@ -734,7 +731,7 @@ def get_block_coordinator_by_device_id( hass: HomeAssistant, device_id: str ) -> ShellyBlockCoordinator | None: """Get a Shelly block device coordinator for the given device id.""" - dev_reg = dr_async_get(hass) + dev_reg = dr.async_get(hass) if device := dev_reg.async_get(device_id): for config_entry in device.config_entries: entry = hass.config_entries.async_get_entry(config_entry) @@ -753,7 +750,7 @@ def get_rpc_coordinator_by_device_id( hass: HomeAssistant, device_id: str ) -> ShellyRpcCoordinator | None: """Get a Shelly RPC device coordinator for the given device id.""" - dev_reg = dr_async_get(hass) + dev_reg = dr.async_get(hass) if device := dev_reg.async_get(device_id): for config_entry in device.config_entries: entry = hass.config_entries.async_get_entry(config_entry) diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 4734edf83f6..e1530a669a1 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -11,14 +11,11 @@ from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError, RpcCal from homeassistant.core import HomeAssistant, State, callback from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.entity_registry import ( - RegistryEntry, - async_entries_for_config_entry, - async_get as er_async_get, -) +from homeassistant.helpers.entity_registry import RegistryEntry from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -112,8 +109,8 @@ def async_restore_block_attribute_entities( """Restore block attributes entities.""" entities = [] - ent_reg = er_async_get(hass) - entries = async_entries_for_config_entry(ent_reg, config_entry.entry_id) + ent_reg = er.async_get(hass) + entries = er.async_entries_for_config_entry(ent_reg, config_entry.entry_id) domain = sensor_class.__module__.split(".")[-1] @@ -221,8 +218,8 @@ def async_restore_rpc_attribute_entities( """Restore block attributes entities.""" entities = [] - ent_reg = er_async_get(hass) - entries = async_entries_for_config_entry(ent_reg, config_entry.entry_id) + ent_reg = er.async_get(hass) + entries = er.async_entries_for_config_entry(ent_reg, config_entry.entry_id) domain = sensor_class.__module__.split(".")[-1] diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index 87c5acc7898..bcd5a859538 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -28,13 +28,13 @@ from homeassistant.components.http import HomeAssistantView from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PORT, EVENT_HOMEASSISTANT_STOP from homeassistant.core import Event, HomeAssistant, callback -from homeassistant.helpers import issue_registry as ir, singleton -from homeassistant.helpers.device_registry import ( - CONNECTION_NETWORK_MAC, - async_get as dr_async_get, - format_mac, +from homeassistant.helpers import ( + device_registry as dr, + entity_registry as er, + issue_registry as ir, + singleton, ) -from homeassistant.helpers.entity_registry import async_get as er_async_get +from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.util.dt import utcnow from .const import ( @@ -60,7 +60,7 @@ def async_remove_shelly_entity( hass: HomeAssistant, domain: str, unique_id: str ) -> None: """Remove a Shelly entity.""" - entity_reg = er_async_get(hass) + entity_reg = er.async_get(hass) entity_id = entity_reg.async_get_entity_id(domain, DOMAIN, unique_id) if entity_id: LOGGER.debug("Removing entity: %s", entity_id) @@ -410,10 +410,10 @@ def update_device_fw_info( """Update the firmware version information in the device registry.""" assert entry.unique_id - dev_reg = dr_async_get(hass) + dev_reg = dr.async_get(hass) if device := dev_reg.async_get_device( identifiers={(DOMAIN, entry.entry_id)}, - connections={(CONNECTION_NETWORK_MAC, format_mac(entry.unique_id))}, + connections={(CONNECTION_NETWORK_MAC, dr.format_mac(entry.unique_id))}, ): if device.sw_version == shellydevice.firmware_version: return @@ -487,7 +487,7 @@ def async_remove_shelly_rpc_entities( hass: HomeAssistant, domain: str, mac: str, keys: list[str] ) -> None: """Remove RPC based Shelly entity.""" - entity_reg = er_async_get(hass) + entity_reg = er.async_get(hass) for key in keys: if entity_id := entity_reg.async_get_entity_id(domain, DOMAIN, f"{mac}-{key}"): LOGGER.debug("Removing entity: %s", entity_id) diff --git a/homeassistant/components/squeezebox/config_flow.py b/homeassistant/components/squeezebox/config_flow.py index c793019d0da..0da8fcce3f7 100644 --- a/homeassistant/components/squeezebox/config_flow.py +++ b/homeassistant/components/squeezebox/config_flow.py @@ -13,9 +13,9 @@ from homeassistant.components.media_player import DOMAIN as MP_DOMAIN from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.data_entry_flow import AbortFlow +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import format_mac -from homeassistant.helpers.entity_registry import async_get from .const import CONF_HTTPS, DEFAULT_PORT, DOMAIN @@ -199,7 +199,7 @@ class SqueezeboxConfigFlow(ConfigFlow, domain=DOMAIN): _LOGGER.debug("Configuring dhcp player with unique id: %s", self.unique_id) - registry = async_get(self.hass) + registry = er.async_get(self.hass) if TYPE_CHECKING: assert self.unique_id diff --git a/homeassistant/components/tasmota/__init__.py b/homeassistant/components/tasmota/__init__.py index d9294c5992a..f1acfa644bf 100644 --- a/homeassistant/components/tasmota/__init__.py +++ b/homeassistant/components/tasmota/__init__.py @@ -24,11 +24,7 @@ from homeassistant.components.mqtt import ( from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr -from homeassistant.helpers.device_registry import ( - CONNECTION_NETWORK_MAC, - DeviceRegistry, - async_entries_for_config_entry, -) +from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceRegistry from . import device_automation, discovery from .const import ( @@ -105,7 +101,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # detach device triggers device_registry = dr.async_get(hass) - devices = async_entries_for_config_entry(device_registry, entry.entry_id) + devices = dr.async_entries_for_config_entry(device_registry, entry.entry_id) for device in devices: await device_automation.async_remove_automations(hass, device.id) diff --git a/homeassistant/components/tibber/sensor.py b/homeassistant/components/tibber/sensor.py index c2faeb98ef3..f0131173403 100644 --- a/homeassistant/components/tibber/sensor.py +++ b/homeassistant/components/tibber/sensor.py @@ -30,12 +30,9 @@ from homeassistant.const import ( ) from homeassistant.core import Event, HomeAssistant, callback from homeassistant.exceptions import PlatformNotReady -from homeassistant.helpers.device_registry import ( - DeviceInfo, - async_get as async_get_dev_reg, -) +from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.entity_registry import async_get as async_get_entity_reg from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, @@ -269,8 +266,8 @@ async def async_setup_entry( tibber_connection = hass.data[TIBBER_DOMAIN] - entity_registry = async_get_entity_reg(hass) - device_registry = async_get_dev_reg(hass) + entity_registry = er.async_get(hass) + device_registry = dr.async_get(hass) coordinator: TibberDataCoordinator | None = None entities: list[TibberSensor] = [] @@ -548,7 +545,7 @@ class TibberRtDataCoordinator(DataUpdateCoordinator): # pylint: disable=hass-en self._async_remove_device_updates_handler = self.async_add_listener( self._add_sensors ) - self.entity_registry = async_get_entity_reg(hass) + self.entity_registry = er.async_get(hass) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop) @callback diff --git a/homeassistant/components/unifi/hub/entity_loader.py b/homeassistant/components/unifi/hub/entity_loader.py index 30b5ba6e686..29448a4114a 100644 --- a/homeassistant/components/unifi/hub/entity_loader.py +++ b/homeassistant/components/unifi/hub/entity_loader.py @@ -18,7 +18,6 @@ from homeassistant.core import callback from homeassistant.helpers import entity_registry as er from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.entity_registry import async_entries_for_config_entry from ..const import LOGGER, UNIFI_WIRELESS_CLIENTS from ..entity import UnifiEntity, UnifiEntityDescription @@ -86,7 +85,7 @@ class UnifiEntityLoader: entity_registry = er.async_get(self.hub.hass) macs: list[str] = [ entry.unique_id.split("-", 1)[1] - for entry in async_entries_for_config_entry( + for entry in er.async_entries_for_config_entry( entity_registry, config.entry.entry_id ) if entry.domain == Platform.DEVICE_TRACKER and "-" in entry.unique_id diff --git a/homeassistant/components/unifiprotect/repairs.py b/homeassistant/components/unifiprotect/repairs.py index ddd5dc087a1..baf08c9b5cf 100644 --- a/homeassistant/components/unifiprotect/repairs.py +++ b/homeassistant/components/unifiprotect/repairs.py @@ -13,7 +13,7 @@ from homeassistant import data_entry_flow from homeassistant.components.repairs import ConfirmRepairFlow, RepairsFlow from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.issue_registry import async_get as async_get_issue_registry +from homeassistant.helpers import issue_registry as ir from .const import CONF_ALLOW_EA from .utils import async_create_api_client @@ -34,7 +34,7 @@ class ProtectRepair(RepairsFlow): @callback def _async_get_placeholders(self) -> dict[str, str]: - issue_registry = async_get_issue_registry(self.hass) + issue_registry = ir.async_get(self.hass) description_placeholders = {} if issue := issue_registry.async_get_issue(self.handler, self.issue_id): description_placeholders = issue.translation_placeholders or {} diff --git a/homeassistant/components/wemo/coordinator.py b/homeassistant/components/wemo/coordinator.py index 9bedd12f54b..a186b666470 100644 --- a/homeassistant/components/wemo/coordinator.py +++ b/homeassistant/components/wemo/coordinator.py @@ -24,11 +24,8 @@ from homeassistant.const import ( CONF_UNIQUE_ID, ) from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.device_registry import ( - CONNECTION_UPNP, - DeviceInfo, - async_get as async_get_device_registry, -) +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.device_registry import CONNECTION_UPNP, DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DOMAIN, WEMO_SUBSCRIPTION_EVENT @@ -291,7 +288,7 @@ async def async_register_device( await device.async_refresh() if not device.last_update_success and device.last_exception: raise device.last_exception - device_registry = async_get_device_registry(hass) + device_registry = dr.async_get(hass) entry = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, **_create_device_info(wemo) ) diff --git a/homeassistant/components/xiaomi_ble/__init__.py b/homeassistant/components/xiaomi_ble/__init__.py index 19c1f3feea1..4a9753bfe85 100644 --- a/homeassistant/components/xiaomi_ble/__init__.py +++ b/homeassistant/components/xiaomi_ble/__init__.py @@ -17,11 +17,8 @@ from homeassistant.components.bluetooth import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import CoreState, HomeAssistant -from homeassistant.helpers.device_registry import ( - CONNECTION_BLUETOOTH, - DeviceRegistry, - async_get, -) +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceRegistry from homeassistant.helpers.dispatcher import async_dispatcher_send from .const import ( @@ -167,7 +164,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) return await data.async_poll(connectable_device) - device_registry = async_get(hass) + device_registry = dr.async_get(hass) coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = ( XiaomiActiveBluetoothProcessorCoordinator( hass,