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): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Perform the setup for Vera controller devices.""" """Perform the setup for Vera controller devices."""
add_devices( add_devices(
VeraBinarySensor(device, VERA_CONTROLLER) VeraBinarySensor(device, hass.data[VERA_CONTROLLER])
for device in VERA_DEVICES['binary_sensor']) for device in hass.data[VERA_DEVICES]['binary_sensor'])
class VeraBinarySensor(VeraDevice, BinarySensorDevice): 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): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Set up of Vera thermostats.""" """Set up of Vera thermostats."""
add_devices_callback( add_devices_callback(
VeraThermostat(device, VERA_CONTROLLER) for VeraThermostat(device, hass.data[VERA_CONTROLLER]) for
device in VERA_DEVICES['climate']) device in hass.data[VERA_DEVICES]['climate'])
class VeraThermostat(VeraDevice, ClimateDevice): class VeraThermostat(VeraDevice, ClimateDevice):

View File

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

View File

@ -21,7 +21,8 @@ DEPENDENCIES = ['vera']
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera lights.""" """Set up the Vera lights."""
add_devices( 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): class VeraLight(VeraDevice, Light):

View File

@ -19,8 +19,8 @@ DEPENDENCIES = ['vera']
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Find and return Vera locks.""" """Find and return Vera locks."""
add_devices( add_devices(
VeraLock(device, VERA_CONTROLLER) for VeraLock(device, hass.data[VERA_CONTROLLER]) for
device in VERA_DEVICES['lock']) device in hass.data[VERA_DEVICES]['lock'])
class VeraLock(VeraDevice, LockDevice): 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): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera controller devices.""" """Set up the Vera controller devices."""
add_devices( add_devices(
VeraSensor(device, VERA_CONTROLLER) VeraSensor(device, hass.data[VERA_CONTROLLER])
for device in VERA_DEVICES['sensor']) for device in hass.data[VERA_DEVICES]['sensor'])
class VeraSensor(VeraDevice, Entity): class VeraSensor(VeraDevice, Entity):

View File

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

View File

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

View File

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