2016-01-09 13:51:49 +00:00
|
|
|
"""
|
|
|
|
Support for SCSGate lights.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/light.scsgate/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-09-13 05:23:53 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-07-18 09:54:27 +00:00
|
|
|
from homeassistant.components import scsgate
|
2016-09-13 05:23:53 +00:00
|
|
|
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__)
|
2016-01-09 13:51:49 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['scsgate']
|
|
|
|
|
2016-09-13 05:23:53 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_DEVICES): vol.Schema({cv.slug: scsgate.SCSGATE_SCHEMA}),
|
|
|
|
})
|
2016-01-09 13:51:49 +00:00
|
|
|
|
2016-09-13 05:23:53 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up the SCSGate switches."""
|
2016-09-13 05:23:53 +00:00
|
|
|
devices = config.get(CONF_DEVICES)
|
2016-01-09 13:51:49 +00:00
|
|
|
lights = []
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
if devices:
|
|
|
|
for _, entity_info in devices.items():
|
2016-09-13 05:23:53 +00:00
|
|
|
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
|
2016-01-09 13:51:49 +00:00
|
|
|
continue
|
|
|
|
|
2016-09-13 05:23:53 +00:00
|
|
|
name = entity_info[CONF_NAME]
|
|
|
|
scs_id = entity_info[scsgate.CONF_SCS_ID]
|
|
|
|
|
|
|
|
logger.info("Adding %s scsgate.light", name)
|
2016-01-09 13:51:49 +00:00
|
|
|
|
2016-09-13 05:23:53 +00:00
|
|
|
light = SCSGateLight(name=name, scs_id=scs_id, logger=logger)
|
2016-01-09 13:51:49 +00:00
|
|
|
lights.append(light)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(lights)
|
2016-01-09 13:51:49 +00:00
|
|
|
scsgate.SCSGATE.add_devices_to_register(lights)
|
|
|
|
|
|
|
|
|
|
|
|
class SCSGateLight(Light):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Representation of a SCSGate light."""
|
2016-03-07 21:08:21 +00:00
|
|
|
|
2016-01-09 13:51:49 +00:00
|
|
|
def __init__(self, scs_id, name, logger):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Initialize the light."""
|
2016-01-09 13:51:49 +00:00
|
|
|
self._name = name
|
|
|
|
self._scs_id = scs_id
|
|
|
|
self._toggled = False
|
|
|
|
self._logger = logger
|
|
|
|
|
|
|
|
@property
|
|
|
|
def scs_id(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the SCS ID."""
|
2016-01-09 13:51:49 +00:00
|
|
|
return self._scs_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""No polling needed for a SCSGate light."""
|
2016-01-09 13:51:49 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the name of the device if any."""
|
2016-01-09 13:51:49 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return true if light is on."""
|
2016-01-09 13:51:49 +00:00
|
|
|
return self._toggled
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Turn the device on."""
|
2016-01-09 13:51:49 +00:00
|
|
|
from scsgate.tasks import ToggleStatusTask
|
|
|
|
|
|
|
|
scsgate.SCSGATE.append_task(
|
2016-09-13 05:23:53 +00:00
|
|
|
ToggleStatusTask(target=self._scs_id, toggled=True))
|
2016-01-09 13:51:49 +00:00
|
|
|
|
|
|
|
self._toggled = True
|
2016-11-30 21:33:38 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-01-09 13:51:49 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Turn the device off."""
|
2016-01-09 13:51:49 +00:00
|
|
|
from scsgate.tasks import ToggleStatusTask
|
|
|
|
|
|
|
|
scsgate.SCSGATE.append_task(
|
2016-09-13 05:23:53 +00:00
|
|
|
ToggleStatusTask(target=self._scs_id, toggled=False))
|
2016-01-09 13:51:49 +00:00
|
|
|
|
|
|
|
self._toggled = False
|
2016-11-30 21:33:38 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-01-09 13:51:49 +00:00
|
|
|
|
|
|
|
def process_event(self, message):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Handle a SCSGate message related with this light."""
|
2016-01-09 13:51:49 +00:00
|
|
|
if self._toggled == message.toggled:
|
|
|
|
self._logger.info(
|
|
|
|
"Light %s, ignoring message %s because state already active",
|
|
|
|
self._scs_id, message)
|
|
|
|
# Nothing changed, ignoring
|
|
|
|
return
|
|
|
|
|
|
|
|
self._toggled = message.toggled
|
2017-03-04 23:10:36 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-01-09 13:51:49 +00:00
|
|
|
|
|
|
|
command = "off"
|
|
|
|
if self._toggled:
|
|
|
|
command = "on"
|
|
|
|
|
|
|
|
self.hass.bus.fire(
|
|
|
|
'button_pressed', {
|
|
|
|
ATTR_ENTITY_ID: self._scs_id,
|
2016-09-13 05:23:53 +00:00
|
|
|
ATTR_STATE: command,
|
2016-01-09 13:51:49 +00:00
|
|
|
}
|
|
|
|
)
|