core/homeassistant/components/scsgate/light.py

111 lines
3.0 KiB
Python
Raw Normal View History

"""Support for SCSGate lights."""
import logging
from scsgate.tasks import ToggleStatusTask
import voluptuous as vol
from homeassistant.components import scsgate
from homeassistant.components.light import PLATFORM_SCHEMA, Light
2019-07-31 19:25:30 +00:00
from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE, CONF_DEVICES, CONF_NAME
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(scsgate.SCSGATE_SCHEMA)}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the SCSGate switches."""
devices = config.get(CONF_DEVICES)
lights = []
logger = logging.getLogger(__name__)
if devices:
for _, entity_info in devices.items():
if entity_info[scsgate.CONF_SCS_ID] in scsgate.SCSGATE.devices:
continue
name = entity_info[CONF_NAME]
scs_id = entity_info[scsgate.CONF_SCS_ID]
logger.info("Adding %s scsgate.light", name)
light = SCSGateLight(name=name, scs_id=scs_id, logger=logger)
lights.append(light)
add_entities(lights)
scsgate.SCSGATE.add_devices_to_register(lights)
class SCSGateLight(Light):
"""Representation of a SCSGate light."""
2016-03-07 21:08:21 +00:00
def __init__(self, scs_id, name, logger):
2016-03-07 21:08:21 +00:00
"""Initialize the light."""
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."""
return self._scs_id
@property
def should_poll(self):
2016-03-07 21:08:21 +00:00
"""No polling needed for a SCSGate light."""
return False
@property
def name(self):
2016-03-07 21:08:21 +00:00
"""Return the name of the device if any."""
return self._name
@property
def is_on(self):
2016-03-07 21:08:21 +00:00
"""Return true if light is on."""
return self._toggled
def turn_on(self, **kwargs):
2016-03-07 21:08:21 +00:00
"""Turn the device on."""
2019-07-31 19:25:30 +00:00
scsgate.SCSGATE.append_task(ToggleStatusTask(target=self._scs_id, toggled=True))
self._toggled = True
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
2016-03-07 21:08:21 +00:00
"""Turn the device off."""
scsgate.SCSGATE.append_task(
2019-07-31 19:25:30 +00:00
ToggleStatusTask(target=self._scs_id, toggled=False)
)
self._toggled = False
self.schedule_update_ha_state()
def process_event(self, message):
2016-03-07 21:08:21 +00:00
"""Handle a SCSGate message related with this light."""
if self._toggled == message.toggled:
self._logger.info(
"Light %s, ignoring message %s because state already active",
2019-07-31 19:25:30 +00:00
self._scs_id,
message,
)
# Nothing changed, ignoring
return
self._toggled = message.toggled
self.schedule_update_ha_state()
command = "off"
if self._toggled:
command = "on"
self.hass.bus.fire(
2019-07-31 19:25:30 +00:00
"button_pressed", {ATTR_ENTITY_ID: self._scs_id, ATTR_STATE: command}
)