Use entity class attributes for canary (#53333)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Robert Hillis 2021-07-22 16:40:32 -04:00 committed by GitHub
parent 84dc6af760
commit 12503d548b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 88 deletions

View file

@ -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."""

View file

@ -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."""

View file

@ -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."""