Merge pull request #1550 from balloob/restructure_vera

Refactor vera into a platform.
This commit is contained in:
Greg Dowling committed 2016-03-16 09:28:27 +00:00
commit fd27e4fc7d
11 files changed
+262 -253

No files matched your search

+2 -1
View File
@@ -17,7 +17,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID)
from homeassistant.components import (
group, wemo, wink, isy994, verisure,
zwave, tellduslive, tellstick, mysensors)
zwave, tellduslive, tellstick, mysensors, vera)
DOMAIN = 'switch'
SCAN_INTERVAL = 30
@@ -42,6 +42,7 @@ DISCOVERY_PLATFORMS = {
tellduslive.DISCOVER_SWITCHES: 'tellduslive',
mysensors.DISCOVER_SWITCHES: 'mysensors',
tellstick.DISCOVER_SWITCHES: 'tellstick',
vera.DISCOVER_SWITCHES: 'vera',
}
PROP_TO_ATTR = {
+14 -83
View File
@@ -6,94 +6,33 @@ https://home-assistant.io/components/switch.vera/
"""
import logging
from requests.exceptions import RequestException
import homeassistant.util.dt as dt_util
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON)
STATE_OFF, STATE_ON)
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):
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Find and return Vera switches."""
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)
devices = []
try:
devices = vera_controller.get_devices([
'Switch', 'Armable Sensor', 'On/Off Switch'])
except RequestException:
# There was a network related error connecting to the vera controller.
_LOGGER.exception("Error communicating with Vera API")
return False
vera_switches = []
for device in devices:
extra_data = device_data.get(device.device_id, {})
exclude = extra_data.get('exclude', False)
if exclude is not True:
vera_switches.append(
VeraSwitch(device, vera_controller, extra_data))
return vera_switches
add_devices_callback(
VeraSwitch(device, VERA_CONTROLLER) for
device in VERA_DEVICES['switch'])
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Find and return Vera lights."""
add_devices(get_devices(hass, config))
class VeraSwitch(SwitchDevice):
class VeraSwitch(VeraDevice, SwitchDevice):
"""Representation of a Vera Switch."""
def __init__(self, vera_device, controller, extra_data=None):
def __init__(self, vera_device, controller):
"""Initialize the Vera device."""
self.vera_device = vera_device
self.extra_data = extra_data
self.controller = controller
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._state = STATE_OFF
self.controller.register(vera_device, self._update_callback)
self.update()
def _update_callback(self, _device):
self.update_ha_state(True)
@property
def name(self):
"""Return the mame of the switch."""
return self._name
self._state = False
VeraDevice.__init__(self, vera_device, controller)
@property
def device_state_attributes(self):
@@ -134,19 +73,11 @@ class VeraSwitch(SwitchDevice):
self._state = STATE_OFF
self.update_ha_state()
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def is_on(self):
"""Return true if device is on."""
return self._state == STATE_ON
return self._state
def update(self):
"""Called by the vera device callback to update state."""
if self.vera_device.is_switched_on():
self._state = STATE_ON
else:
self._state = STATE_OFF
self._state = self.vera_device.is_switched_on()