Use voluptuous for SCSGate (#3265)
* Migrate to voluptuous * Extend platforms
This commit is contained in:
parent
8189ec2c8d
commit
8ba952ee0e
5 changed files with 139 additions and 111 deletions
|
@ -6,37 +6,43 @@ https://home-assistant.io/components/cover.scsgate/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.components.scsgate as scsgate
|
import homeassistant.components.scsgate as scsgate
|
||||||
from homeassistant.components.cover import CoverDevice
|
from homeassistant.components.cover import (CoverDevice, PLATFORM_SCHEMA)
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import (CONF_DEVICES, CONF_NAME)
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEPENDENCIES = ['scsgate']
|
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."""
|
"""Setup the SCSGate cover."""
|
||||||
devices = config.get('devices')
|
devices = config.get(CONF_DEVICES)
|
||||||
covers = []
|
covers = []
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
if devices:
|
if devices:
|
||||||
for _, entity_info in devices.items():
|
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
|
continue
|
||||||
|
|
||||||
logger.info("Adding %s scsgate.cover", entity_info[CONF_NAME])
|
|
||||||
|
|
||||||
name = entity_info[CONF_NAME]
|
name = entity_info[CONF_NAME]
|
||||||
scs_id = entity_info[SCS_ID]
|
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
||||||
cover = SCSGateCover(
|
|
||||||
name=name,
|
logger.info("Adding %s scsgate.cover", name)
|
||||||
scs_id=scs_id,
|
|
||||||
logger=logger)
|
cover = SCSGateCover(name=name, scs_id=scs_id, logger=logger)
|
||||||
scsgate.SCSGATE.add_device(cover)
|
scsgate.SCSGATE.add_device(cover)
|
||||||
covers.append(cover)
|
covers.append(cover)
|
||||||
|
|
||||||
add_devices_callback(covers)
|
add_devices(covers)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
||||||
|
@ -91,6 +97,5 @@ class SCSGateCover(CoverDevice):
|
||||||
|
|
||||||
def process_event(self, message):
|
def process_event(self, message):
|
||||||
"""Handle a SCSGate message related with this cover."""
|
"""Handle a SCSGate message related with this cover."""
|
||||||
self._logger.debug(
|
self._logger.debug("Cover %s, got message %s",
|
||||||
"Rollershutter %s, got message %s",
|
self._scs_id, message.toggled)
|
||||||
self._scs_id, message.toggled)
|
|
||||||
|
|
|
@ -6,35 +6,43 @@ https://home-assistant.io/components/light.scsgate/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.components.scsgate as scsgate
|
import homeassistant.components.scsgate as scsgate
|
||||||
from homeassistant.components.light import Light
|
from homeassistant.components.light import (Light, PLATFORM_SCHEMA)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
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']
|
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."""
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
devices = config.get('devices')
|
"""Setup the SCSGate switches."""
|
||||||
|
devices = config.get(CONF_DEVICES)
|
||||||
lights = []
|
lights = []
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
if devices:
|
if devices:
|
||||||
for _, entity_info in devices.items():
|
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
|
continue
|
||||||
|
|
||||||
logger.info("Adding %s scsgate.light", entity_info['name'])
|
name = entity_info[CONF_NAME]
|
||||||
|
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
||||||
|
|
||||||
name = entity_info['name']
|
logger.info("Adding %s scsgate.light", name)
|
||||||
scs_id = entity_info['scs_id']
|
|
||||||
light = SCSGateLight(
|
light = SCSGateLight(name=name, scs_id=scs_id, logger=logger)
|
||||||
name=name,
|
|
||||||
scs_id=scs_id,
|
|
||||||
logger=logger)
|
|
||||||
lights.append(light)
|
lights.append(light)
|
||||||
|
|
||||||
add_devices_callback(lights)
|
add_devices(lights)
|
||||||
scsgate.SCSGATE.add_devices_to_register(lights)
|
scsgate.SCSGATE.add_devices_to_register(lights)
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,9 +81,7 @@ class SCSGateLight(Light):
|
||||||
from scsgate.tasks import ToggleStatusTask
|
from scsgate.tasks import ToggleStatusTask
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(
|
scsgate.SCSGATE.append_task(
|
||||||
ToggleStatusTask(
|
ToggleStatusTask(target=self._scs_id, toggled=True))
|
||||||
target=self._scs_id,
|
|
||||||
toggled=True))
|
|
||||||
|
|
||||||
self._toggled = True
|
self._toggled = True
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
@ -85,9 +91,7 @@ class SCSGateLight(Light):
|
||||||
from scsgate.tasks import ToggleStatusTask
|
from scsgate.tasks import ToggleStatusTask
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(
|
scsgate.SCSGATE.append_task(
|
||||||
ToggleStatusTask(
|
ToggleStatusTask(target=self._scs_id, toggled=False))
|
||||||
target=self._scs_id,
|
|
||||||
toggled=False))
|
|
||||||
|
|
||||||
self._toggled = False
|
self._toggled = False
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
@ -111,6 +115,6 @@ class SCSGateLight(Light):
|
||||||
self.hass.bus.fire(
|
self.hass.bus.fire(
|
||||||
'button_pressed', {
|
'button_pressed', {
|
||||||
ATTR_ENTITY_ID: self._scs_id,
|
ATTR_ENTITY_ID: self._scs_id,
|
||||||
'state': command
|
ATTR_STATE: command,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -7,15 +7,60 @@ https://home-assistant.io/components/scsgate/
|
||||||
import logging
|
import logging
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.const import (CONF_DEVICE, CONF_NAME)
|
||||||
from homeassistant.core import EVENT_HOMEASSISTANT_STOP
|
from homeassistant.core import EVENT_HOMEASSISTANT_STOP
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['scsgate==0.1.0']
|
REQUIREMENTS = ['scsgate==0.1.0']
|
||||||
DOMAIN = "scsgate"
|
|
||||||
SCSGATE = None
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ATTR_STATE = 'state'
|
||||||
|
|
||||||
class SCSGate:
|
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(object):
|
||||||
"""The class for dealing with the SCSGate device via scsgate.Reactor."""
|
"""The class for dealing with the SCSGate device via scsgate.Reactor."""
|
||||||
|
|
||||||
def __init__(self, device, logger):
|
def __init__(self, device, logger):
|
||||||
|
@ -32,8 +77,7 @@ class SCSGate:
|
||||||
|
|
||||||
from scsgate.reactor import Reactor
|
from scsgate.reactor import Reactor
|
||||||
self._reactor = Reactor(
|
self._reactor = Reactor(
|
||||||
connection=connection,
|
connection=connection, logger=self._logger,
|
||||||
logger=self._logger,
|
|
||||||
handle_message=self.handle_message)
|
handle_message=self.handle_message)
|
||||||
|
|
||||||
def handle_message(self, message):
|
def handle_message(self, message):
|
||||||
|
@ -61,8 +105,7 @@ class SCSGate:
|
||||||
try:
|
try:
|
||||||
self._devices[message.entity].process_event(message)
|
self._devices[message.entity].process_event(message)
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
msg = "Exception while processing event: {}".format(
|
msg = "Exception while processing event: {}".format(exception)
|
||||||
exception)
|
|
||||||
self._logger.error(msg)
|
self._logger.error(msg)
|
||||||
else:
|
else:
|
||||||
self._logger.info(
|
self._logger.info(
|
||||||
|
@ -127,26 +170,3 @@ class SCSGate:
|
||||||
def append_task(self, task):
|
def append_task(self, task):
|
||||||
"""Register a new task to be executed."""
|
"""Register a new task to be executed."""
|
||||||
self._reactor.append_task(task)
|
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
|
|
||||||
|
|
|
@ -6,48 +6,56 @@ https://home-assistant.io/components/switch.scsgate/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.components.scsgate as scsgate
|
import homeassistant.components.scsgate as scsgate
|
||||||
from homeassistant.components.switch import SwitchDevice
|
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
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']
|
DEPENDENCIES = ['scsgate']
|
||||||
|
|
||||||
|
CONF_TRADITIONAL = 'traditional'
|
||||||
|
CONF_SCENARIO = 'scenario'
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
CONF_SCS_ID = 'scs_id'
|
||||||
|
|
||||||
|
DOMAIN = 'scsgate'
|
||||||
|
|
||||||
|
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."""
|
"""Setup the SCSGate switches."""
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
_setup_traditional_switches(
|
_setup_traditional_switches(
|
||||||
logger=logger,
|
logger=logger, config=config, add_devices_callback=add_devices)
|
||||||
config=config,
|
|
||||||
add_devices_callback=add_devices_callback)
|
|
||||||
|
|
||||||
_setup_scenario_switches(
|
_setup_scenario_switches(logger=logger, config=config, hass=hass)
|
||||||
logger=logger,
|
|
||||||
config=config,
|
|
||||||
hass=hass)
|
|
||||||
|
|
||||||
|
|
||||||
def _setup_traditional_switches(logger, config, add_devices_callback):
|
def _setup_traditional_switches(logger, config, add_devices_callback):
|
||||||
"""Add traditional SCSGate switches."""
|
"""Add traditional SCSGate switches."""
|
||||||
traditional = config.get('traditional')
|
traditional = config.get(CONF_TRADITIONAL)
|
||||||
switches = []
|
switches = []
|
||||||
|
|
||||||
if traditional:
|
if traditional:
|
||||||
for _, entity_info in traditional.items():
|
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
|
continue
|
||||||
|
|
||||||
logger.info(
|
name = entity_info[CONF_NAME]
|
||||||
"Adding %s scsgate.traditional_switch", entity_info['name'])
|
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
||||||
|
|
||||||
name = entity_info['name']
|
logger.info("Adding %s scsgate.traditional_switch", name)
|
||||||
scs_id = entity_info['scs_id']
|
|
||||||
|
|
||||||
switch = SCSGateSwitch(
|
switch = SCSGateSwitch(name=name, scs_id=scs_id, logger=logger)
|
||||||
name=name,
|
|
||||||
scs_id=scs_id,
|
|
||||||
logger=logger)
|
|
||||||
switches.append(switch)
|
switches.append(switch)
|
||||||
|
|
||||||
add_devices_callback(switches)
|
add_devices_callback(switches)
|
||||||
|
@ -56,24 +64,20 @@ def _setup_traditional_switches(logger, config, add_devices_callback):
|
||||||
|
|
||||||
def _setup_scenario_switches(logger, config, hass):
|
def _setup_scenario_switches(logger, config, hass):
|
||||||
"""Add only SCSGate scenario switches."""
|
"""Add only SCSGate scenario switches."""
|
||||||
scenario = config.get("scenario")
|
scenario = config.get(CONF_SCENARIO)
|
||||||
|
|
||||||
if scenario:
|
if scenario:
|
||||||
for _, entity_info in scenario.items():
|
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
|
continue
|
||||||
|
|
||||||
logger.info(
|
name = entity_info[CONF_NAME]
|
||||||
"Adding %s scsgate.scenario_switch", entity_info['name'])
|
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
||||||
|
|
||||||
name = entity_info['name']
|
logger.info("Adding %s scsgate.scenario_switch", name)
|
||||||
scs_id = entity_info['scs_id']
|
|
||||||
|
|
||||||
switch = SCSGateScenarioSwitch(
|
switch = SCSGateScenarioSwitch(
|
||||||
name=name,
|
name=name, scs_id=scs_id, logger=logger, hass=hass)
|
||||||
scs_id=scs_id,
|
|
||||||
logger=logger,
|
|
||||||
hass=hass)
|
|
||||||
scsgate.SCSGATE.add_device(switch)
|
scsgate.SCSGATE.add_device(switch)
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,9 +116,7 @@ class SCSGateSwitch(SwitchDevice):
|
||||||
from scsgate.tasks import ToggleStatusTask
|
from scsgate.tasks import ToggleStatusTask
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(
|
scsgate.SCSGATE.append_task(
|
||||||
ToggleStatusTask(
|
ToggleStatusTask(target=self._scs_id, toggled=True))
|
||||||
target=self._scs_id,
|
|
||||||
toggled=True))
|
|
||||||
|
|
||||||
self._toggled = True
|
self._toggled = True
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
@ -124,9 +126,7 @@ class SCSGateSwitch(SwitchDevice):
|
||||||
from scsgate.tasks import ToggleStatusTask
|
from scsgate.tasks import ToggleStatusTask
|
||||||
|
|
||||||
scsgate.SCSGATE.append_task(
|
scsgate.SCSGATE.append_task(
|
||||||
ToggleStatusTask(
|
ToggleStatusTask(target=self._scs_id, toggled=False))
|
||||||
target=self._scs_id,
|
|
||||||
toggled=False))
|
|
||||||
|
|
||||||
self._toggled = False
|
self._toggled = False
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
|
@ -150,12 +150,11 @@ class SCSGateSwitch(SwitchDevice):
|
||||||
self.hass.bus.fire(
|
self.hass.bus.fire(
|
||||||
'button_pressed', {
|
'button_pressed', {
|
||||||
ATTR_ENTITY_ID: self._scs_id,
|
ATTR_ENTITY_ID: self._scs_id,
|
||||||
'state': command
|
ATTR_STATE: command}
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SCSGateScenarioSwitch:
|
class SCSGateScenarioSwitch(object):
|
||||||
"""Provides a SCSGate scenario switch.
|
"""Provides a SCSGate scenario switch.
|
||||||
|
|
||||||
This switch is always in a 'off" state, when toggled it's used to trigger
|
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):
|
elif isinstance(message, ScenarioTriggeredMessage):
|
||||||
scenario_id = message.scenario
|
scenario_id = message.scenario
|
||||||
else:
|
else:
|
||||||
self._logger.warn(
|
self._logger.warn("Scenario switch: received unknown message %s",
|
||||||
"Scenario switch: received unknown message %s",
|
message)
|
||||||
message)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
self._hass.bus.fire(
|
self._hass.bus.fire(
|
||||||
'scenario_switch_triggered', {
|
'scenario_switch_triggered', {
|
||||||
ATTR_ENTITY_ID: int(self._scs_id),
|
ATTR_ENTITY_ID: int(self._scs_id),
|
||||||
'scenario_id': int(scenario_id, 16)
|
ATTR_SCENARIO_ID: int(scenario_id, 16)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -263,6 +263,7 @@ ATTR_GPS_ACCURACY = 'gps_accuracy'
|
||||||
|
|
||||||
# If state is assumed
|
# If state is assumed
|
||||||
ATTR_ASSUMED_STATE = 'assumed_state'
|
ATTR_ASSUMED_STATE = 'assumed_state'
|
||||||
|
ATTR_STATE = 'state'
|
||||||
|
|
||||||
# #### SERVICES ####
|
# #### SERVICES ####
|
||||||
SERVICE_HOMEASSISTANT_STOP = 'stop'
|
SERVICE_HOMEASSISTANT_STOP = 'stop'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue