From 12503d548b78f2b93745c6a396a0d0e39cb18ea3 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Thu, 22 Jul 2021 16:40:32 -0400 Subject: [PATCH] Use entity class attributes for canary (#53333) Co-authored-by: Franck Nijhof --- .../components/canary/alarm_control_panel.py | 21 ++----- homeassistant/components/canary/camera.py | 36 +++--------- homeassistant/components/canary/sensor.py | 56 ++++--------------- 3 files changed, 25 insertions(+), 88 deletions(-) diff --git a/homeassistant/components/canary/alarm_control_panel.py b/homeassistant/components/canary/alarm_control_panel.py index 4e29c40f49f..4d29d4893e7 100644 --- a/homeassistant/components/canary/alarm_control_panel.py +++ b/homeassistant/components/canary/alarm_control_panel.py @@ -52,6 +52,9 @@ class CanaryAlarm(CoordinatorEntity, AlarmControlPanelEntity): """Representation of a Canary alarm control panel.""" coordinator: CanaryDataUpdateCoordinator + _attr_supported_features = ( + SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_NIGHT + ) def __init__( self, coordinator: CanaryDataUpdateCoordinator, location: Location @@ -59,23 +62,14 @@ class CanaryAlarm(CoordinatorEntity, AlarmControlPanelEntity): """Initialize a Canary security camera.""" super().__init__(coordinator) self._location_id: str = location.location_id - self._location_name: str = location.name + self._attr_name = location.name + self._attr_unique_id = str(self._location_id) @property def location(self) -> Location: """Return information about the location.""" return self.coordinator.data["locations"][self._location_id] - @property - def name(self) -> str: - """Return the name of the alarm.""" - return self._location_name - - @property - def unique_id(self) -> str: - """Return the unique ID of the alarm.""" - return str(self._location_id) - @property def state(self) -> str | None: """Return the state of the device.""" @@ -92,11 +86,6 @@ class CanaryAlarm(CoordinatorEntity, AlarmControlPanelEntity): return None - @property - def supported_features(self) -> int: - """Return the list of supported features.""" - return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_NIGHT - @property def extra_state_attributes(self) -> dict[str, Any]: """Return the state attributes.""" diff --git a/homeassistant/components/canary/camera.py b/homeassistant/components/canary/camera.py index b1725945db2..2699ba1f640 100644 --- a/homeassistant/components/canary/camera.py +++ b/homeassistant/components/canary/camera.py @@ -21,7 +21,6 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream -from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import Throttle @@ -30,7 +29,6 @@ from .const import ( CONF_FFMPEG_ARGUMENTS, DATA_COORDINATOR, DEFAULT_FFMPEG_ARGUMENTS, - DEFAULT_TIMEOUT, DOMAIN, MANUFACTURER, ) @@ -73,7 +71,6 @@ async def async_setup_entry( coordinator, location_id, device, - DEFAULT_TIMEOUT, ffmpeg_arguments, ) ) @@ -92,7 +89,6 @@ class CanaryCamera(CoordinatorEntity, Camera): coordinator: CanaryDataUpdateCoordinator, location_id: str, device: Device, - timeout: int, ffmpeg_args: str, ) -> None: """Initialize a Canary security camera.""" @@ -102,37 +98,21 @@ class CanaryCamera(CoordinatorEntity, Camera): self._ffmpeg_arguments = ffmpeg_args self._location_id = location_id self._device = device - self._device_id: str = device.device_id - self._device_name: str = device.name - self._device_type_name = device.device_type["name"] - self._timeout = timeout self._live_stream_session: LiveStreamSession | None = None + self._attr_name = device.name + self._attr_unique_id = str(device.device_id) + self._attr_device_info = { + "identifiers": {(DOMAIN, str(device.device_id))}, + "name": device.name, + "model": device.device_type["name"], + "manufacturer": MANUFACTURER, + } @property def location(self) -> Location: """Return information about the location.""" return self.coordinator.data["locations"][self._location_id] - @property - def name(self) -> str: - """Return the name of this device.""" - return self._device_name - - @property - def unique_id(self) -> str: - """Return the unique ID of this camera.""" - return str(self._device_id) - - @property - def device_info(self) -> DeviceInfo: - """Return the device_info of the device.""" - return { - "identifiers": {(DOMAIN, str(self._device_id))}, - "name": self._device_name, - "model": self._device_type_name, - "manufacturer": MANUFACTURER, - } - @property def is_recording(self) -> bool: """Return true if the device is recording.""" diff --git a/homeassistant/components/canary/sensor.py b/homeassistant/components/canary/sensor.py index 91dc3bad5eb..5c92f0089f2 100644 --- a/homeassistant/components/canary/sensor.py +++ b/homeassistant/components/canary/sensor.py @@ -17,7 +17,6 @@ from homeassistant.const import ( TEMP_CELSIUS, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -97,11 +96,9 @@ class CanarySensor(CoordinatorEntity, SensorEntity): super().__init__(coordinator) self._sensor_type = sensor_type self._device_id = device.device_id - self._device_name = device.name - self._device_type_name = device.device_type["name"] sensor_type_name = sensor_type[0].replace("_", " ").title() - self._name = f"{location.name} {device.name} {sensor_type_name}" + self._attr_name = f"{location.name} {device.name} {sensor_type_name}" canary_sensor_type = None if self._sensor_type[0] == "air_quality": @@ -116,6 +113,17 @@ class CanarySensor(CoordinatorEntity, SensorEntity): canary_sensor_type = SensorType.BATTERY self._canary_type = canary_sensor_type + self._attr_state = self.reading + self._attr_unique_id = f"{device.device_id}_{sensor_type[0]}" + self._attr_device_info = { + "identifiers": {(DOMAIN, str(device.device_id))}, + "name": device.name, + "model": device.device_type["name"], + "manufacturer": MANUFACTURER, + } + self._attr_unit_of_measurement = sensor_type[1] + self._attr_device_class = sensor_type[3] + self._attr_icon = sensor_type[2] @property def reading(self) -> float | None: @@ -136,46 +144,6 @@ class CanarySensor(CoordinatorEntity, SensorEntity): return None - @property - def name(self) -> str: - """Return the name of the Canary sensor.""" - return self._name - - @property - def state(self) -> float | None: - """Return the state of the sensor.""" - return self.reading - - @property - def unique_id(self) -> str: - """Return the unique ID of this sensor.""" - return f"{self._device_id}_{self._sensor_type[0]}" - - @property - def device_info(self) -> DeviceInfo: - """Return the device_info of the device.""" - return { - "identifiers": {(DOMAIN, str(self._device_id))}, - "name": self._device_name, - "model": self._device_type_name, - "manufacturer": MANUFACTURER, - } - - @property - def unit_of_measurement(self) -> str | None: - """Return the unit of measurement.""" - return self._sensor_type[1] - - @property - def device_class(self) -> str | None: - """Device class for the sensor.""" - return self._sensor_type[3] - - @property - def icon(self) -> str | None: - """Icon for the sensor.""" - return self._sensor_type[2] - @property def extra_state_attributes(self) -> dict[str, str] | None: """Return the state attributes."""