diff --git a/homeassistant/components/cover/scsgate.py b/homeassistant/components/cover/scsgate.py index 18692534e907e67fe9e1f09eda7a1b7125c72f1c..c491f3bf5486b5aecd24f4b4e6a8b169463a5d62 100644 --- a/homeassistant/components/cover/scsgate.py +++ b/homeassistant/components/cover/scsgate.py @@ -6,37 +6,43 @@ https://home-assistant.io/components/cover.scsgate/ """ 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 @@ -91,6 +97,5 @@ class SCSGateCover(CoverDevice): 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) diff --git a/homeassistant/components/light/scsgate.py b/homeassistant/components/light/scsgate.py index 31c0751313646a69f6a055000d6fbbeb1e46fc11..a33b30736fe14f039ac0cdcd868e17c8c91ece06 100644 --- a/homeassistant/components/light/scsgate.py +++ b/homeassistant/components/light/scsgate.py @@ -6,35 +6,43 @@ https://home-assistant.io/components/light.scsgate/ """ 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) @@ -73,9 +81,7 @@ class SCSGateLight(Light): 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() @@ -85,9 +91,7 @@ class SCSGateLight(Light): 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() @@ -111,6 +115,6 @@ class SCSGateLight(Light): self.hass.bus.fire( 'button_pressed', { ATTR_ENTITY_ID: self._scs_id, - 'state': command + ATTR_STATE: command, } ) diff --git a/homeassistant/components/scsgate.py b/homeassistant/components/scsgate.py index bcbe0affbbac3bd02ebad40de6f336f52d1725bc..549759f5e127b55547e5380bb72d2b1e6c45bef8 100644 --- a/homeassistant/components/scsgate.py +++ b/homeassistant/components/scsgate.py @@ -7,15 +7,60 @@ https://home-assistant.io/components/scsgate/ 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): @@ -32,8 +77,7 @@ class SCSGate: 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): @@ -61,8 +105,7 @@ class SCSGate: 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( @@ -127,26 +170,3 @@ class SCSGate: 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 diff --git a/homeassistant/components/switch/scsgate.py b/homeassistant/components/switch/scsgate.py index 964b23c37daf7bddebe11189de1f3a411de55e61..c1d5b19fdeeaa90bd869a4e196d7c43d78fc4774 100644 --- a/homeassistant/components/switch/scsgate.py +++ b/homeassistant/components/switch/scsgate.py @@ -6,48 +6,56 @@ https://home-assistant.io/components/switch.scsgate/ """ import logging +import voluptuous as vol + import homeassistant.components.scsgate as scsgate -from homeassistant.components.switch import SwitchDevice -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) +from homeassistant.const import ( + ATTR_ENTITY_ID, ATTR_STATE, CONF_NAME, CONF_DEVICES) +import homeassistant.helpers.config_validation as cv + +ATTR_SCENARIO_ID = 'scenario_id' DEPENDENCIES = ['scsgate'] +CONF_TRADITIONAL = 'traditional' +CONF_SCENARIO = 'scenario' + +CONF_SCS_ID = 'scs_id' + +DOMAIN = 'scsgate' -def setup_platform(hass, config, add_devices_callback, discovery_info=None): +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_DEVICES): vol.Schema({cv.slug: scsgate.SCSGATE_SCHEMA}), +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the SCSGate switches.""" logger = logging.getLogger(__name__) _setup_traditional_switches( - logger=logger, - config=config, - add_devices_callback=add_devices_callback) + logger=logger, config=config, add_devices_callback=add_devices) - _setup_scenario_switches( - logger=logger, - config=config, - hass=hass) + _setup_scenario_switches(logger=logger, config=config, hass=hass) def _setup_traditional_switches(logger, config, add_devices_callback): """Add traditional SCSGate switches.""" - traditional = config.get('traditional') + traditional = config.get(CONF_TRADITIONAL) switches = [] if traditional: for _, entity_info in traditional.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.traditional_switch", entity_info['name']) + name = entity_info[CONF_NAME] + scs_id = entity_info[scsgate.CONF_SCS_ID] - name = entity_info['name'] - scs_id = entity_info['scs_id'] + logger.info("Adding %s scsgate.traditional_switch", name) - switch = SCSGateSwitch( - name=name, - scs_id=scs_id, - logger=logger) + switch = SCSGateSwitch(name=name, scs_id=scs_id, logger=logger) switches.append(switch) add_devices_callback(switches) @@ -56,24 +64,20 @@ def _setup_traditional_switches(logger, config, add_devices_callback): def _setup_scenario_switches(logger, config, hass): """Add only SCSGate scenario switches.""" - scenario = config.get("scenario") + scenario = config.get(CONF_SCENARIO) if scenario: for _, entity_info in scenario.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.scenario_switch", entity_info['name']) + name = entity_info[CONF_NAME] + scs_id = entity_info[scsgate.CONF_SCS_ID] - name = entity_info['name'] - scs_id = entity_info['scs_id'] + logger.info("Adding %s scsgate.scenario_switch", name) switch = SCSGateScenarioSwitch( - name=name, - scs_id=scs_id, - logger=logger, - hass=hass) + name=name, scs_id=scs_id, logger=logger, hass=hass) scsgate.SCSGATE.add_device(switch) @@ -112,9 +116,7 @@ class SCSGateSwitch(SwitchDevice): 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() @@ -124,9 +126,7 @@ class SCSGateSwitch(SwitchDevice): 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() @@ -150,12 +150,11 @@ class SCSGateSwitch(SwitchDevice): self.hass.bus.fire( 'button_pressed', { ATTR_ENTITY_ID: self._scs_id, - 'state': command - } + ATTR_STATE: command} ) -class SCSGateScenarioSwitch: +class SCSGateScenarioSwitch(object): """Provides a SCSGate scenario switch. This switch is always in a 'off" state, when toggled it's used to trigger @@ -188,14 +187,13 @@ class SCSGateScenarioSwitch: elif isinstance(message, ScenarioTriggeredMessage): scenario_id = message.scenario else: - self._logger.warn( - "Scenario switch: received unknown message %s", - message) + self._logger.warn("Scenario switch: received unknown message %s", + message) return self._hass.bus.fire( 'scenario_switch_triggered', { ATTR_ENTITY_ID: int(self._scs_id), - 'scenario_id': int(scenario_id, 16) + ATTR_SCENARIO_ID: int(scenario_id, 16) } ) diff --git a/homeassistant/const.py b/homeassistant/const.py index b8b3756f37b82fc67efe0144a82fd47cc2eba89f..4107fb65a243a0b2ecff829da5248d87435d761c 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -263,6 +263,7 @@ ATTR_GPS_ACCURACY = 'gps_accuracy' # If state is assumed ATTR_ASSUMED_STATE = 'assumed_state' +ATTR_STATE = 'state' # #### SERVICES #### SERVICE_HOMEASSISTANT_STOP = 'stop'