Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Vera scenes #10424

Merged
merged 5 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/binary_sensor/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/climate/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/cover/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
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):
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/light/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/lock/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
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):
Expand Down
60 changes: 60 additions & 0 deletions homeassistant/components/scene/vera.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions homeassistant/components/sensor/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/switch/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
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):
Expand Down
29 changes: 20 additions & 9 deletions homeassistant/components/vera.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
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'

Expand All @@ -34,7 +34,8 @@
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])

Expand All @@ -47,20 +48,20 @@
}, 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)

Expand All @@ -70,11 +71,14 @@ def stop_subscription(event):
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")
Expand All @@ -84,12 +88,19 @@ def stop_subscription(event):
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)
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,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
Expand Down