Skip to content

Commit

Permalink
Use voluptuous for SCSGate (home-assistant#3265)
Browse files Browse the repository at this point in the history
* Migrate to voluptuous

* Extend platforms
  • Loading branch information
fabaff authored Sep 13, 2016
1 parent 8189ec2 commit 8ba952e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 111 deletions.
39 changes: 22 additions & 17 deletions homeassistant/components/cover/scsgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,43 @@
"""
import logging

import voluptuous as vol

import homeassistant.components.scsgate as scsgate
from homeassistant.components.cover import CoverDevice
from homeassistant.const import CONF_NAME
from homeassistant.components.cover import (CoverDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_DEVICES, CONF_NAME)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['scsgate']
SCS_ID = 'scs_id'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DEVICES): vol.Schema({cv.slug: scsgate.SCSGATE_SCHEMA}),
})


def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the SCSGate cover."""
devices = config.get('devices')
devices = config.get(CONF_DEVICES)
covers = []
logger = logging.getLogger(__name__)

if devices:
for _, entity_info in devices.items():
if entity_info[SCS_ID] in scsgate.SCSGATE.devices:
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
continue

logger.info("Adding %s scsgate.cover", entity_info[CONF_NAME])

name = entity_info[CONF_NAME]
scs_id = entity_info[SCS_ID]
cover = SCSGateCover(
name=name,
scs_id=scs_id,
logger=logger)
scs_id = entity_info[scsgate.CONF_SCS_ID]

logger.info("Adding %s scsgate.cover", name)

cover = SCSGateCover(name=name, scs_id=scs_id, logger=logger)
scsgate.SCSGATE.add_device(cover)
covers.append(cover)

add_devices_callback(covers)
add_devices(covers)


# pylint: disable=too-many-arguments, too-many-instance-attributes
Expand Down Expand Up @@ -91,6 +97,5 @@ def stop_cover(self, **kwargs):

def process_event(self, message):
"""Handle a SCSGate message related with this cover."""
self._logger.debug(
"Rollershutter %s, got message %s",
self._scs_id, message.toggled)
self._logger.debug("Cover %s, got message %s",
self._scs_id, message.toggled)
46 changes: 25 additions & 21 deletions homeassistant/components/light/scsgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,43 @@
"""
import logging

import voluptuous as vol

import homeassistant.components.scsgate as scsgate
from homeassistant.components.light import Light
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.components.light import (Light, PLATFORM_SCHEMA)
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_STATE, CONF_DEVICES, CONF_NAME)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['scsgate']

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DEVICES): vol.Schema({cv.slug: scsgate.SCSGATE_SCHEMA}),
})

def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Add the SCSGate swiches defined inside of the configuration file."""
devices = config.get('devices')

def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the SCSGate switches."""
devices = config.get(CONF_DEVICES)
lights = []
logger = logging.getLogger(__name__)

if devices:
for _, entity_info in devices.items():
if entity_info['scs_id'] in scsgate.SCSGATE.devices:
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
continue

logger.info("Adding %s scsgate.light", entity_info['name'])
name = entity_info[CONF_NAME]
scs_id = entity_info[scsgate.CONF_SCS_ID]

logger.info("Adding %s scsgate.light", name)

name = entity_info['name']
scs_id = entity_info['scs_id']
light = SCSGateLight(
name=name,
scs_id=scs_id,
logger=logger)
light = SCSGateLight(name=name, scs_id=scs_id, logger=logger)
lights.append(light)

add_devices_callback(lights)
add_devices(lights)
scsgate.SCSGATE.add_devices_to_register(lights)


Expand Down Expand Up @@ -73,9 +81,7 @@ def turn_on(self, **kwargs):
from scsgate.tasks import ToggleStatusTask

scsgate.SCSGATE.append_task(
ToggleStatusTask(
target=self._scs_id,
toggled=True))
ToggleStatusTask(target=self._scs_id, toggled=True))

self._toggled = True
self.update_ha_state()
Expand All @@ -85,9 +91,7 @@ def turn_off(self, **kwargs):
from scsgate.tasks import ToggleStatusTask

scsgate.SCSGATE.append_task(
ToggleStatusTask(
target=self._scs_id,
toggled=False))
ToggleStatusTask(target=self._scs_id, toggled=False))

self._toggled = False
self.update_ha_state()
Expand All @@ -111,6 +115,6 @@ def process_event(self, message):
self.hass.bus.fire(
'button_pressed', {
ATTR_ENTITY_ID: self._scs_id,
'state': command
ATTR_STATE: command,
}
)
80 changes: 50 additions & 30 deletions homeassistant/components/scsgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,60 @@
import logging
from threading import Lock

import voluptuous as vol

from homeassistant.const import (CONF_DEVICE, CONF_NAME)
from homeassistant.core import EVENT_HOMEASSISTANT_STOP
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['scsgate==0.1.0']
DOMAIN = "scsgate"
SCSGATE = None

_LOGGER = logging.getLogger(__name__)

ATTR_STATE = 'state'

CONF_SCS_ID = 'scs_id'

DOMAIN = 'scsgate'

SCSGATE = None

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_DEVICE): cv.string,
}),
}, extra=vol.ALLOW_EXTRA)

SCSGATE_SCHEMA = vol.Schema({
vol.Required(CONF_SCS_ID): cv.string,
vol.Optional(CONF_NAME): cv.string,
})


def setup(hass, config):
"""Setup the SCSGate component."""
device = config[DOMAIN][CONF_DEVICE]
global SCSGATE

# pylint: disable=broad-except
try:
SCSGATE = SCSGate(device=device, logger=_LOGGER)
SCSGATE.start()
except Exception as exception:
_LOGGER.error("Cannot setup SCSGate component: %s", exception)
return False

def stop_monitor(event):
"""Stop the SCSGate."""
_LOGGER.info("Stopping SCSGate monitor thread")
SCSGATE.stop()

hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_monitor)

return True

class SCSGate:

class SCSGate(object):
"""The class for dealing with the SCSGate device via scsgate.Reactor."""

def __init__(self, device, logger):
Expand All @@ -32,8 +77,7 @@ def __init__(self, device, logger):

from scsgate.reactor import Reactor
self._reactor = Reactor(
connection=connection,
logger=self._logger,
connection=connection, logger=self._logger,
handle_message=self.handle_message)

def handle_message(self, message):
Expand Down Expand Up @@ -61,8 +105,7 @@ def handle_message(self, message):
try:
self._devices[message.entity].process_event(message)
except Exception as exception:
msg = "Exception while processing event: {}".format(
exception)
msg = "Exception while processing event: {}".format(exception)
self._logger.error(msg)
else:
self._logger.info(
Expand Down Expand Up @@ -127,26 +170,3 @@ def stop(self):
def append_task(self, task):
"""Register a new task to be executed."""
self._reactor.append_task(task)


def setup(hass, config):
"""Setup the SCSGate component."""
device = config['scsgate']['device']
global SCSGATE

# pylint: disable=broad-except
try:
SCSGATE = SCSGate(device=device, logger=_LOGGER)
SCSGATE.start()
except Exception as exception:
_LOGGER.error("Cannot setup SCSGate component: %s", exception)
return False

def stop_monitor(event):
"""Stop the SCSGate."""
_LOGGER.info("Stopping SCSGate monitor thread")
SCSGATE.stop()

hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_monitor)

return True
Loading

0 comments on commit 8ba952e

Please sign in to comment.