Refactor vera into a platform.
This commit is contained in:
parent
3e3d1ae9de
commit
9fc73fa644
11 changed files with 262 additions and 253 deletions
|
@ -6,109 +6,40 @@ https://home-assistant.io/components/sensor.vera/
|
|||
"""
|
||||
import logging
|
||||
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.const import (
|
||||
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
|
||||
EVENT_HOMEASSISTANT_STOP, TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
||||
TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.components.vera import (
|
||||
VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
|
||||
|
||||
REQUIREMENTS = ['pyvera==0.2.8']
|
||||
DEPENDENCIES = ['vera']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def get_devices(hass, config):
|
||||
"""Setup the Vera Sensors."""
|
||||
import pyvera as veraApi
|
||||
|
||||
base_url = config.get('vera_controller_url')
|
||||
if not base_url:
|
||||
_LOGGER.error(
|
||||
"The required parameter 'vera_controller_url'"
|
||||
" was not found in config"
|
||||
)
|
||||
return False
|
||||
|
||||
device_data = config.get('device_data', {})
|
||||
|
||||
vera_controller, created = veraApi.init_controller(base_url)
|
||||
|
||||
if created:
|
||||
def stop_subscription(event):
|
||||
"""Shutdown Vera subscriptions and subscription thread on exit."""
|
||||
_LOGGER.info("Shutting down subscriptions.")
|
||||
vera_controller.stop()
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription)
|
||||
|
||||
categories = ['Temperature Sensor',
|
||||
'Light Sensor',
|
||||
'Humidity Sensor',
|
||||
'Sensor']
|
||||
devices = []
|
||||
try:
|
||||
devices = vera_controller.get_devices(categories)
|
||||
except RequestException:
|
||||
# There was a network related error connecting to the vera controller.
|
||||
_LOGGER.exception("Error communicating with Vera API")
|
||||
return False
|
||||
|
||||
vera_sensors = []
|
||||
for device in devices:
|
||||
extra_data = device_data.get(device.device_id, {})
|
||||
exclude = extra_data.get('exclude', False)
|
||||
|
||||
if exclude is not True:
|
||||
vera_sensors.append(
|
||||
VeraSensor(device, vera_controller, extra_data))
|
||||
|
||||
return vera_sensors
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
"""Perform the setup for Vera controller devices."""
|
||||
add_devices(get_devices(hass, config))
|
||||
add_devices_callback(
|
||||
VeraSensor(device, VERA_CONTROLLER)
|
||||
for device in VERA_DEVICES['sensor'])
|
||||
|
||||
|
||||
class VeraSensor(Entity):
|
||||
class VeraSensor(VeraDevice, Entity):
|
||||
"""Representation of a Vera Sensor."""
|
||||
|
||||
def __init__(self, vera_device, controller, extra_data=None):
|
||||
def __init__(self, vera_device, controller):
|
||||
"""Initialize the sensor."""
|
||||
self.vera_device = vera_device
|
||||
self.controller = controller
|
||||
self.extra_data = extra_data
|
||||
if self.extra_data and self.extra_data.get('name'):
|
||||
self._name = self.extra_data.get('name')
|
||||
else:
|
||||
self._name = self.vera_device.name
|
||||
self.current_value = ''
|
||||
self.current_value = None
|
||||
self._temperature_units = None
|
||||
|
||||
self.controller.register(vera_device, self._update_callback)
|
||||
self.update()
|
||||
|
||||
def _update_callback(self, _device):
|
||||
"""Called by the vera device callback to update state."""
|
||||
self.update_ha_state(True)
|
||||
|
||||
def __str__(self):
|
||||
"""String representation of sensor."""
|
||||
return "%s %s %s" % (self.name, self.vera_device.device_id, self.state)
|
||||
VeraDevice.__init__(self, vera_device, controller)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self.current_value
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the mame of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
|
@ -144,11 +75,6 @@ class VeraSensor(Entity):
|
|||
attr['Vera Device Id'] = self.vera_device.vera_device_id
|
||||
return attr
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
def update(self):
|
||||
"""Update the state."""
|
||||
if self.vera_device.category == "Temperature Sensor":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue