mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Use EntityDescription - logi_circle (#54429)
This commit is contained in:
parent
0abcfb42b3
commit
38a210292f
@ -29,8 +29,8 @@ from .const import (
|
|||||||
DEFAULT_CACHEDB,
|
DEFAULT_CACHEDB,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
LED_MODE_KEY,
|
LED_MODE_KEY,
|
||||||
LOGI_SENSORS,
|
|
||||||
RECORDING_MODE_KEY,
|
RECORDING_MODE_KEY,
|
||||||
|
SENSOR_TYPES,
|
||||||
SIGNAL_LOGI_CIRCLE_RECONFIGURE,
|
SIGNAL_LOGI_CIRCLE_RECONFIGURE,
|
||||||
SIGNAL_LOGI_CIRCLE_RECORD,
|
SIGNAL_LOGI_CIRCLE_RECORD,
|
||||||
SIGNAL_LOGI_CIRCLE_SNAPSHOT,
|
SIGNAL_LOGI_CIRCLE_SNAPSHOT,
|
||||||
@ -50,10 +50,12 @@ ATTR_DURATION = "duration"
|
|||||||
|
|
||||||
PLATFORMS = ["camera", "sensor"]
|
PLATFORMS = ["camera", "sensor"]
|
||||||
|
|
||||||
|
SENSOR_KEYS = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
SENSOR_SCHEMA = vol.Schema(
|
SENSOR_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(LOGI_SENSORS)): vol.All(
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
|
||||||
cv.ensure_list, [vol.In(LOGI_SENSORS)]
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
"""Constants in Logi Circle component."""
|
"""Constants in Logi Circle component."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntityDescription
|
||||||
from homeassistant.const import PERCENTAGE
|
from homeassistant.const import PERCENTAGE
|
||||||
|
|
||||||
DOMAIN = "logi_circle"
|
DOMAIN = "logi_circle"
|
||||||
@ -12,15 +15,40 @@ DEFAULT_CACHEDB = ".logi_cache.pickle"
|
|||||||
LED_MODE_KEY = "LED"
|
LED_MODE_KEY = "LED"
|
||||||
RECORDING_MODE_KEY = "RECORDING_MODE"
|
RECORDING_MODE_KEY = "RECORDING_MODE"
|
||||||
|
|
||||||
# Sensor types: Name, unit of measure, icon per sensor key.
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
LOGI_SENSORS = {
|
SensorEntityDescription(
|
||||||
"battery_level": ["Battery", PERCENTAGE, "battery-50"],
|
key="battery_level",
|
||||||
"last_activity_time": ["Last Activity", None, "history"],
|
name="Battery",
|
||||||
"recording": ["Recording Mode", None, "eye"],
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
"signal_strength_category": ["WiFi Signal Category", None, "wifi"],
|
icon="mdi:battery-50",
|
||||||
"signal_strength_percentage": ["WiFi Signal Strength", PERCENTAGE, "wifi"],
|
),
|
||||||
"streaming": ["Streaming Mode", None, "camera"],
|
SensorEntityDescription(
|
||||||
}
|
key="last_activity_time",
|
||||||
|
name="Last Activity",
|
||||||
|
icon="mdi:history",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="recording",
|
||||||
|
name="Recording Mode",
|
||||||
|
icon="mdi:eye",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="signal_strength_category",
|
||||||
|
name="WiFi Signal Category",
|
||||||
|
icon="mdi:wifi",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="signal_strength_percentage",
|
||||||
|
name="WiFi Signal Strength",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
icon="mdi:wifi",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="streaming",
|
||||||
|
name="Streaming Mode",
|
||||||
|
icon="mdi:camera",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
SIGNAL_LOGI_CIRCLE_RECONFIGURE = "logi_circle_reconfigure"
|
SIGNAL_LOGI_CIRCLE_RECONFIGURE = "logi_circle_reconfigure"
|
||||||
SIGNAL_LOGI_CIRCLE_SNAPSHOT = "logi_circle_snapshot"
|
SIGNAL_LOGI_CIRCLE_SNAPSHOT = "logi_circle_snapshot"
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
"""Support for Logi Circle sensors."""
|
"""Support for Logi Circle sensors."""
|
||||||
import logging
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
ATTR_BATTERY_CHARGING,
|
ATTR_BATTERY_CHARGING,
|
||||||
@ -13,12 +16,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.helpers.icon import icon_for_battery_level
|
from homeassistant.helpers.icon import icon_for_battery_level
|
||||||
from homeassistant.util.dt import as_local
|
from homeassistant.util.dt import as_local
|
||||||
|
|
||||||
from .const import (
|
from .const import ATTRIBUTION, DEVICE_BRAND, DOMAIN as LOGI_CIRCLE_DOMAIN, SENSOR_TYPES
|
||||||
ATTRIBUTION,
|
|
||||||
DEVICE_BRAND,
|
|
||||||
DOMAIN as LOGI_CIRCLE_DOMAIN,
|
|
||||||
LOGI_SENSORS as SENSOR_TYPES,
|
|
||||||
)
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -33,44 +31,30 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
devices = await hass.data[LOGI_CIRCLE_DOMAIN].cameras
|
devices = await hass.data[LOGI_CIRCLE_DOMAIN].cameras
|
||||||
time_zone = str(hass.config.time_zone)
|
time_zone = str(hass.config.time_zone)
|
||||||
|
|
||||||
sensors = []
|
monitored_conditions = entry.data.get(CONF_SENSORS).get(CONF_MONITORED_CONDITIONS)
|
||||||
for sensor_type in entry.data.get(CONF_SENSORS).get(CONF_MONITORED_CONDITIONS):
|
entities = [
|
||||||
for device in devices:
|
LogiSensor(device, time_zone, description)
|
||||||
if device.supports_feature(sensor_type):
|
for description in SENSOR_TYPES
|
||||||
sensors.append(LogiSensor(device, time_zone, sensor_type))
|
if description.key in monitored_conditions
|
||||||
|
for device in devices
|
||||||
|
if device.supports_feature(description.key)
|
||||||
|
]
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class LogiSensor(SensorEntity):
|
class LogiSensor(SensorEntity):
|
||||||
"""A sensor implementation for a Logi Circle camera."""
|
"""A sensor implementation for a Logi Circle camera."""
|
||||||
|
|
||||||
def __init__(self, camera, time_zone, sensor_type):
|
def __init__(self, camera, time_zone, description: SensorEntityDescription):
|
||||||
"""Initialize a sensor for Logi Circle camera."""
|
"""Initialize a sensor for Logi Circle camera."""
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
self._camera = camera
|
self._camera = camera
|
||||||
self._id = f"{self._camera.mac_address}-{self._sensor_type}"
|
self._attr_unique_id = f"{camera.mac_address}-{description.key}"
|
||||||
self._icon = f"mdi:{SENSOR_TYPES.get(self._sensor_type)[2]}"
|
self._attr_name = f"{camera.name} {description.name}"
|
||||||
self._name = f"{self._camera.name} {SENSOR_TYPES.get(self._sensor_type)[0]}"
|
self._activity: dict[Any, Any] = {}
|
||||||
self._activity = {}
|
|
||||||
self._state = None
|
|
||||||
self._tz = time_zone
|
self._tz = time_zone
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique ID."""
|
|
||||||
return self._id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return information about the device."""
|
"""Return information about the device."""
|
||||||
@ -93,7 +77,7 @@ class LogiSensor(SensorEntity):
|
|||||||
"microphone_gain": self._camera.microphone_gain,
|
"microphone_gain": self._camera.microphone_gain,
|
||||||
}
|
}
|
||||||
|
|
||||||
if self._sensor_type == "battery_level":
|
if self.entity_description.key == "battery_level":
|
||||||
state[ATTR_BATTERY_CHARGING] = self._camera.charging
|
state[ATTR_BATTERY_CHARGING] = self._camera.charging
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@ -101,37 +85,36 @@ class LogiSensor(SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Icon to use in the frontend, if any."""
|
"""Icon to use in the frontend, if any."""
|
||||||
if self._sensor_type == "battery_level" and self._state is not None:
|
sensor_type = self.entity_description.key
|
||||||
|
if sensor_type == "battery_level" and self._attr_native_value is not None:
|
||||||
return icon_for_battery_level(
|
return icon_for_battery_level(
|
||||||
battery_level=int(self._state), charging=False
|
battery_level=int(self._attr_native_value), charging=False
|
||||||
)
|
)
|
||||||
if self._sensor_type == "recording_mode" and self._state is not None:
|
if sensor_type == "recording_mode" and self._attr_native_value is not None:
|
||||||
return "mdi:eye" if self._state == STATE_ON else "mdi:eye-off"
|
return "mdi:eye" if self._attr_native_value == STATE_ON else "mdi:eye-off"
|
||||||
if self._sensor_type == "streaming_mode" and self._state is not None:
|
if sensor_type == "streaming_mode" and self._attr_native_value is not None:
|
||||||
return "mdi:camera" if self._state == STATE_ON else "mdi:camera-off"
|
return (
|
||||||
return self._icon
|
"mdi:camera"
|
||||||
|
if self._attr_native_value == STATE_ON
|
||||||
@property
|
else "mdi:camera-off"
|
||||||
def native_unit_of_measurement(self):
|
)
|
||||||
"""Return the units of measurement."""
|
return self.entity_description.icon
|
||||||
return SENSOR_TYPES.get(self._sensor_type)[1]
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Get the latest data and updates the state."""
|
"""Get the latest data and updates the state."""
|
||||||
_LOGGER.debug("Pulling data from %s sensor", self._name)
|
_LOGGER.debug("Pulling data from %s sensor", self.name)
|
||||||
await self._camera.update()
|
await self._camera.update()
|
||||||
|
|
||||||
if self._sensor_type == "last_activity_time":
|
if self.entity_description.key == "last_activity_time":
|
||||||
last_activity = await self._camera.get_last_activity(force_refresh=True)
|
last_activity = await self._camera.get_last_activity(force_refresh=True)
|
||||||
if last_activity is not None:
|
if last_activity is not None:
|
||||||
last_activity_time = as_local(last_activity.end_time_utc)
|
last_activity_time = as_local(last_activity.end_time_utc)
|
||||||
self._state = (
|
self._attr_native_value = (
|
||||||
f"{last_activity_time.hour:0>2}:{last_activity_time.minute:0>2}"
|
f"{last_activity_time.hour:0>2}:{last_activity_time.minute:0>2}"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
state = getattr(self._camera, self._sensor_type, None)
|
state = getattr(self._camera, self.entity_description.key, None)
|
||||||
if isinstance(state, bool):
|
if isinstance(state, bool):
|
||||||
self._state = STATE_ON if state is True else STATE_OFF
|
self._attr_native_value = STATE_ON if state is True else STATE_OFF
|
||||||
else:
|
else:
|
||||||
self._state = state
|
self._attr_native_value = state
|
||||||
self._state = state
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user