core/homeassistant/components/lcn/scene.py

75 lines
2.3 KiB
Python
Raw Normal View History

2019-06-05 09:12:05 +00:00
"""Support for LCN scenes."""
2019-06-05 09:12:05 +00:00
import pypck
from homeassistant.components.scene import DOMAIN as DOMAIN_SCENE, Scene
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_ENTITIES, CONF_SCENE
2019-06-05 09:12:05 +00:00
from . import LcnEntity
2019-06-05 09:12:05 +00:00
from .const import (
CONF_DOMAIN_DATA,
2019-07-31 19:25:30 +00:00
CONF_OUTPUTS,
CONF_REGISTER,
CONF_TRANSITION,
OUTPUT_PORTS,
)
from .helpers import get_device_connection
2019-06-05 09:12:05 +00:00
PARALLEL_UPDATES = 0
2019-06-05 09:12:05 +00:00
def create_lcn_scene_entity(hass, entity_config, config_entry):
"""Set up an entity for this domain."""
device_connection = get_device_connection(
hass, tuple(entity_config[CONF_ADDRESS]), config_entry
)
2019-06-05 09:12:05 +00:00
return LcnScene(entity_config, config_entry.entry_id, device_connection)
2019-06-05 09:12:05 +00:00
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up LCN switch entities from a config entry."""
entities = []
for entity_config in config_entry.data[CONF_ENTITIES]:
if entity_config[CONF_DOMAIN] == DOMAIN_SCENE:
entities.append(create_lcn_scene_entity(hass, entity_config, config_entry))
async_add_entities(entities)
2019-06-05 09:12:05 +00:00
class LcnScene(LcnEntity, Scene):
2019-06-05 09:12:05 +00:00
"""Representation of a LCN scene."""
def __init__(self, config, entry_id, device_connection):
2019-06-05 09:12:05 +00:00
"""Initialize the LCN scene."""
super().__init__(config, entry_id, device_connection)
2019-06-05 09:12:05 +00:00
self.register_id = config[CONF_DOMAIN_DATA][CONF_REGISTER]
self.scene_id = config[CONF_DOMAIN_DATA][CONF_SCENE]
2019-06-05 09:12:05 +00:00
self.output_ports = []
self.relay_ports = []
for port in config[CONF_DOMAIN_DATA][CONF_OUTPUTS]:
2019-06-05 09:12:05 +00:00
if port in OUTPUT_PORTS:
self.output_ports.append(pypck.lcn_defs.OutputPort[port])
else: # in RELEAY_PORTS
self.relay_ports.append(pypck.lcn_defs.RelayPort[port])
if config[CONF_DOMAIN_DATA][CONF_TRANSITION] is None:
2019-06-05 09:12:05 +00:00
self.transition = None
else:
self.transition = pypck.lcn_defs.time_to_ramp_value(
config[CONF_DOMAIN_DATA][CONF_TRANSITION]
)
2019-06-05 09:12:05 +00:00
async def async_activate(self, **kwargs):
2019-06-05 09:12:05 +00:00
"""Activate scene."""
await self.device_connection.activate_scene(
2019-07-31 19:25:30 +00:00
self.register_id,
self.scene_id,
self.output_ports,
self.relay_ports,
self.transition,
)