2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Logi Circle sensors."""
|
2018-09-21 10:00:15 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-04-09 12:26:58 +00:00
|
|
|
ATTR_ATTRIBUTION, ATTR_BATTERY_CHARGING, CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_SENSORS, STATE_OFF, STATE_ON)
|
2018-09-21 10:00:15 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.helpers.icon import icon_for_battery_level
|
|
|
|
from homeassistant.util.dt import as_local
|
|
|
|
|
2019-04-09 12:26:58 +00:00
|
|
|
from .const import (
|
2019-04-25 22:21:05 +00:00
|
|
|
ATTRIBUTION, DEVICE_BRAND, DOMAIN as LOGI_CIRCLE_DOMAIN,
|
|
|
|
LOGI_SENSORS as SENSOR_TYPES)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-09-21 10:00:15 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2019-04-09 12:26:58 +00:00
|
|
|
"""Set up a sensor for a Logi Circle device. Obsolete."""
|
|
|
|
_LOGGER.warning(
|
|
|
|
'Logi Circle no longer works with sensor platform configuration')
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""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)
|
|
|
|
|
|
|
|
sensors = []
|
2019-04-09 12:26:58 +00:00
|
|
|
for sensor_type in (entry.data.get(CONF_SENSORS)
|
|
|
|
.get(CONF_MONITORED_CONDITIONS)):
|
2018-09-21 10:00:15 +00:00
|
|
|
for device in devices:
|
|
|
|
if device.supports_feature(sensor_type):
|
|
|
|
sensors.append(LogiSensor(device, time_zone, sensor_type))
|
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
|
|
|
|
|
|
|
|
|
|
|
class LogiSensor(Entity):
|
|
|
|
"""A sensor implementation for a Logi Circle camera."""
|
|
|
|
|
|
|
|
def __init__(self, camera, time_zone, sensor_type):
|
|
|
|
"""Initialize a sensor for Logi Circle camera."""
|
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._camera = camera
|
|
|
|
self._id = '{}-{}'.format(self._camera.mac_address, self._sensor_type)
|
|
|
|
self._icon = 'mdi:{}'.format(SENSOR_TYPES.get(self._sensor_type)[2])
|
|
|
|
self._name = "{0} {1}".format(
|
|
|
|
self._camera.name, SENSOR_TYPES.get(self._sensor_type)[0])
|
2019-04-09 12:26:58 +00:00
|
|
|
self._activity = {}
|
2018-09-21 10:00:15 +00:00
|
|
|
self._state = None
|
|
|
|
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 state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
2019-04-25 22:21:05 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return information about the device."""
|
|
|
|
return {
|
|
|
|
'name': self._camera.name,
|
|
|
|
'identifiers': {
|
|
|
|
(LOGI_CIRCLE_DOMAIN, self._camera.id)
|
|
|
|
},
|
|
|
|
'model': self._camera.model_name,
|
|
|
|
'sw_version': self._camera.firmware,
|
|
|
|
'manufacturer': DEVICE_BRAND
|
|
|
|
}
|
|
|
|
|
2018-09-21 10:00:15 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
state = {
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2018-09-21 10:00:15 +00:00
|
|
|
'battery_saving_mode': (
|
|
|
|
STATE_ON if self._camera.battery_saving else STATE_OFF),
|
|
|
|
'microphone_gain': self._camera.microphone_gain
|
|
|
|
}
|
|
|
|
|
|
|
|
if self._sensor_type == '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."""
|
|
|
|
if (self._sensor_type == 'battery_level' and
|
|
|
|
self._state is not None):
|
|
|
|
return icon_for_battery_level(battery_level=int(self._state),
|
|
|
|
charging=False)
|
2019-04-09 12:26:58 +00:00
|
|
|
if (self._sensor_type == 'recording_mode' and
|
2018-09-21 10:00:15 +00:00
|
|
|
self._state is not None):
|
2019-04-09 12:26:58 +00:00
|
|
|
return 'mdi:eye' if self._state == STATE_ON else 'mdi:eye-off'
|
2018-09-21 10:00:15 +00:00
|
|
|
if (self._sensor_type == 'streaming_mode' and
|
|
|
|
self._state is not None):
|
|
|
|
return (
|
|
|
|
'mdi:camera' if self._state == STATE_ON else 'mdi:camera-off')
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the units of measurement."""
|
|
|
|
return SENSOR_TYPES.get(self._sensor_type)[1]
|
|
|
|
|
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."""
|
|
|
|
_LOGGER.debug("Pulling data from %s sensor", self._name)
|
|
|
|
await self._camera.update()
|
|
|
|
|
|
|
|
if self._sensor_type == 'last_activity_time':
|
2019-04-09 12:26:58 +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)
|
|
|
|
self._state = '{0:0>2}:{1:0>2}'.format(
|
|
|
|
last_activity_time.hour, last_activity_time.minute)
|
|
|
|
else:
|
|
|
|
state = getattr(self._camera, self._sensor_type, None)
|
|
|
|
if isinstance(state, bool):
|
|
|
|
self._state = STATE_ON if state is True else STATE_OFF
|
|
|
|
else:
|
|
|
|
self._state = state
|
2019-04-09 12:26:58 +00:00
|
|
|
self._state = state
|