Refactored Arlo component and enhanced Arlo API queries and times (#14823)

* start arlo refactoring

* Refactored Arlo Hub to avoid uncessary and duplicated GETs to Arlo API

* Refactored Arlo camera component to avoid duplicate queries

* Added debug and error messages when video is not found

* Transformed Arlo Control Panel to Sync

* Makes linter happy

* Uses total_seconds() for scan_interval

* Added callback and fixed scan_interval issue

* Disable multiple tries and supported custom modes set in Arlo

* Bump PyArlo version to 0.1.4

* Makes lint happy

* Removed ArloHub object and added some tweaks

* Fixed hub_refresh method

* Makes lint happy

* Ajusted async syntax and added callbacks decorators

* Bump PyArlo version to 0.1.6 to include some enhacements

* Refined code
This commit is contained in:
Marcelo Moreira de Mello authored and Martin Hjelmare committed 2018-06-12 08:01:26 +02:00
1 parent be4776d039
commit cdc5388dc9
5 files changed
+116 -84

No files matched your search

+24 -26
View File
@@ -4,17 +4,17 @@ This component provides HA sensor for Netgear Arlo IP cameras.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.arlo/
"""
import asyncio
import logging
from datetime import timedelta
import voluptuous as vol
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.components.arlo import (
CONF_ATTRIBUTION, DEFAULT_BRAND, DATA_ARLO)
CONF_ATTRIBUTION, DEFAULT_BRAND, DATA_ARLO, SIGNAL_UPDATE_ARLO)
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.icon import icon_for_battery_level
@@ -22,8 +22,6 @@ _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['arlo']
SCAN_INTERVAL = timedelta(seconds=90)
# sensor_type [ description, unit, icon ]
SENSOR_TYPES = {
'last_capture': ['Last', None, 'run-fast'],
@@ -39,8 +37,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
})
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up an Arlo IP sensor."""
arlo = hass.data.get(DATA_ARLO)
if not arlo:
@@ -50,24 +47,22 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
if sensor_type == 'total_cameras':
sensors.append(ArloSensor(
hass, SENSOR_TYPES[sensor_type][0], arlo, sensor_type))
SENSOR_TYPES[sensor_type][0], arlo, sensor_type))
else:
for camera in arlo.cameras:
name = '{0} {1}'.format(
SENSOR_TYPES[sensor_type][0], camera.name)
sensors.append(ArloSensor(hass, name, camera, sensor_type))
sensors.append(ArloSensor(name, camera, sensor_type))
async_add_devices(sensors, True)
add_devices(sensors, True)
class ArloSensor(Entity):
"""An implementation of a Netgear Arlo IP sensor."""
def __init__(self, hass, name, device, sensor_type):
def __init__(self, name, device, sensor_type):
"""Initialize an Arlo sensor."""
super().__init__()
self._name = name
self._hass = hass
self._data = device
self._sensor_type = sensor_type
self._state = None
@@ -78,6 +73,16 @@ class ArloSensor(Entity):
"""Return the name of this camera."""
return self._name
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)
@property
def state(self):
"""Return the state of the sensor."""
@@ -98,18 +103,7 @@ class ArloSensor(Entity):
def update(self):
"""Get the latest data and updates the state."""
try:
base_station = self._data.base_station
except (AttributeError, IndexError):
return
if not base_station:
return
base_station.refresh_rate = SCAN_INTERVAL.total_seconds()
self._data.update()
_LOGGER.debug("Updating Arlo sensor %s", self.name)
if self._sensor_type == 'total_cameras':
self._state = len(self._data.cameras)
@@ -118,9 +112,13 @@ class ArloSensor(Entity):
elif self._sensor_type == 'last_capture':
try:
video = self._data.videos()[0]
video = self._data.last_video
self._state = video.created_at_pretty("%m-%d-%Y %H:%M:%S")
except (AttributeError, IndexError):
error_msg = \
'Video not found for {0}. Older than {1} days?'.format(
self.name, self._data.min_days_vdo_cache)
_LOGGER.debug(error_msg)
self._state = None
elif self._sensor_type == 'battery_level':