2016-03-15 09:17:09 +00:00
|
|
|
"""
|
|
|
|
Support for Vera devices.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/vera/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from collections import defaultdict
|
2016-06-12 00:43:13 +00:00
|
|
|
|
2016-08-19 21:43:40 +00:00
|
|
|
import voluptuous as vol
|
2016-03-15 09:17:09 +00:00
|
|
|
|
2016-08-19 21:43:40 +00:00
|
|
|
from requests.exceptions import RequestException
|
2016-07-20 02:13:33 +00:00
|
|
|
|
|
|
|
from homeassistant.util.dt import utc_from_timestamp
|
2017-03-11 18:06:46 +00:00
|
|
|
from homeassistant.util import (convert, slugify)
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2016-08-19 21:43:40 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2016-07-20 02:13:33 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
|
|
|
|
EVENT_HOMEASSISTANT_STOP)
|
2016-03-15 09:17:09 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2017-07-20 09:21:08 +00:00
|
|
|
REQUIREMENTS = ['pyvera==0.2.35']
|
2016-03-15 09:17:09 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'vera'
|
|
|
|
|
|
|
|
VERA_CONTROLLER = None
|
|
|
|
|
2016-08-19 21:43:40 +00:00
|
|
|
CONF_CONTROLLER = 'vera_controller_url'
|
2016-03-15 09:17:09 +00:00
|
|
|
CONF_EXCLUDE = 'exclude'
|
|
|
|
CONF_LIGHTS = 'lights'
|
|
|
|
|
2017-03-11 18:06:46 +00:00
|
|
|
VERA_ID_FORMAT = '{}_{}'
|
|
|
|
|
2017-03-19 21:02:11 +00:00
|
|
|
ATTR_CURRENT_POWER_W = "current_power_w"
|
2017-04-18 10:01:23 +00:00
|
|
|
ATTR_CURRENT_ENERGY_KWH = "current_energy_kwh"
|
2016-03-15 09:17:09 +00:00
|
|
|
|
|
|
|
VERA_DEVICES = defaultdict(list)
|
|
|
|
|
2016-08-19 21:43:40 +00:00
|
|
|
VERA_ID_LIST_SCHEMA = vol.Schema([int])
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_CONTROLLER): cv.url,
|
|
|
|
vol.Optional(CONF_EXCLUDE, default=[]): VERA_ID_LIST_SCHEMA,
|
2016-09-14 04:36:49 +00:00
|
|
|
vol.Optional(CONF_LIGHTS, default=[]): VERA_ID_LIST_SCHEMA
|
2016-08-19 21:43:40 +00:00
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2016-09-14 04:36:49 +00:00
|
|
|
VERA_COMPONENTS = [
|
2016-09-15 18:47:03 +00:00
|
|
|
'binary_sensor', 'sensor', 'light', 'switch', 'lock', 'climate', 'cover'
|
2016-09-14 04:36:49 +00:00
|
|
|
]
|
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument, too-many-function-args
|
|
|
|
def setup(hass, base_config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up for Vera devices."""
|
2016-03-15 09:17:09 +00:00
|
|
|
global VERA_CONTROLLER
|
|
|
|
import pyvera as veraApi
|
|
|
|
|
|
|
|
def stop_subscription(event):
|
|
|
|
"""Shutdown Vera subscriptions and subscription thread on exit."""
|
2017-06-13 09:10:32 +00:00
|
|
|
_LOGGER.info("Shutting down subscriptions")
|
2016-03-15 09:17:09 +00:00
|
|
|
VERA_CONTROLLER.stop()
|
|
|
|
|
2017-02-27 18:57:39 +00:00
|
|
|
config = base_config.get(DOMAIN)
|
|
|
|
|
|
|
|
# Get Vera specific configuration.
|
|
|
|
base_url = config.get(CONF_CONTROLLER)
|
|
|
|
light_ids = config.get(CONF_LIGHTS)
|
|
|
|
exclude_ids = config.get(CONF_EXCLUDE)
|
|
|
|
|
|
|
|
# Initialize the Vera controller.
|
|
|
|
VERA_CONTROLLER, _ = veraApi.init_controller(base_url)
|
2016-03-15 09:17:09 +00:00
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription)
|
|
|
|
|
|
|
|
try:
|
2016-07-20 02:13:33 +00:00
|
|
|
all_devices = VERA_CONTROLLER.get_devices()
|
2016-03-15 09:17:09 +00:00
|
|
|
except RequestException:
|
2017-02-27 18:57:39 +00:00
|
|
|
# There was a network related error connecting to the Vera controller.
|
2016-03-15 09:17:09 +00:00
|
|
|
_LOGGER.exception("Error communicating with Vera API")
|
|
|
|
return False
|
|
|
|
|
2017-02-27 18:57:39 +00:00
|
|
|
# Exclude devices unwanted by user.
|
|
|
|
devices = [device for device in all_devices
|
|
|
|
if device.device_id not in exclude_ids]
|
2016-03-15 09:17:09 +00:00
|
|
|
|
2017-02-27 18:57:39 +00:00
|
|
|
for device in devices:
|
|
|
|
device_type = map_vera_device(device, light_ids)
|
|
|
|
if device_type is None:
|
2016-03-15 09:17:09 +00:00
|
|
|
continue
|
2017-02-27 18:57:39 +00:00
|
|
|
|
|
|
|
VERA_DEVICES[device_type].append(device)
|
2016-03-15 09:17:09 +00:00
|
|
|
|
2016-09-14 04:36:49 +00:00
|
|
|
for component in VERA_COMPONENTS:
|
2016-06-15 05:51:46 +00:00
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, base_config)
|
2016-06-12 00:43:13 +00:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-07-20 02:13:33 +00:00
|
|
|
def map_vera_device(vera_device, remap):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Map vera classes to Home Assistant types."""
|
2016-07-20 02:13:33 +00:00
|
|
|
import pyvera as veraApi
|
|
|
|
if isinstance(vera_device, veraApi.VeraDimmer):
|
|
|
|
return 'light'
|
|
|
|
if isinstance(vera_device, veraApi.VeraBinarySensor):
|
|
|
|
return 'binary_sensor'
|
|
|
|
if isinstance(vera_device, veraApi.VeraSensor):
|
|
|
|
return 'sensor'
|
|
|
|
if isinstance(vera_device, veraApi.VeraArmableDevice):
|
|
|
|
return 'switch'
|
|
|
|
if isinstance(vera_device, veraApi.VeraLock):
|
|
|
|
return 'lock'
|
2016-09-14 04:36:49 +00:00
|
|
|
if isinstance(vera_device, veraApi.VeraThermostat):
|
|
|
|
return 'climate'
|
2016-09-15 18:47:03 +00:00
|
|
|
if isinstance(vera_device, veraApi.VeraCurtain):
|
|
|
|
return 'cover'
|
2017-04-25 07:17:25 +00:00
|
|
|
if isinstance(vera_device, veraApi.VeraSceneController):
|
|
|
|
return 'sensor'
|
2016-07-20 02:13:33 +00:00
|
|
|
if isinstance(vera_device, veraApi.VeraSwitch):
|
|
|
|
if vera_device.device_id in remap:
|
|
|
|
return 'light'
|
2017-07-06 03:02:16 +00:00
|
|
|
return 'switch'
|
2016-07-20 02:13:33 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
class VeraDevice(Entity):
|
2017-02-27 18:57:39 +00:00
|
|
|
"""Representation of a Vera device entity."""
|
2016-03-15 09:17:09 +00:00
|
|
|
|
|
|
|
def __init__(self, vera_device, controller):
|
|
|
|
"""Initialize the device."""
|
|
|
|
self.vera_device = vera_device
|
|
|
|
self.controller = controller
|
2017-02-27 18:57:39 +00:00
|
|
|
|
2017-03-11 18:06:46 +00:00
|
|
|
self._name = self.vera_device.name
|
2017-02-27 18:57:39 +00:00
|
|
|
# Append device id to prevent name clashes in HA.
|
2017-03-11 18:06:46 +00:00
|
|
|
self.vera_id = VERA_ID_FORMAT.format(
|
|
|
|
slugify(vera_device.name), vera_device.device_id)
|
2016-03-15 09:17:09 +00:00
|
|
|
|
|
|
|
self.controller.register(vera_device, self._update_callback)
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def _update_callback(self, _device):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Update the state."""
|
2016-11-17 15:34:46 +00:00
|
|
|
self.update()
|
|
|
|
self.schedule_update_ha_state()
|
2016-03-15 09:17:09 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2017-04-25 07:17:25 +00:00
|
|
|
"""Get polling requirement from vera device."""
|
|
|
|
return self.vera_device.should_poll
|
2016-07-20 02:13:33 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
attr = {}
|
|
|
|
|
|
|
|
if self.vera_device.has_battery:
|
2017-06-17 08:38:15 +00:00
|
|
|
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level
|
2016-07-20 02:13:33 +00:00
|
|
|
|
|
|
|
if self.vera_device.is_armable:
|
|
|
|
armed = self.vera_device.is_armed
|
|
|
|
attr[ATTR_ARMED] = 'True' if armed else 'False'
|
|
|
|
|
|
|
|
if self.vera_device.is_trippable:
|
|
|
|
last_tripped = self.vera_device.last_trip
|
|
|
|
if last_tripped is not None:
|
|
|
|
utc_time = utc_from_timestamp(int(last_tripped))
|
|
|
|
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
|
|
|
|
else:
|
|
|
|
attr[ATTR_LAST_TRIP_TIME] = None
|
|
|
|
tripped = self.vera_device.is_tripped
|
|
|
|
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
|
|
|
|
|
|
|
|
power = self.vera_device.power
|
|
|
|
if power:
|
2017-03-19 21:02:11 +00:00
|
|
|
attr[ATTR_CURRENT_POWER_W] = convert(power, float, 0.0)
|
2016-07-20 02:13:33 +00:00
|
|
|
|
2017-04-18 10:01:23 +00:00
|
|
|
energy = self.vera_device.energy
|
|
|
|
if energy:
|
|
|
|
attr[ATTR_CURRENT_ENERGY_KWH] = convert(energy, float, 0.0)
|
|
|
|
|
2016-07-20 02:13:33 +00:00
|
|
|
attr['Vera Device Id'] = self.vera_device.vera_device_id
|
|
|
|
|
|
|
|
return attr
|