2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Logi Circle sensors."""
|
2021-08-16 22:14:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-09-21 10:00:15 +00:00
|
|
|
import logging
|
2021-08-16 22:14:00 +00:00
|
|
|
from typing import Any
|
2018-09-21 10:00:15 +00:00
|
|
|
|
2021-08-16 22:14:00 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2022-01-05 12:30:37 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2018-09-21 10:00:15 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_BATTERY_CHARGING,
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_SENSORS,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2022-01-05 12:30:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-25 21:26:40 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-05 12:30:37 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-09-21 10:00:15 +00:00
|
|
|
from homeassistant.helpers.icon import icon_for_battery_level
|
2022-01-05 12:30:37 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-09-21 10:00:15 +00:00
|
|
|
from homeassistant.util.dt import as_local
|
|
|
|
|
2021-08-16 22:14:00 +00:00
|
|
|
from .const import ATTRIBUTION, DEVICE_BRAND, DOMAIN as LOGI_CIRCLE_DOMAIN, SENSOR_TYPES
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-09-21 10:00:15 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-05 12:30:37 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2019-04-09 12:26:58 +00:00
|
|
|
"""Set up a sensor for a Logi Circle device. Obsolete."""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Logi Circle no longer works with sensor platform configuration")
|
2019-04-09 12:26:58 +00:00
|
|
|
|
|
|
|
|
2022-01-05 12:30:37 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2019-04-09 12:26:58 +00:00
|
|
|
"""Set up a Logi Circle sensor based on a config entry."""
|
|
|
|
devices = await hass.data[LOGI_CIRCLE_DOMAIN].cameras
|
2018-09-21 10:00:15 +00:00
|
|
|
time_zone = str(hass.config.time_zone)
|
|
|
|
|
2022-01-05 12:30:37 +00:00
|
|
|
monitored_conditions = entry.data[CONF_SENSORS].get(CONF_MONITORED_CONDITIONS)
|
2021-08-16 22:14:00 +00:00
|
|
|
entities = [
|
|
|
|
LogiSensor(device, time_zone, description)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key in monitored_conditions
|
|
|
|
for device in devices
|
|
|
|
if device.supports_feature(description.key)
|
|
|
|
]
|
2018-09-21 10:00:15 +00:00
|
|
|
|
2021-08-16 22:14:00 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-09-21 10:00:15 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class LogiSensor(SensorEntity):
|
2018-09-21 10:00:15 +00:00
|
|
|
"""A sensor implementation for a Logi Circle camera."""
|
|
|
|
|
2021-08-16 22:14:00 +00:00
|
|
|
def __init__(self, camera, time_zone, description: SensorEntityDescription):
|
2018-09-21 10:00:15 +00:00
|
|
|
"""Initialize a sensor for Logi Circle camera."""
|
2021-08-16 22:14:00 +00:00
|
|
|
self.entity_description = description
|
2018-09-21 10:00:15 +00:00
|
|
|
self._camera = camera
|
2021-08-16 22:14:00 +00:00
|
|
|
self._attr_unique_id = f"{camera.mac_address}-{description.key}"
|
|
|
|
self._attr_name = f"{camera.name} {description.name}"
|
|
|
|
self._activity: dict[Any, Any] = {}
|
2018-09-21 10:00:15 +00:00
|
|
|
self._tz = time_zone
|
|
|
|
|
2019-04-25 22:21:05 +00:00
|
|
|
@property
|
2021-10-25 21:26:40 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2019-04-25 22:21:05 +00:00
|
|
|
"""Return information about the device."""
|
2021-10-25 21:26:40 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(LOGI_CIRCLE_DOMAIN, self._camera.id)},
|
|
|
|
manufacturer=DEVICE_BRAND,
|
|
|
|
model=self._camera.model_name,
|
|
|
|
name=self._camera.name,
|
|
|
|
sw_version=self._camera.firmware,
|
|
|
|
)
|
2019-04-25 22:21:05 +00:00
|
|
|
|
2018-09-21 10:00:15 +00:00
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-09-21 10:00:15 +00:00
|
|
|
"""Return the state attributes."""
|
|
|
|
state = {
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2019-07-31 19:25:30 +00:00
|
|
|
"battery_saving_mode": (
|
|
|
|
STATE_ON if self._camera.battery_saving else STATE_OFF
|
|
|
|
),
|
|
|
|
"microphone_gain": self._camera.microphone_gain,
|
2018-09-21 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 22:14:00 +00:00
|
|
|
if self.entity_description.key == "battery_level":
|
2019-04-09 12:26:58 +00:00
|
|
|
state[ATTR_BATTERY_CHARGING] = self._camera.charging
|
2018-09-21 10:00:15 +00:00
|
|
|
|
|
|
|
return state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
2021-08-16 22:14:00 +00:00
|
|
|
sensor_type = self.entity_description.key
|
|
|
|
if sensor_type == "battery_level" and self._attr_native_value is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
return icon_for_battery_level(
|
2021-08-16 22:14:00 +00:00
|
|
|
battery_level=int(self._attr_native_value), charging=False
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-08-16 22:14:00 +00:00
|
|
|
if sensor_type == "recording_mode" and self._attr_native_value is not None:
|
|
|
|
return "mdi:eye" if self._attr_native_value == STATE_ON else "mdi:eye-off"
|
|
|
|
if sensor_type == "streaming_mode" and self._attr_native_value is not None:
|
|
|
|
return (
|
|
|
|
"mdi:camera"
|
|
|
|
if self._attr_native_value == STATE_ON
|
|
|
|
else "mdi:camera-off"
|
|
|
|
)
|
|
|
|
return self.entity_description.icon
|
2018-09-21 10:00:15 +00:00
|
|
|
|
2018-10-28 12:46:28 +00:00
|
|
|
async def async_update(self):
|
2018-09-21 10:00:15 +00:00
|
|
|
"""Get the latest data and updates the state."""
|
2021-08-16 22:14:00 +00:00
|
|
|
_LOGGER.debug("Pulling data from %s sensor", self.name)
|
2018-09-21 10:00:15 +00:00
|
|
|
await self._camera.update()
|
|
|
|
|
2021-08-16 22:14:00 +00:00
|
|
|
if self.entity_description.key == "last_activity_time":
|
2019-07-31 19:25:30 +00:00
|
|
|
last_activity = await self._camera.get_last_activity(force_refresh=True)
|
2018-09-21 10:00:15 +00:00
|
|
|
if last_activity is not None:
|
|
|
|
last_activity_time = as_local(last_activity.end_time_utc)
|
2021-08-16 22:14:00 +00:00
|
|
|
self._attr_native_value = (
|
2020-02-28 11:39:29 +00:00
|
|
|
f"{last_activity_time.hour:0>2}:{last_activity_time.minute:0>2}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-09-21 10:00:15 +00:00
|
|
|
else:
|
2021-08-16 22:14:00 +00:00
|
|
|
state = getattr(self._camera, self.entity_description.key, None)
|
2018-09-21 10:00:15 +00:00
|
|
|
if isinstance(state, bool):
|
2021-08-16 22:14:00 +00:00
|
|
|
self._attr_native_value = STATE_ON if state is True else STATE_OFF
|
2018-09-21 10:00:15 +00:00
|
|
|
else:
|
2021-08-16 22:14:00 +00:00
|
|
|
self._attr_native_value = state
|