From 8dfa628af0803b643fe666808597913f21249818 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 22 Oct 2021 12:12:19 +0200 Subject: [PATCH] Use DeviceInfo on components with via_device (A-G) (#58210) Co-authored-by: epenet --- homeassistant/components/acmeda/base.py | 14 +++++------ homeassistant/components/bosch_shc/entity.py | 16 ++++++------ .../components/deconz/deconz_device.py | 23 +++++++++-------- homeassistant/components/deconz/light.py | 17 +++++++------ homeassistant/components/directv/const.py | 3 --- homeassistant/components/directv/entity.py | 25 +++++++------------ homeassistant/components/elkm1/__init__.py | 10 ++++---- homeassistant/components/freebox/sensor.py | 14 +++++------ homeassistant/components/fritz/common.py | 16 ++++++------ homeassistant/components/guardian/__init__.py | 12 ++++----- 10 files changed, 71 insertions(+), 79 deletions(-) diff --git a/homeassistant/components/acmeda/base.py b/homeassistant/components/acmeda/base.py index 459f4ab2097..df835950380 100644 --- a/homeassistant/components/acmeda/base.py +++ b/homeassistant/components/acmeda/base.py @@ -77,11 +77,11 @@ class AcmedaBase(entity.Entity): return self.roller.name @property - def device_info(self): + def device_info(self) -> entity.DeviceInfo: """Return the device info.""" - return { - "identifiers": {(DOMAIN, self.unique_id)}, - "name": self.roller.name, - "manufacturer": "Rollease Acmeda", - "via_device": (DOMAIN, self.roller.hub.id), - } + return entity.DeviceInfo( + identifiers={(DOMAIN, self.unique_id)}, + name=self.roller.name, + manufacturer="Rollease Acmeda", + via_device=(DOMAIN, self.roller.hub.id), + ) diff --git a/homeassistant/components/bosch_shc/entity.py b/homeassistant/components/bosch_shc/entity.py index a8966ce2f4a..1176c29e351 100644 --- a/homeassistant/components/bosch_shc/entity.py +++ b/homeassistant/components/bosch_shc/entity.py @@ -2,7 +2,7 @@ from boschshcpy.device import SHCDevice from homeassistant.helpers.device_registry import async_get as get_dev_reg -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.entity import DeviceInfo, Entity from .const import DOMAIN @@ -28,18 +28,18 @@ class SHCEntity(Entity): self._entry_id = entry_id self._attr_name = device.name self._attr_unique_id = device.serial - self._attr_device_info = { - "identifiers": {(DOMAIN, device.id)}, - "name": device.name, - "manufacturer": device.manufacturer, - "model": device.device_model, - "via_device": ( + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, device.id)}, + name=device.name, + manufacturer=device.manufacturer, + model=device.device_model, + via_device=( DOMAIN, device.parent_device_id if device.parent_device_id is not None else parent_id, ), - } + ) async def async_added_to_hass(self): """Subscribe to SHC events.""" diff --git a/homeassistant/components/deconz/deconz_device.py b/homeassistant/components/deconz/deconz_device.py index e8f27e35f98..bbd4051c177 100644 --- a/homeassistant/components/deconz/deconz_device.py +++ b/homeassistant/components/deconz/deconz_device.py @@ -1,9 +1,10 @@ """Base class for deCONZ devices.""" +from __future__ import annotations from homeassistant.core import callback from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE from homeassistant.helpers.dispatcher import async_dispatcher_connect -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.entity import DeviceInfo, Entity from .const import DOMAIN as DECONZ_DOMAIN @@ -30,20 +31,20 @@ class DeconzBase: return self._device.unique_id.split("-", 1)[0] @property - def device_info(self): + def device_info(self) -> DeviceInfo | None: """Return a device description for device registry.""" if self.serial is None: return None - return { - "connections": {(CONNECTION_ZIGBEE, self.serial)}, - "identifiers": {(DECONZ_DOMAIN, self.serial)}, - "manufacturer": self._device.manufacturer, - "model": self._device.model_id, - "name": self._device.name, - "sw_version": self._device.software_version, - "via_device": (DECONZ_DOMAIN, self.gateway.api.config.bridge_id), - } + return DeviceInfo( + connections={(CONNECTION_ZIGBEE, self.serial)}, + identifiers={(DECONZ_DOMAIN, self.serial)}, + manufacturer=self._device.manufacturer, + model=self._device.model_id, + name=self._device.name, + sw_version=self._device.software_version, + via_device=(DECONZ_DOMAIN, self.gateway.api.config.bridge_id), + ) class DeconzDevice(DeconzBase, Entity): diff --git a/homeassistant/components/deconz/light.py b/homeassistant/components/deconz/light.py index 0069e0fb7d9..6bb4f5c5b00 100644 --- a/homeassistant/components/deconz/light.py +++ b/homeassistant/components/deconz/light.py @@ -35,6 +35,7 @@ from homeassistant.components.light import ( ) from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import DeviceInfo from homeassistant.util.color import color_hs_to_xy from .const import DOMAIN as DECONZ_DOMAIN, POWER_PLUGS @@ -266,15 +267,15 @@ class DeconzGroup(DeconzBaseLight): return self._unique_id @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return a device description for device registry.""" - return { - "identifiers": {(DECONZ_DOMAIN, self.unique_id)}, - "manufacturer": "Dresden Elektronik", - "model": "deCONZ group", - "name": self._device.name, - "via_device": (DECONZ_DOMAIN, self.gateway.api.config.bridge_id), - } + return DeviceInfo( + identifiers={(DECONZ_DOMAIN, self.unique_id)}, + manufacturer="Dresden Elektronik", + model="deCONZ group", + name=self._device.name, + via_device=(DECONZ_DOMAIN, self.gateway.api.config.bridge_id), + ) @property def extra_state_attributes(self): diff --git a/homeassistant/components/directv/const.py b/homeassistant/components/directv/const.py index b840b7bd2dc..e90fd6879c7 100644 --- a/homeassistant/components/directv/const.py +++ b/homeassistant/components/directv/const.py @@ -1,6 +1,4 @@ """Constants for the DirecTV integration.""" -from typing import Final - DOMAIN = "directv" # Attributes @@ -8,7 +6,6 @@ ATTR_MEDIA_CURRENTLY_RECORDING = "media_currently_recording" ATTR_MEDIA_RATING = "media_rating" ATTR_MEDIA_RECORDED = "media_recorded" ATTR_MEDIA_START_TIME = "media_start_time" -ATTR_VIA_DEVICE: Final = "via_device" CONF_RECEIVER_ID = "receiver_id" diff --git a/homeassistant/components/directv/entity.py b/homeassistant/components/directv/entity.py index 2e6ffb81a52..b3ac8fd7df6 100644 --- a/homeassistant/components/directv/entity.py +++ b/homeassistant/components/directv/entity.py @@ -3,16 +3,9 @@ from __future__ import annotations from directv import DIRECTV -from homeassistant.const import ( - ATTR_IDENTIFIERS, - ATTR_MANUFACTURER, - ATTR_MODEL, - ATTR_NAME, - ATTR_SW_VERSION, -) from homeassistant.helpers.entity import DeviceInfo, Entity -from .const import ATTR_VIA_DEVICE, DOMAIN +from .const import DOMAIN class DIRECTVEntity(Entity): @@ -28,11 +21,11 @@ class DIRECTVEntity(Entity): @property def device_info(self) -> DeviceInfo: """Return device information about this DirecTV receiver.""" - return { - ATTR_IDENTIFIERS: {(DOMAIN, self._device_id)}, - ATTR_NAME: self.name, - ATTR_MANUFACTURER: self.dtv.device.info.brand, - ATTR_MODEL: None, - ATTR_SW_VERSION: self.dtv.device.info.version, - ATTR_VIA_DEVICE: (DOMAIN, self.dtv.device.info.receiver_id), - } + return DeviceInfo( + identifiers={(DOMAIN, self._device_id)}, + name=self.name, + manufacturer=self.dtv.device.info.brand, + model=None, + sw_version=self.dtv.device.info.version, + via_device=(DOMAIN, self.dtv.device.info.receiver_id), + ) diff --git a/homeassistant/components/elkm1/__init__.py b/homeassistant/components/elkm1/__init__.py index 0b4fa26e833..775ac466445 100644 --- a/homeassistant/components/elkm1/__init__.py +++ b/homeassistant/components/elkm1/__init__.py @@ -26,7 +26,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.entity import DeviceInfo, Entity from homeassistant.helpers.typing import ConfigType import homeassistant.util.dt as dt_util @@ -452,11 +452,11 @@ class ElkEntity(Entity): self._element_callback(self._element, {}) @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Device info connecting via the ElkM1 system.""" - return { - "via_device": (DOMAIN, f"{self._prefix}_system"), - } + return DeviceInfo( + via_device=(DOMAIN, f"{self._prefix}_system"), + ) class ElkAttachedEntity(ElkEntity): diff --git a/homeassistant/components/freebox/sensor.py b/homeassistant/components/freebox/sensor.py index 654a73b786c..6b56300d743 100644 --- a/homeassistant/components/freebox/sensor.py +++ b/homeassistant/components/freebox/sensor.py @@ -163,16 +163,16 @@ class FreeboxDiskSensor(FreeboxSensor): @property def device_info(self) -> DeviceInfo: """Return the device information.""" - return { - "identifiers": {(DOMAIN, self._disk["id"])}, - "name": f"Disk {self._disk['id']}", - "model": self._disk["model"], - "sw_version": self._disk["firmware"], - "via_device": ( + return DeviceInfo( + identifiers={(DOMAIN, self._disk["id"])}, + name=f"Disk {self._disk['id']}", + model=self._disk["model"], + sw_version=self._disk["firmware"], + via_device=( DOMAIN, self._router.mac, ), - } + ) @callback def async_update_state(self) -> None: diff --git a/homeassistant/components/fritz/common.py b/homeassistant/components/fritz/common.py index 053b0e117d9..89356ac5b42 100644 --- a/homeassistant/components/fritz/common.py +++ b/homeassistant/components/fritz/common.py @@ -351,17 +351,17 @@ class FritzDeviceBase(Entity): @property def device_info(self) -> DeviceInfo: """Return the device information.""" - return { - "connections": {(CONNECTION_NETWORK_MAC, self._mac)}, - "identifiers": {(DOMAIN, self._mac)}, - "default_name": self.name, - "default_manufacturer": "AVM", - "default_model": "FRITZ!Box Tracked device", - "via_device": ( + return DeviceInfo( + connections={(CONNECTION_NETWORK_MAC, self._mac)}, + identifiers={(DOMAIN, self._mac)}, + default_name=self.name, + default_manufacturer="AVM", + default_model="FRITZ!Box Tracked device", + via_device=( DOMAIN, self._router.unique_id, ), - } + ) @property def should_poll(self) -> bool: diff --git a/homeassistant/components/guardian/__init__.py b/homeassistant/components/guardian/__init__.py index 94413c76578..9cb2590a289 100644 --- a/homeassistant/components/guardian/__init__.py +++ b/homeassistant/components/guardian/__init__.py @@ -11,7 +11,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ATTRIBUTION, CONF_IP_ADDRESS, CONF_PORT from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_send -from homeassistant.helpers.entity import EntityDescription +from homeassistant.helpers.entity import DeviceInfo, EntityDescription from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, DataUpdateCoordinator, @@ -244,11 +244,11 @@ class PairedSensorEntity(GuardianEntity): super().__init__(entry, description) paired_sensor_uid = coordinator.data["uid"] - self._attr_device_info = { - "identifiers": {(DOMAIN, paired_sensor_uid)}, - "name": f"Guardian Paired Sensor {paired_sensor_uid}", - "via_device": (DOMAIN, entry.data[CONF_UID]), - } + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, paired_sensor_uid)}, + name=f"Guardian Paired Sensor {paired_sensor_uid}", + via_device=(DOMAIN, entry.data[CONF_UID]), + ) self._attr_name = ( f"Guardian Paired Sensor {paired_sensor_uid}: {description.name}" )