2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX scenes."""
|
2018-02-26 07:44:09 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.scene import CONF_PLATFORM, Scene
|
2019-02-19 13:09:06 +00:00
|
|
|
from homeassistant.const import CONF_ADDRESS, CONF_NAME
|
2018-02-26 07:44:09 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import ATTR_DISCOVER_DEVICES, DATA_KNX
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SCENE_NUMBER = "scene_number"
|
2018-02-26 07:44:09 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "KNX SCENE"
|
|
|
|
PLATFORM_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PLATFORM): "knx",
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_ADDRESS): cv.string,
|
|
|
|
vol.Required(CONF_SCENE_NUMBER): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
2018-02-26 07:44:09 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-02-26 07:44:09 +00:00
|
|
|
"""Set up the scenes for KNX platform."""
|
|
|
|
if discovery_info is not None:
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities_discovery(hass, discovery_info, async_add_entities)
|
2018-02-26 07:44:09 +00:00
|
|
|
else:
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities_config(hass, config, async_add_entities)
|
2018-02-26 07:44:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2018-08-24 14:37:30 +00:00
|
|
|
def async_add_entities_discovery(hass, discovery_info, async_add_entities):
|
2018-02-26 07:44:09 +00:00
|
|
|
"""Set up scenes for KNX platform configured via xknx.yaml."""
|
|
|
|
entities = []
|
|
|
|
for device_name in discovery_info[ATTR_DISCOVER_DEVICES]:
|
|
|
|
device = hass.data[DATA_KNX].xknx.devices[device_name]
|
|
|
|
entities.append(KNXScene(device))
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities)
|
2018-02-26 07:44:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
2018-08-24 14:37:30 +00:00
|
|
|
def async_add_entities_config(hass, config, async_add_entities):
|
2018-02-26 07:44:09 +00:00
|
|
|
"""Set up scene for KNX platform configured within platform."""
|
|
|
|
import xknx
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-02-26 07:44:09 +00:00
|
|
|
scene = xknx.devices.Scene(
|
|
|
|
hass.data[DATA_KNX].xknx,
|
2019-07-11 20:01:37 +00:00
|
|
|
name=config[CONF_NAME],
|
|
|
|
group_address=config[CONF_ADDRESS],
|
2019-07-31 19:25:30 +00:00
|
|
|
scene_number=config[CONF_SCENE_NUMBER],
|
|
|
|
)
|
2018-02-26 07:44:09 +00:00
|
|
|
hass.data[DATA_KNX].xknx.devices.add(scene)
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities([KNXScene(scene)])
|
2018-02-26 07:44:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KNXScene(Scene):
|
|
|
|
"""Representation of a KNX scene."""
|
|
|
|
|
|
|
|
def __init__(self, scene):
|
|
|
|
"""Init KNX scene."""
|
|
|
|
self.scene = scene
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the scene."""
|
|
|
|
return self.scene.name
|
|
|
|
|
2018-03-03 21:55:24 +00:00
|
|
|
async def async_activate(self):
|
2018-02-26 07:44:09 +00:00
|
|
|
"""Activate the scene."""
|
2018-03-03 21:55:24 +00:00
|
|
|
await self.scene.run()
|