2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP switches."""
|
2016-09-14 06:03:30 +00:00
|
|
|
import voluptuous as vol
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
2019-02-19 13:09:06 +00:00
|
|
|
from homeassistant.const import CONF_ADDRESS, CONF_NAME
|
2017-09-07 07:11:55 +00:00
|
|
|
from homeassistant.core import callback
|
2016-09-14 06:03:30 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import ATTR_DISCOVER_DEVICES, DATA_KNX
|
|
|
|
|
2016-09-14 06:03:30 +00:00
|
|
|
CONF_STATE_ADDRESS = 'state_address'
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2016-09-14 06:03:30 +00:00
|
|
|
DEFAULT_NAME = 'KNX Switch'
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_STATE_ADDRESS): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
2018-02-24 18:24:33 +00:00
|
|
|
discovery_info=None):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up switch(es) 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)
|
2017-09-07 07:11:55 +00:00
|
|
|
else:
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities_config(hass, config, async_add_entities)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@callback
|
2018-08-24 14:37:30 +00:00
|
|
|
def async_add_entities_discovery(hass, discovery_info, async_add_entities):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Set up switches 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]
|
2018-10-15 01:29:36 +00:00
|
|
|
entities.append(KNXSwitch(device))
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@callback
|
2018-08-24 14:37:30 +00:00
|
|
|
def async_add_entities_config(hass, config, async_add_entities):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Set up switch for KNX platform configured within platform."""
|
2017-09-07 07:11:55 +00:00
|
|
|
import xknx
|
|
|
|
switch = xknx.devices.Switch(
|
|
|
|
hass.data[DATA_KNX].xknx,
|
|
|
|
name=config.get(CONF_NAME),
|
|
|
|
group_address=config.get(CONF_ADDRESS),
|
|
|
|
group_address_state=config.get(CONF_STATE_ADDRESS))
|
|
|
|
hass.data[DATA_KNX].xknx.devices.add(switch)
|
2018-10-15 01:29:36 +00:00
|
|
|
async_add_entities([KNXSwitch(switch)])
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KNXSwitch(SwitchDevice):
|
|
|
|
"""Representation of a KNX switch."""
|
|
|
|
|
2018-10-15 01:29:36 +00:00
|
|
|
def __init__(self, device):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX switch."""
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device = device
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_register_callbacks(self):
|
|
|
|
"""Register callbacks to update hass after device was changed."""
|
2018-02-24 18:24:33 +00:00
|
|
|
async def after_update_callback(device):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Call after device was updated."""
|
2018-02-24 18:24:33 +00:00
|
|
|
await self.async_update_ha_state()
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device.register_device_updated_cb(after_update_callback)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-10-15 01:29:36 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Store register state change callback."""
|
|
|
|
self.async_register_callbacks()
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the KNX device."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.name
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-01-07 21:39:14 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return true if entity is available."""
|
2018-01-07 21:39:14 +00:00
|
|
|
return self.hass.data[DATA_KNX].connected
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return the polling state. Not needed within KNX."""
|
2017-09-07 07:11:55 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.state
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the device on."""
|
2018-08-26 19:25:39 +00:00
|
|
|
await self.device.set_on()
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the device off."""
|
2018-08-26 19:25:39 +00:00
|
|
|
await self.device.set_off()
|