mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Use entity class attributes for canary (#53333)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
84dc6af760
commit
12503d548b
@ -52,6 +52,9 @@ class CanaryAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
|||||||
"""Representation of a Canary alarm control panel."""
|
"""Representation of a Canary alarm control panel."""
|
||||||
|
|
||||||
coordinator: CanaryDataUpdateCoordinator
|
coordinator: CanaryDataUpdateCoordinator
|
||||||
|
_attr_supported_features = (
|
||||||
|
SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_NIGHT
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, coordinator: CanaryDataUpdateCoordinator, location: Location
|
self, coordinator: CanaryDataUpdateCoordinator, location: Location
|
||||||
@ -59,23 +62,14 @@ class CanaryAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
|||||||
"""Initialize a Canary security camera."""
|
"""Initialize a Canary security camera."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._location_id: str = location.location_id
|
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
|
@property
|
||||||
def location(self) -> Location:
|
def location(self) -> Location:
|
||||||
"""Return information about the location."""
|
"""Return information about the location."""
|
||||||
return self.coordinator.data["locations"][self._location_id]
|
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
|
@property
|
||||||
def state(self) -> str | None:
|
def state(self) -> str | None:
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
@ -92,11 +86,6 @@ class CanaryAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
|||||||
|
|
||||||
return None
|
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
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
|
@ -21,7 +21,6 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
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.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
@ -30,7 +29,6 @@ from .const import (
|
|||||||
CONF_FFMPEG_ARGUMENTS,
|
CONF_FFMPEG_ARGUMENTS,
|
||||||
DATA_COORDINATOR,
|
DATA_COORDINATOR,
|
||||||
DEFAULT_FFMPEG_ARGUMENTS,
|
DEFAULT_FFMPEG_ARGUMENTS,
|
||||||
DEFAULT_TIMEOUT,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
MANUFACTURER,
|
MANUFACTURER,
|
||||||
)
|
)
|
||||||
@ -73,7 +71,6 @@ async def async_setup_entry(
|
|||||||
coordinator,
|
coordinator,
|
||||||
location_id,
|
location_id,
|
||||||
device,
|
device,
|
||||||
DEFAULT_TIMEOUT,
|
|
||||||
ffmpeg_arguments,
|
ffmpeg_arguments,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -92,7 +89,6 @@ class CanaryCamera(CoordinatorEntity, Camera):
|
|||||||
coordinator: CanaryDataUpdateCoordinator,
|
coordinator: CanaryDataUpdateCoordinator,
|
||||||
location_id: str,
|
location_id: str,
|
||||||
device: Device,
|
device: Device,
|
||||||
timeout: int,
|
|
||||||
ffmpeg_args: str,
|
ffmpeg_args: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Canary security camera."""
|
"""Initialize a Canary security camera."""
|
||||||
@ -102,37 +98,21 @@ class CanaryCamera(CoordinatorEntity, Camera):
|
|||||||
self._ffmpeg_arguments = ffmpeg_args
|
self._ffmpeg_arguments = ffmpeg_args
|
||||||
self._location_id = location_id
|
self._location_id = location_id
|
||||||
self._device = device
|
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._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
|
@property
|
||||||
def location(self) -> Location:
|
def location(self) -> Location:
|
||||||
"""Return information about the location."""
|
"""Return information about the location."""
|
||||||
return self.coordinator.data["locations"][self._location_id]
|
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
|
@property
|
||||||
def is_recording(self) -> bool:
|
def is_recording(self) -> bool:
|
||||||
"""Return true if the device is recording."""
|
"""Return true if the device is recording."""
|
||||||
|
@ -17,7 +17,6 @@ from homeassistant.const import (
|
|||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
@ -97,11 +96,9 @@ class CanarySensor(CoordinatorEntity, SensorEntity):
|
|||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._sensor_type = sensor_type
|
self._sensor_type = sensor_type
|
||||||
self._device_id = device.device_id
|
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()
|
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
|
canary_sensor_type = None
|
||||||
if self._sensor_type[0] == "air_quality":
|
if self._sensor_type[0] == "air_quality":
|
||||||
@ -116,6 +113,17 @@ class CanarySensor(CoordinatorEntity, SensorEntity):
|
|||||||
canary_sensor_type = SensorType.BATTERY
|
canary_sensor_type = SensorType.BATTERY
|
||||||
|
|
||||||
self._canary_type = canary_sensor_type
|
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
|
@property
|
||||||
def reading(self) -> float | None:
|
def reading(self) -> float | None:
|
||||||
@ -136,46 +144,6 @@ class CanarySensor(CoordinatorEntity, SensorEntity):
|
|||||||
|
|
||||||
return None
|
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
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, str] | None:
|
def extra_state_attributes(self) -> dict[str, str] | None:
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user