From 58c5b5058cf4036f1ca8bafc0545f20d140451bc Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 22 Oct 2021 11:52:48 +0200 Subject: [PATCH] Use DeviceInfo on components with via_device (H) (#58211) Co-authored-by: epenet --- .../components/hive/binary_sensor.py | 19 +++++++------- homeassistant/components/hive/climate.py | 19 +++++++------- homeassistant/components/hive/light.py | 19 +++++++------- homeassistant/components/hive/sensor.py | 19 +++++++------- homeassistant/components/hive/switch.py | 22 +++++++++------- homeassistant/components/hive/water_heater.py | 19 +++++++------- .../homematicip_cloud/alarm_control_panel.py | 14 +++++----- .../components/homematicip_cloud/climate.py | 14 +++++----- .../homematicip_cloud/generic_entity.py | 16 ++++++------ homeassistant/components/hue/sensor_device.py | 18 ++++++------- .../hunterdouglas_powerview/entity.py | 26 ++++++++++--------- 11 files changed, 108 insertions(+), 97 deletions(-) diff --git a/homeassistant/components/hive/binary_sensor.py b/homeassistant/components/hive/binary_sensor.py index d5f1ca53afd..71a2e787aec 100644 --- a/homeassistant/components/hive/binary_sensor.py +++ b/homeassistant/components/hive/binary_sensor.py @@ -9,6 +9,7 @@ from homeassistant.components.binary_sensor import ( DEVICE_CLASS_SOUND, BinarySensorEntity, ) +from homeassistant.helpers.entity import DeviceInfo from . import HiveEntity from .const import ATTR_MODE, DOMAIN @@ -46,16 +47,16 @@ class HiveBinarySensorEntity(HiveEntity, BinarySensorEntity): return self._unique_id @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return device information.""" - return { - "identifiers": {(DOMAIN, self.device["device_id"])}, - "name": self.device["device_name"], - "model": self.device["deviceData"]["model"], - "manufacturer": self.device["deviceData"]["manufacturer"], - "sw_version": self.device["deviceData"]["version"], - "via_device": (DOMAIN, self.device["parentDevice"]), - } + return DeviceInfo( + identifiers={(DOMAIN, self.device["device_id"])}, + name=self.device["device_name"], + model=self.device["deviceData"]["model"], + manufacturer=self.device["deviceData"]["manufacturer"], + sw_version=self.device["deviceData"]["version"], + via_device=(DOMAIN, self.device["parentDevice"]), + ) @property def device_class(self): diff --git a/homeassistant/components/hive/climate.py b/homeassistant/components/hive/climate.py index 80a6bb0941e..930a6698518 100644 --- a/homeassistant/components/hive/climate.py +++ b/homeassistant/components/hive/climate.py @@ -19,6 +19,7 @@ from homeassistant.components.climate.const import ( ) from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.helpers import config_validation as cv, entity_platform +from homeassistant.helpers.entity import DeviceInfo from . import HiveEntity, refresh_system from .const import ( @@ -117,16 +118,16 @@ class HiveClimateEntity(HiveEntity, ClimateEntity): return self._unique_id @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return device information.""" - return { - "identifiers": {(DOMAIN, self.device["device_id"])}, - "name": self.device["device_name"], - "model": self.device["deviceData"]["model"], - "manufacturer": self.device["deviceData"]["manufacturer"], - "sw_version": self.device["deviceData"]["version"], - "via_device": (DOMAIN, self.device["parentDevice"]), - } + return DeviceInfo( + identifiers={(DOMAIN, self.device["device_id"])}, + name=self.device["device_name"], + model=self.device["deviceData"]["model"], + manufacturer=self.device["deviceData"]["manufacturer"], + sw_version=self.device["deviceData"]["version"], + via_device=(DOMAIN, self.device["parentDevice"]), + ) @property def supported_features(self): diff --git a/homeassistant/components/hive/light.py b/homeassistant/components/hive/light.py index 46e8c5b5790..37158da7b58 100644 --- a/homeassistant/components/hive/light.py +++ b/homeassistant/components/hive/light.py @@ -10,6 +10,7 @@ from homeassistant.components.light import ( SUPPORT_COLOR_TEMP, LightEntity, ) +from homeassistant.helpers.entity import DeviceInfo import homeassistant.util.color as color_util from . import HiveEntity, refresh_system @@ -40,16 +41,16 @@ class HiveDeviceLight(HiveEntity, LightEntity): return self._unique_id @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return device information.""" - return { - "identifiers": {(DOMAIN, self.device["device_id"])}, - "name": self.device["device_name"], - "model": self.device["deviceData"]["model"], - "manufacturer": self.device["deviceData"]["manufacturer"], - "sw_version": self.device["deviceData"]["version"], - "via_device": (DOMAIN, self.device["parentDevice"]), - } + return DeviceInfo( + identifiers={(DOMAIN, self.device["device_id"])}, + name=self.device["device_name"], + model=self.device["deviceData"]["model"], + manufacturer=self.device["deviceData"]["manufacturer"], + sw_version=self.device["deviceData"]["version"], + via_device=(DOMAIN, self.device["parentDevice"]), + ) @property def name(self): diff --git a/homeassistant/components/hive/sensor.py b/homeassistant/components/hive/sensor.py index 5ea81bff123..abb45d04dec 100644 --- a/homeassistant/components/hive/sensor.py +++ b/homeassistant/components/hive/sensor.py @@ -3,6 +3,7 @@ from datetime import timedelta from homeassistant.components.sensor import DEVICE_CLASS_BATTERY, SensorEntity +from homeassistant.helpers.entity import DeviceInfo from . import HiveEntity from .const import DOMAIN @@ -35,16 +36,16 @@ class HiveSensorEntity(HiveEntity, SensorEntity): return self._unique_id @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return device information.""" - return { - "identifiers": {(DOMAIN, self.device["device_id"])}, - "name": self.device["device_name"], - "model": self.device["deviceData"]["model"], - "manufacturer": self.device["deviceData"]["manufacturer"], - "sw_version": self.device["deviceData"]["version"], - "via_device": (DOMAIN, self.device["parentDevice"]), - } + return DeviceInfo( + identifiers={(DOMAIN, self.device["device_id"])}, + name=self.device["device_name"], + model=self.device["deviceData"]["model"], + manufacturer=self.device["deviceData"]["manufacturer"], + sw_version=self.device["deviceData"]["version"], + via_device=(DOMAIN, self.device["parentDevice"]), + ) @property def available(self): diff --git a/homeassistant/components/hive/switch.py b/homeassistant/components/hive/switch.py index 7ad81a25f0e..9c009e1627c 100644 --- a/homeassistant/components/hive/switch.py +++ b/homeassistant/components/hive/switch.py @@ -1,7 +1,10 @@ """Support for the Hive switches.""" +from __future__ import annotations + from datetime import timedelta from homeassistant.components.switch import SwitchEntity +from homeassistant.helpers.entity import DeviceInfo from . import HiveEntity, refresh_system from .const import ATTR_MODE, DOMAIN @@ -31,17 +34,18 @@ class HiveDevicePlug(HiveEntity, SwitchEntity): return self._unique_id @property - def device_info(self): + def device_info(self) -> DeviceInfo | None: """Return device information.""" if self.device["hiveType"] == "activeplug": - return { - "identifiers": {(DOMAIN, self.device["device_id"])}, - "name": self.device["device_name"], - "model": self.device["deviceData"]["model"], - "manufacturer": self.device["deviceData"]["manufacturer"], - "sw_version": self.device["deviceData"]["version"], - "via_device": (DOMAIN, self.device["parentDevice"]), - } + return DeviceInfo( + identifiers={(DOMAIN, self.device["device_id"])}, + name=self.device["device_name"], + model=self.device["deviceData"]["model"], + manufacturer=self.device["deviceData"]["manufacturer"], + sw_version=self.device["deviceData"]["version"], + via_device=(DOMAIN, self.device["parentDevice"]), + ) + return None @property def name(self): diff --git a/homeassistant/components/hive/water_heater.py b/homeassistant/components/hive/water_heater.py index b9377a378c3..8cefda074b6 100644 --- a/homeassistant/components/hive/water_heater.py +++ b/homeassistant/components/hive/water_heater.py @@ -13,6 +13,7 @@ from homeassistant.components.water_heater import ( ) from homeassistant.const import TEMP_CELSIUS from homeassistant.helpers import config_validation as cv, entity_platform +from homeassistant.helpers.entity import DeviceInfo from . import HiveEntity, refresh_system from .const import ( @@ -78,16 +79,16 @@ class HiveWaterHeater(HiveEntity, WaterHeaterEntity): return self._unique_id @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return device information.""" - return { - "identifiers": {(DOMAIN, self.device["device_id"])}, - "name": self.device["device_name"], - "model": self.device["deviceData"]["model"], - "manufacturer": self.device["deviceData"]["manufacturer"], - "sw_version": self.device["deviceData"]["version"], - "via_device": (DOMAIN, self.device["parentDevice"]), - } + return DeviceInfo( + identifiers={(DOMAIN, self.device["device_id"])}, + name=self.device["device_name"], + model=self.device["deviceData"]["model"], + manufacturer=self.device["deviceData"]["manufacturer"], + sw_version=self.device["deviceData"]["version"], + via_device=(DOMAIN, self.device["parentDevice"]), + ) @property def supported_features(self): diff --git a/homeassistant/components/homematicip_cloud/alarm_control_panel.py b/homeassistant/components/homematicip_cloud/alarm_control_panel.py index 212737b7018..6249b004d23 100644 --- a/homeassistant/components/homematicip_cloud/alarm_control_panel.py +++ b/homeassistant/components/homematicip_cloud/alarm_control_panel.py @@ -47,13 +47,13 @@ class HomematicipAlarmControlPanelEntity(AlarmControlPanelEntity): @property def device_info(self) -> DeviceInfo: """Return device specific attributes.""" - return { - "identifiers": {(HMIPC_DOMAIN, f"ACP {self._home.id}")}, - "name": self.name, - "manufacturer": "eQ-3", - "model": CONST_ALARM_CONTROL_PANEL_NAME, - "via_device": (HMIPC_DOMAIN, self._home.id), - } + return DeviceInfo( + identifiers={(HMIPC_DOMAIN, f"ACP {self._home.id}")}, + name=self.name, + manufacturer="eQ-3", + model=CONST_ALARM_CONTROL_PANEL_NAME, + via_device=(HMIPC_DOMAIN, self._home.id), + ) @property def state(self) -> str: diff --git a/homeassistant/components/homematicip_cloud/climate.py b/homeassistant/components/homematicip_cloud/climate.py index 060d265c62a..df1c3d11f31 100644 --- a/homeassistant/components/homematicip_cloud/climate.py +++ b/homeassistant/components/homematicip_cloud/climate.py @@ -76,13 +76,13 @@ class HomematicipHeatingGroup(HomematicipGenericEntity, ClimateEntity): @property def device_info(self) -> DeviceInfo: """Return device specific attributes.""" - return { - "identifiers": {(HMIPC_DOMAIN, self._device.id)}, - "name": self._device.label, - "manufacturer": "eQ-3", - "model": self._device.modelType, - "via_device": (HMIPC_DOMAIN, self._device.homeId), - } + return DeviceInfo( + identifiers={(HMIPC_DOMAIN, self._device.id)}, + name=self._device.label, + manufacturer="eQ-3", + model=self._device.modelType, + via_device=(HMIPC_DOMAIN, self._device.homeId), + ) @property def temperature_unit(self) -> str: diff --git a/homeassistant/components/homematicip_cloud/generic_entity.py b/homeassistant/components/homematicip_cloud/generic_entity.py index 976650aa46b..2de3ded42db 100644 --- a/homeassistant/components/homematicip_cloud/generic_entity.py +++ b/homeassistant/components/homematicip_cloud/generic_entity.py @@ -96,18 +96,18 @@ class HomematicipGenericEntity(Entity): """Return device specific attributes.""" # Only physical devices should be HA devices. if isinstance(self._device, AsyncDevice): - return { - "identifiers": { + return DeviceInfo( + identifiers={ # Serial numbers of Homematic IP device (HMIPC_DOMAIN, self._device.id) }, - "name": self._device.label, - "manufacturer": self._device.oem, - "model": self._device.modelType, - "sw_version": self._device.firmwareVersion, + name=self._device.label, + manufacturer=self._device.oem, + model=self._device.modelType, + sw_version=self._device.firmwareVersion, # Link to the homematic ip access point. - "via_device": (HMIPC_DOMAIN, self._device.homeId), - } + via_device=(HMIPC_DOMAIN, self._device.homeId), + ) return None async def async_added_to_hass(self) -> None: diff --git a/homeassistant/components/hue/sensor_device.py b/homeassistant/components/hue/sensor_device.py index 8670f0853a3..4efb89a913f 100644 --- a/homeassistant/components/hue/sensor_device.py +++ b/homeassistant/components/hue/sensor_device.py @@ -40,19 +40,19 @@ class GenericHueDevice(entity.Entity): return self.primary_sensor.raw.get("swupdate", {}).get("state") @property - def device_info(self): + def device_info(self) -> entity.DeviceInfo: """Return the device info. Links individual entities together in the hass device registry. """ - return { - "identifiers": {(HUE_DOMAIN, self.device_id)}, - "name": self.primary_sensor.name, - "manufacturer": self.primary_sensor.manufacturername, - "model": (self.primary_sensor.productname or self.primary_sensor.modelid), - "sw_version": self.primary_sensor.swversion, - "via_device": (HUE_DOMAIN, self.bridge.api.config.bridgeid), - } + return entity.DeviceInfo( + identifiers={(HUE_DOMAIN, self.device_id)}, + name=self.primary_sensor.name, + manufacturer=self.primary_sensor.manufacturername, + model=(self.primary_sensor.productname or self.primary_sensor.modelid), + sw_version=self.primary_sensor.swversion, + via_device=(HUE_DOMAIN, self.bridge.api.config.bridgeid), + ) async def async_added_to_hass(self) -> None: """Handle entity being added to Home Assistant.""" diff --git a/homeassistant/components/hunterdouglas_powerview/entity.py b/homeassistant/components/hunterdouglas_powerview/entity.py index db4b984703c..a5a4fe852fa 100644 --- a/homeassistant/components/hunterdouglas_powerview/entity.py +++ b/homeassistant/components/hunterdouglas_powerview/entity.py @@ -2,7 +2,9 @@ from aiopvapi.resources.shade import ATTR_TYPE +from homeassistant.const import ATTR_MODEL, ATTR_SW_VERSION import homeassistant.helpers.device_registry as dr +from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ( @@ -63,21 +65,21 @@ class ShadeEntity(HDEntity): self._shade = shade @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return the device_info of the device.""" - device_info = { - "identifiers": {(DOMAIN, self._shade.id)}, - "name": self._shade_name, - "suggested_area": self._room_name, - "manufacturer": MANUFACTURER, - "model": str(self._shade.raw_data[ATTR_TYPE]), - "via_device": (DOMAIN, self._device_info[DEVICE_SERIAL_NUMBER]), - } + device_info = DeviceInfo( + identifiers={(DOMAIN, self._shade.id)}, + name=self._shade_name, + suggested_area=self._room_name, + manufacturer=MANUFACTURER, + model=str(self._shade.raw_data[ATTR_TYPE]), + via_device=(DOMAIN, self._device_info[DEVICE_SERIAL_NUMBER]), + ) for shade in self._shade.shade_types: - if shade.shade_type == device_info["model"]: - device_info["model"] = shade.description + if shade.shade_type == device_info[ATTR_MODEL]: + device_info[ATTR_MODEL] = shade.description break if FIRMWARE not in self._shade.raw_data: @@ -86,6 +88,6 @@ class ShadeEntity(HDEntity): firmware = self._shade.raw_data[FIRMWARE] sw_version = f"{firmware[FIRMWARE_REVISION]}.{firmware[FIRMWARE_SUB_REVISION]}.{firmware[FIRMWARE_BUILD]}" - device_info["sw_version"] = sw_version + device_info[ATTR_SW_VERSION] = sw_version return device_info