hass-core/homeassistant/components/scene/knx.py
Julius Mittenzwei 7d5c1581f1 KNX Component: Scene support and expose sensor values (#11978)
* XKNX improvements: Added Scene support, added support for exposing sensors to KNX bus, added reset value option for binary switches

* fixed import

* Bumped version of KNX library (minor upgrade with two important bugfixes)

* bumped version of xknx (now without python requirement *sigh*)

* Issue #11978: fixed review comments

* Issue #11978: hound suggestion fixed:

* review comments

* made async functions async

* Addressed issues mentined by @MartinHjelmare

* removed default=None from validation schema

* ATTR_ENTITY_ID->CONF_ENTITY_ID

* moved missing function to async syntax

* pylint

* Trigger notification

* Trigger notification

* fixed review comment
2018-02-25 23:44:09 -08:00

79 lines
2.3 KiB
Python

"""
Support for KNX scenes.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/scene.knx/
"""
import asyncio
import voluptuous as vol
from homeassistant.components.knx import ATTR_DISCOVER_DEVICES, DATA_KNX
from homeassistant.components.scene import CONF_PLATFORM, Scene
from homeassistant.const import CONF_NAME
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
CONF_ADDRESS = 'address'
CONF_SCENE_NUMBER = 'scene_number'
DEFAULT_NAME = 'KNX SCENE'
DEPENDENCIES = ['knx']
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,
})
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices,
discovery_info=None):
"""Set up the scenes for KNX platform."""
if discovery_info is not None:
async_add_devices_discovery(hass, discovery_info, async_add_devices)
else:
async_add_devices_config(hass, config, async_add_devices)
@callback
def async_add_devices_discovery(hass, discovery_info, async_add_devices):
"""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))
async_add_devices(entities)
@callback
def async_add_devices_config(hass, config, async_add_devices):
"""Set up scene for KNX platform configured within platform."""
import xknx
scene = xknx.devices.Scene(
hass.data[DATA_KNX].xknx,
name=config.get(CONF_NAME),
group_address=config.get(CONF_ADDRESS),
scene_number=config.get(CONF_SCENE_NUMBER))
hass.data[DATA_KNX].xknx.devices.add(scene)
async_add_devices([KNXScene(scene)])
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
@asyncio.coroutine
def async_activate(self):
"""Activate the scene."""
yield from self.scene.run()