2019-02-13 20:21:14 +00:00
|
|
|
"""Sensor support for Netgear Arlo IP cameras."""
|
2017-05-31 07:25:25 +00:00
|
|
|
import logging
|
2017-10-07 08:59:46 +00:00
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2018-07-06 08:26:03 +00:00
|
|
|
from homeassistant.const import (
|
2019-03-21 05:56:46 +00:00
|
|
|
ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS, DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS)
|
|
|
|
from homeassistant.core import callback
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-06-12 06:01:26 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-05-31 07:25:25 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-10-08 08:05:41 +00:00
|
|
|
from homeassistant.helpers.icon import icon_for_battery_level
|
2017-05-31 07:25:25 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import ATTRIBUTION, DATA_ARLO, DEFAULT_BRAND, SIGNAL_UPDATE_ARLO
|
|
|
|
|
2017-10-07 08:59:46 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
# sensor_type [ description, unit, icon ]
|
|
|
|
SENSOR_TYPES = {
|
|
|
|
'last_capture': ['Last', None, 'run-fast'],
|
|
|
|
'total_cameras': ['Arlo Cameras', None, 'video'],
|
|
|
|
'captured_today': ['Captured Today', None, 'file-video'],
|
2017-11-15 22:33:50 +00:00
|
|
|
'battery_level': ['Battery Level', '%', 'battery-50'],
|
2018-07-06 08:26:03 +00:00
|
|
|
'signal_strength': ['Signal Strength', None, 'signal'],
|
|
|
|
'temperature': ['Temperature', TEMP_CELSIUS, 'thermometer'],
|
|
|
|
'humidity': ['Humidity', '%', 'water-percent'],
|
|
|
|
'air_quality': ['Air Quality', 'ppm', 'biohazard']
|
2017-05-31 07:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)):
|
|
|
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-31 07:25:25 +00:00
|
|
|
"""Set up an Arlo IP sensor."""
|
2017-07-04 08:06:46 +00:00
|
|
|
arlo = hass.data.get(DATA_ARLO)
|
2017-05-31 07:25:25 +00:00
|
|
|
if not arlo:
|
2018-07-06 08:26:03 +00:00
|
|
|
return
|
2017-05-31 07:25:25 +00:00
|
|
|
|
|
|
|
sensors = []
|
|
|
|
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
|
|
|
|
if sensor_type == 'total_cameras':
|
2017-10-07 08:59:46 +00:00
|
|
|
sensors.append(ArloSensor(
|
2018-06-12 06:01:26 +00:00
|
|
|
SENSOR_TYPES[sensor_type][0], arlo, sensor_type))
|
2017-05-31 07:25:25 +00:00
|
|
|
else:
|
|
|
|
for camera in arlo.cameras:
|
2018-07-17 17:34:29 +00:00
|
|
|
if sensor_type in ('temperature', 'humidity', 'air_quality'):
|
2018-07-06 08:26:03 +00:00
|
|
|
continue
|
|
|
|
|
2017-10-07 08:59:46 +00:00
|
|
|
name = '{0} {1}'.format(
|
|
|
|
SENSOR_TYPES[sensor_type][0], camera.name)
|
2018-06-12 06:01:26 +00:00
|
|
|
sensors.append(ArloSensor(name, camera, sensor_type))
|
2017-05-31 07:25:25 +00:00
|
|
|
|
2018-07-06 08:26:03 +00:00
|
|
|
for base_station in arlo.base_stations:
|
2018-07-17 17:34:29 +00:00
|
|
|
if sensor_type in ('temperature', 'humidity', 'air_quality') \
|
|
|
|
and base_station.model_id == 'ABC1000':
|
2018-07-06 08:26:03 +00:00
|
|
|
name = '{0} {1}'.format(
|
|
|
|
SENSOR_TYPES[sensor_type][0], base_station.name)
|
|
|
|
sensors.append(ArloSensor(name, base_station, sensor_type))
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors, True)
|
2017-05-31 07:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ArloSensor(Entity):
|
|
|
|
"""An implementation of a Netgear Arlo IP sensor."""
|
|
|
|
|
2018-06-12 06:01:26 +00:00
|
|
|
def __init__(self, name, device, sensor_type):
|
2017-05-31 07:25:25 +00:00
|
|
|
"""Initialize an Arlo sensor."""
|
2018-07-06 08:26:03 +00:00
|
|
|
_LOGGER.debug('ArloSensor created for %s', name)
|
2017-05-31 07:25:25 +00:00
|
|
|
self._name = name
|
|
|
|
self._data = device
|
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
self._icon = 'mdi:{}'.format(SENSOR_TYPES.get(self._sensor_type)[2])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this camera."""
|
|
|
|
return self._name
|
|
|
|
|
2018-06-12 06:01:26 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_UPDATE_ARLO, self._update_callback)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
2017-10-08 08:05:41 +00:00
|
|
|
if self._sensor_type == 'battery_level' and self._state is not None:
|
|
|
|
return icon_for_battery_level(battery_level=int(self._state),
|
|
|
|
charging=False)
|
2017-05-31 07:25:25 +00:00
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the units of measurement."""
|
|
|
|
return SENSOR_TYPES.get(self._sensor_type)[1]
|
|
|
|
|
2018-07-06 08:26:03 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
if self._sensor_type == 'temperature':
|
|
|
|
return DEVICE_CLASS_TEMPERATURE
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._sensor_type == 'humidity':
|
2018-07-06 08:26:03 +00:00
|
|
|
return DEVICE_CLASS_HUMIDITY
|
|
|
|
return None
|
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the state."""
|
2018-06-12 06:01:26 +00:00
|
|
|
_LOGGER.debug("Updating Arlo sensor %s", self.name)
|
2017-05-31 07:25:25 +00:00
|
|
|
if self._sensor_type == 'total_cameras':
|
|
|
|
self._state = len(self._data.cameras)
|
|
|
|
|
|
|
|
elif self._sensor_type == 'captured_today':
|
|
|
|
self._state = len(self._data.captured_today)
|
|
|
|
|
|
|
|
elif self._sensor_type == 'last_capture':
|
|
|
|
try:
|
2018-06-12 06:01:26 +00:00
|
|
|
video = self._data.last_video
|
2017-05-31 07:25:25 +00:00
|
|
|
self._state = video.created_at_pretty("%m-%d-%Y %H:%M:%S")
|
|
|
|
except (AttributeError, IndexError):
|
2018-06-12 06:01:26 +00:00
|
|
|
error_msg = \
|
|
|
|
'Video not found for {0}. Older than {1} days?'.format(
|
|
|
|
self.name, self._data.min_days_vdo_cache)
|
|
|
|
_LOGGER.debug(error_msg)
|
2017-10-02 10:38:55 +00:00
|
|
|
self._state = None
|
|
|
|
|
|
|
|
elif self._sensor_type == 'battery_level':
|
|
|
|
try:
|
2017-11-15 22:33:50 +00:00
|
|
|
self._state = self._data.battery_level
|
|
|
|
except TypeError:
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
elif self._sensor_type == 'signal_strength':
|
|
|
|
try:
|
|
|
|
self._state = self._data.signal_strength
|
2017-10-02 10:38:55 +00:00
|
|
|
except TypeError:
|
|
|
|
self._state = None
|
2017-05-31 07:25:25 +00:00
|
|
|
|
2018-07-06 08:26:03 +00:00
|
|
|
elif self._sensor_type == 'temperature':
|
|
|
|
try:
|
|
|
|
self._state = self._data.ambient_temperature
|
|
|
|
except TypeError:
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
elif self._sensor_type == 'humidity':
|
|
|
|
try:
|
|
|
|
self._state = self._data.ambient_humidity
|
|
|
|
except TypeError:
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
elif self._sensor_type == 'air_quality':
|
|
|
|
try:
|
|
|
|
self._state = self._data.ambient_air_quality
|
|
|
|
except TypeError:
|
|
|
|
self._state = None
|
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
2017-10-07 08:59:46 +00:00
|
|
|
"""Return the device state attributes."""
|
2017-05-31 07:25:25 +00:00
|
|
|
attrs = {}
|
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
attrs[ATTR_ATTRIBUTION] = ATTRIBUTION
|
2017-05-31 07:25:25 +00:00
|
|
|
attrs['brand'] = DEFAULT_BRAND
|
|
|
|
|
2018-07-06 08:26:03 +00:00
|
|
|
if self._sensor_type != 'total_cameras':
|
2017-05-31 07:25:25 +00:00
|
|
|
attrs['model'] = self._data.model_id
|
|
|
|
|
|
|
|
return attrs
|