2017-06-19 05:30:39 +00:00
|
|
|
"""
|
2017-09-07 07:11:55 +00:00
|
|
|
Support for KNX/IP covers.
|
2017-06-19 05:30:39 +00:00
|
|
|
|
2017-06-20 10:09:42 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/cover.knx/
|
2017-06-19 05:30:39 +00:00
|
|
|
"""
|
2017-09-07 07:11:55 +00:00
|
|
|
import asyncio
|
2017-06-19 05:30:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
from homeassistant.components.knx import DATA_KNX, ATTR_DISCOVER_DEVICES
|
|
|
|
from homeassistant.helpers.event import async_track_utc_time_change
|
2017-06-19 05:30:39 +00:00
|
|
|
from homeassistant.components.cover import (
|
2017-09-07 07:11:55 +00:00
|
|
|
CoverDevice, PLATFORM_SCHEMA, SUPPORT_OPEN, SUPPORT_CLOSE,
|
|
|
|
SUPPORT_SET_POSITION, SUPPORT_STOP, SUPPORT_SET_TILT_POSITION,
|
|
|
|
ATTR_POSITION, ATTR_TILT_POSITION)
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.const import CONF_NAME
|
2017-06-19 05:30:39 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
CONF_MOVE_LONG_ADDRESS = 'move_long_address'
|
|
|
|
CONF_MOVE_SHORT_ADDRESS = 'move_short_address'
|
|
|
|
CONF_POSITION_ADDRESS = 'position_address'
|
|
|
|
CONF_POSITION_STATE_ADDRESS = 'position_state_address'
|
|
|
|
CONF_ANGLE_ADDRESS = 'angle_address'
|
|
|
|
CONF_ANGLE_STATE_ADDRESS = 'angle_state_address'
|
|
|
|
CONF_TRAVELLING_TIME_DOWN = 'travelling_time_down'
|
|
|
|
CONF_TRAVELLING_TIME_UP = 'travelling_time_up'
|
2017-06-22 11:42:13 +00:00
|
|
|
CONF_INVERT_POSITION = 'invert_position'
|
2017-06-28 12:08:07 +00:00
|
|
|
CONF_INVERT_ANGLE = 'invert_angle'
|
2017-06-19 05:30:39 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
DEFAULT_TRAVEL_TIME = 25
|
2017-06-19 05:30:39 +00:00
|
|
|
DEFAULT_NAME = 'KNX Cover'
|
|
|
|
DEPENDENCIES = ['knx']
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2017-09-07 07:11:55 +00:00
|
|
|
vol.Optional(CONF_MOVE_LONG_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_MOVE_SHORT_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_POSITION_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_POSITION_STATE_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_ANGLE_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_ANGLE_STATE_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_TRAVELLING_TIME_DOWN, default=DEFAULT_TRAVEL_TIME):
|
|
|
|
cv.positive_int,
|
|
|
|
vol.Optional(CONF_TRAVELLING_TIME_UP, default=DEFAULT_TRAVEL_TIME):
|
|
|
|
cv.positive_int,
|
2017-06-22 11:42:13 +00:00
|
|
|
vol.Optional(CONF_INVERT_POSITION, default=False): cv.boolean,
|
2017-06-28 12:08:07 +00:00
|
|
|
vol.Optional(CONF_INVERT_ANGLE, default=False): cv.boolean,
|
2017-06-19 05:30:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, add_devices,
|
|
|
|
discovery_info=None):
|
|
|
|
"""Set up cover(s) for KNX platform."""
|
|
|
|
if DATA_KNX not in hass.data \
|
|
|
|
or not hass.data[DATA_KNX].initialized:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if discovery_info is not None:
|
|
|
|
async_add_devices_discovery(hass, discovery_info, add_devices)
|
|
|
|
else:
|
|
|
|
async_add_devices_config(hass, config, add_devices)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_devices_discovery(hass, discovery_info, add_devices):
|
|
|
|
"""Set up covers 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(KNXCover(hass, device))
|
|
|
|
add_devices(entities)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_devices_config(hass, config, add_devices):
|
|
|
|
"""Set up cover for KNX platform configured within plattform."""
|
|
|
|
import xknx
|
|
|
|
cover = xknx.devices.Cover(
|
|
|
|
hass.data[DATA_KNX].xknx,
|
|
|
|
name=config.get(CONF_NAME),
|
|
|
|
group_address_long=config.get(CONF_MOVE_LONG_ADDRESS),
|
|
|
|
group_address_short=config.get(CONF_MOVE_SHORT_ADDRESS),
|
|
|
|
group_address_position_state=config.get(
|
|
|
|
CONF_POSITION_STATE_ADDRESS),
|
|
|
|
group_address_angle=config.get(CONF_ANGLE_ADDRESS),
|
|
|
|
group_address_angle_state=config.get(CONF_ANGLE_STATE_ADDRESS),
|
|
|
|
group_address_position=config.get(CONF_POSITION_ADDRESS),
|
|
|
|
travel_time_down=config.get(CONF_TRAVELLING_TIME_DOWN),
|
|
|
|
travel_time_up=config.get(CONF_TRAVELLING_TIME_UP))
|
|
|
|
|
|
|
|
invert_position = config.get(CONF_INVERT_POSITION)
|
|
|
|
invert_angle = config.get(CONF_INVERT_ANGLE)
|
|
|
|
hass.data[DATA_KNX].xknx.devices.add(cover)
|
|
|
|
add_devices([KNXCover(hass, cover, invert_position, invert_angle)])
|
|
|
|
|
|
|
|
|
|
|
|
class KNXCover(CoverDevice):
|
|
|
|
"""Representation of a KNX cover."""
|
|
|
|
|
|
|
|
def __init__(self, hass, device, invert_position=False,
|
|
|
|
invert_angle=False):
|
2017-06-19 05:30:39 +00:00
|
|
|
"""Initialize the cover."""
|
2017-09-07 07:11:55 +00:00
|
|
|
self.device = device
|
|
|
|
self.invert_position = invert_position
|
|
|
|
self.invert_angle = invert_angle
|
|
|
|
self.hass = hass
|
|
|
|
self.async_register_callbacks()
|
|
|
|
|
|
|
|
self._unsubscribe_auto_updater = None
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_register_callbacks(self):
|
|
|
|
"""Register callbacks to update hass after device was changed."""
|
|
|
|
@asyncio.coroutine
|
|
|
|
def after_update_callback(device):
|
|
|
|
"""Callback after device was updated."""
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
yield from self.async_update_ha_state()
|
|
|
|
self.device.register_device_updated_cb(after_update_callback)
|
2017-06-28 12:08:07 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the KNX device."""
|
|
|
|
return self.device.name
|
2017-06-19 05:30:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""No polling needed within KNX."""
|
|
|
|
return False
|
2017-06-19 05:30:39 +00:00
|
|
|
|
2017-06-28 12:08:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2017-09-07 07:11:55 +00:00
|
|
|
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | \
|
|
|
|
SUPPORT_SET_POSITION | SUPPORT_STOP
|
|
|
|
if self.device.supports_angle:
|
|
|
|
supported_features |= SUPPORT_SET_TILT_POSITION
|
|
|
|
return supported_features
|
2017-06-19 05:30:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_cover_position(self):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the current position of the cover."""
|
|
|
|
return int(self.from_knx_position(
|
|
|
|
self.device.current_position(),
|
|
|
|
self.invert_position))
|
2017-06-19 05:30:39 +00:00
|
|
|
|
|
|
|
@property
|
2017-09-07 07:11:55 +00:00
|
|
|
def is_closed(self):
|
|
|
|
"""Return if the cover is closed."""
|
|
|
|
return self.device.is_closed()
|
2017-06-19 05:30:39 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_close_cover(self, **kwargs):
|
2017-06-19 05:30:39 +00:00
|
|
|
"""Close the cover."""
|
2017-09-07 07:11:55 +00:00
|
|
|
if not self.device.is_closed():
|
|
|
|
yield from self.device.set_down()
|
|
|
|
self.start_auto_updater()
|
2017-06-19 05:30:39 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_open_cover(self, **kwargs):
|
|
|
|
"""Open the cover."""
|
|
|
|
if not self.device.is_open():
|
|
|
|
yield from self.device.set_up()
|
|
|
|
self.start_auto_updater()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_set_cover_position(self, **kwargs):
|
|
|
|
"""Move the cover to a specific position."""
|
|
|
|
if ATTR_POSITION in kwargs:
|
|
|
|
position = kwargs[ATTR_POSITION]
|
|
|
|
knx_position = self.to_knx_position(position, self.invert_position)
|
|
|
|
yield from self.device.set_position(knx_position)
|
|
|
|
self.start_auto_updater()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_stop_cover(self, **kwargs):
|
|
|
|
"""Stop the cover."""
|
|
|
|
yield from self.device.stop()
|
|
|
|
self.stop_auto_updater()
|
2017-06-28 12:08:07 +00:00
|
|
|
|
2017-06-19 05:30:39 +00:00
|
|
|
@property
|
2017-09-07 07:11:55 +00:00
|
|
|
def current_cover_tilt_position(self):
|
|
|
|
"""Return current tilt position of cover."""
|
|
|
|
if not self.device.supports_angle:
|
|
|
|
return None
|
|
|
|
return int(self.from_knx_position(
|
|
|
|
self.device.angle,
|
|
|
|
self.invert_angle))
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_set_cover_tilt_position(self, **kwargs):
|
|
|
|
"""Move the cover tilt to a specific position."""
|
|
|
|
if ATTR_TILT_POSITION in kwargs:
|
|
|
|
position = kwargs[ATTR_TILT_POSITION]
|
|
|
|
knx_position = self.to_knx_position(position, self.invert_angle)
|
|
|
|
yield from self.device.set_angle(knx_position)
|
|
|
|
|
|
|
|
def start_auto_updater(self):
|
|
|
|
"""Start the autoupdater to update HASS while cover is moving."""
|
|
|
|
if self._unsubscribe_auto_updater is None:
|
|
|
|
self._unsubscribe_auto_updater = async_track_utc_time_change(
|
|
|
|
self.hass, self.auto_updater_hook)
|
|
|
|
|
|
|
|
def stop_auto_updater(self):
|
|
|
|
"""Stop the autoupdater."""
|
|
|
|
if self._unsubscribe_auto_updater is not None:
|
|
|
|
self._unsubscribe_auto_updater()
|
|
|
|
self._unsubscribe_auto_updater = None
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def auto_updater_hook(self, now):
|
|
|
|
"""Callback for autoupdater."""
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
self.hass.async_add_job(self.async_update_ha_state())
|
|
|
|
if self.device.position_reached():
|
|
|
|
self.stop_auto_updater()
|
|
|
|
|
|
|
|
self.hass.add_job(self.device.auto_stop_if_necessary())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_knx_position(raw, invert):
|
|
|
|
"""Convert KNX position [0...255] to hass position [100...0]."""
|
|
|
|
position = round((raw/256)*100)
|
|
|
|
if not invert:
|
|
|
|
position = 100 - position
|
|
|
|
return position
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def to_knx_position(value, invert):
|
|
|
|
"""Convert hass position [100...0] to KNX position [0...255]."""
|
|
|
|
knx_position = round(value/100*255.4)
|
|
|
|
if not invert:
|
|
|
|
knx_position = 255-knx_position
|
|
|
|
print(value, " -> ", knx_position)
|
|
|
|
return knx_position
|