Added Vera scenes (#10424)

* Added Vera scenes

* Fixed flake8 issues

* Fixed comments

* Moved vera to use hass.data

* Made requested changes
pull/10940/merge
Alan Fischer 2017-12-06 23:47:19 -07:00 committed by Martin Hjelmare
parent c952f2e18a
commit 39d33c97ff
10 changed files with 95 additions and 23 deletions

View File

@ -19,8 +19,8 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Perform the setup for Vera controller devices."""
add_devices(
VeraBinarySensor(device, VERA_CONTROLLER)
for device in VERA_DEVICES['binary_sensor'])
VeraBinarySensor(device, hass.data[VERA_CONTROLLER])
for device in hass.data[VERA_DEVICES]['binary_sensor'])
class VeraBinarySensor(VeraDevice, BinarySensorDevice):

View File

@ -32,8 +32,8 @@ SUPPORT_FLAGS = (SUPPORT_TARGET_TEMPERATURE | SUPPORT_OPERATION_MODE |
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Set up of Vera thermostats."""
add_devices_callback(
VeraThermostat(device, VERA_CONTROLLER) for
device in VERA_DEVICES['climate'])
VeraThermostat(device, hass.data[VERA_CONTROLLER]) for
device in hass.data[VERA_DEVICES]['climate'])
class VeraThermostat(VeraDevice, ClimateDevice):

View File

@ -18,8 +18,8 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera covers."""
add_devices(
VeraCover(device, VERA_CONTROLLER) for
device in VERA_DEVICES['cover'])
VeraCover(device, hass.data[VERA_CONTROLLER]) for
device in hass.data[VERA_DEVICES]['cover'])
class VeraCover(VeraDevice, CoverDevice):

View File

@ -21,7 +21,8 @@ DEPENDENCIES = ['vera']
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera lights."""
add_devices(
VeraLight(device, VERA_CONTROLLER) for device in VERA_DEVICES['light'])
VeraLight(device, hass.data[VERA_CONTROLLER]) for
device in hass.data[VERA_DEVICES]['light'])
class VeraLight(VeraDevice, Light):

View File

@ -19,8 +19,8 @@ DEPENDENCIES = ['vera']
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Find and return Vera locks."""
add_devices(
VeraLock(device, VERA_CONTROLLER) for
device in VERA_DEVICES['lock'])
VeraLock(device, hass.data[VERA_CONTROLLER]) for
device in hass.data[VERA_DEVICES]['lock'])
class VeraLock(VeraDevice, LockDevice):

View File

@ -0,0 +1,60 @@
"""
Support for Vera scenes.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/scene.vera/
"""
import logging
from homeassistant.util import slugify
from homeassistant.components.scene import Scene
from homeassistant.components.vera import (
VERA_CONTROLLER, VERA_SCENES, VERA_ID_FORMAT)
DEPENDENCIES = ['vera']
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera scenes."""
add_devices(
[VeraScene(scene, hass.data[VERA_CONTROLLER])
for scene in hass.data[VERA_SCENES]], True)
class VeraScene(Scene):
"""Representation of a Vera scene entity."""
def __init__(self, vera_scene, controller):
"""Initialize the scene."""
self.vera_scene = vera_scene
self.controller = controller
self._name = self.vera_scene.name
# Append device id to prevent name clashes in HA.
self.vera_id = VERA_ID_FORMAT.format(
slugify(vera_scene.name), vera_scene.scene_id)
def update(self):
"""Update the scene status."""
self.vera_scene.refresh()
def activate(self, **kwargs):
"""Activate the scene."""
self.vera_scene.activate()
@property
def name(self):
"""Return the name of the scene."""
return self._name
@property
def device_state_attributes(self):
"""Return the state attributes of the scene."""
return {'vera_scene_id': self.vera_scene.vera_scene_id}
@property
def should_poll(self):
"""Return that polling is not necessary."""
return False

View File

@ -25,8 +25,8 @@ SCAN_INTERVAL = timedelta(seconds=5)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera controller devices."""
add_devices(
VeraSensor(device, VERA_CONTROLLER)
for device in VERA_DEVICES['sensor'])
VeraSensor(device, hass.data[VERA_CONTROLLER])
for device in hass.data[VERA_DEVICES]['sensor'])
class VeraSensor(VeraDevice, Entity):

View File

@ -19,8 +19,8 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera switches."""
add_devices(
VeraSwitch(device, VERA_CONTROLLER) for
device in VERA_DEVICES['switch'])
VeraSwitch(device, hass.data[VERA_CONTROLLER]) for
device in hass.data[VERA_DEVICES]['switch'])
class VeraSwitch(VeraDevice, SwitchDevice):

View File

@ -19,13 +19,13 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_STOP, CONF_LIGHTS, CONF_EXCLUDE)
from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['pyvera==0.2.38']
REQUIREMENTS = ['pyvera==0.2.39']
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'vera'
VERA_CONTROLLER = None
VERA_CONTROLLER = 'vera_controller'
CONF_CONTROLLER = 'vera_controller_url'
@ -34,7 +34,8 @@ VERA_ID_FORMAT = '{}_{}'
ATTR_CURRENT_POWER_W = "current_power_w"
ATTR_CURRENT_ENERGY_KWH = "current_energy_kwh"
VERA_DEVICES = defaultdict(list)
VERA_DEVICES = 'vera_devices'
VERA_SCENES = 'vera_scenes'
VERA_ID_LIST_SCHEMA = vol.Schema([int])
@ -47,20 +48,20 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA)
VERA_COMPONENTS = [
'binary_sensor', 'sensor', 'light', 'switch', 'lock', 'climate', 'cover'
'binary_sensor', 'sensor', 'light', 'switch',
'lock', 'climate', 'cover', 'scene'
]
# pylint: disable=unused-argument, too-many-function-args
def setup(hass, base_config):
"""Set up for Vera devices."""
global VERA_CONTROLLER
import pyvera as veraApi
def stop_subscription(event):
"""Shutdown Vera subscriptions and subscription thread on exit."""
_LOGGER.info("Shutting down subscriptions")
VERA_CONTROLLER.stop()
hass.data[VERA_CONTROLLER].stop()
config = base_config.get(DOMAIN)
@ -70,11 +71,14 @@ def setup(hass, base_config):
exclude_ids = config.get(CONF_EXCLUDE)
# Initialize the Vera controller.
VERA_CONTROLLER, _ = veraApi.init_controller(base_url)
controller, _ = veraApi.init_controller(base_url)
hass.data[VERA_CONTROLLER] = controller
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription)
try:
all_devices = VERA_CONTROLLER.get_devices()
all_devices = controller.get_devices()
all_scenes = controller.get_scenes()
except RequestException:
# There was a network related error connecting to the Vera controller.
_LOGGER.exception("Error communicating with Vera API")
@ -84,12 +88,19 @@ def setup(hass, base_config):
devices = [device for device in all_devices
if device.device_id not in exclude_ids]
vera_devices = defaultdict(list)
for device in devices:
device_type = map_vera_device(device, light_ids)
if device_type is None:
continue
VERA_DEVICES[device_type].append(device)
vera_devices[device_type].append(device)
hass.data[VERA_DEVICES] = vera_devices
vera_scenes = []
for scene in all_scenes:
vera_scenes.append(scene)
hass.data[VERA_SCENES] = vera_scenes
for component in VERA_COMPONENTS:
discovery.load_platform(hass, component, DOMAIN, {}, base_config)

View File

@ -925,7 +925,7 @@ pyunifi==2.13
# pyuserinput==0.1.11
# homeassistant.components.vera
pyvera==0.2.38
pyvera==0.2.39
# homeassistant.components.media_player.vizio
pyvizio==0.0.2