2019-02-23 09:13:15 +00:00
|
|
|
"""Support for LCN covers."""
|
2019-04-24 02:20:20 +00:00
|
|
|
import pypck
|
|
|
|
|
2019-02-23 09:13:15 +00:00
|
|
|
from homeassistant.components.cover import CoverDevice
|
|
|
|
from homeassistant.const import CONF_ADDRESS
|
|
|
|
|
2019-05-25 09:40:44 +00:00
|
|
|
from . import LcnDevice
|
2019-07-29 18:49:44 +00:00
|
|
|
from .const import CONF_CONNECTIONS, CONF_MOTOR, CONF_REVERSE_TIME, DATA_LCN
|
2019-05-25 09:40:44 +00:00
|
|
|
from .helpers import get_connection
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-02-23 09:13:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, hass_config, async_add_entities, discovery_info=None
|
|
|
|
):
|
2019-02-23 09:13:15 +00:00
|
|
|
"""Setups the LCN cover platform."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
devices = []
|
|
|
|
for config in discovery_info:
|
|
|
|
address, connection_id = config[CONF_ADDRESS]
|
|
|
|
addr = pypck.lcn_addr.LcnAddr(*address)
|
|
|
|
connections = hass.data[DATA_LCN][CONF_CONNECTIONS]
|
|
|
|
connection = get_connection(connections, connection_id)
|
|
|
|
address_connection = connection.get_address_conn(addr)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if config[CONF_MOTOR] == "OUTPUTS":
|
2019-07-29 18:49:44 +00:00
|
|
|
devices.append(LcnOutputsCover(config, address_connection))
|
2019-07-31 19:25:30 +00:00
|
|
|
else: # RELAYS
|
2019-07-29 18:49:44 +00:00
|
|
|
devices.append(LcnRelayCover(config, address_connection))
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
async_add_entities(devices)
|
|
|
|
|
|
|
|
|
2019-07-29 18:49:44 +00:00
|
|
|
class LcnOutputsCover(LcnDevice, CoverDevice):
|
|
|
|
"""Representation of a LCN cover connected to output ports."""
|
|
|
|
|
|
|
|
def __init__(self, config, address_connection):
|
|
|
|
"""Initialize the LCN cover."""
|
|
|
|
super().__init__(config, address_connection)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.output_ids = [
|
|
|
|
pypck.lcn_defs.OutputPort["OUTPUTUP"].value,
|
|
|
|
pypck.lcn_defs.OutputPort["OUTPUTDOWN"].value,
|
|
|
|
]
|
2019-07-29 18:49:44 +00:00
|
|
|
if CONF_REVERSE_TIME in config:
|
|
|
|
self.reverse_time = pypck.lcn_defs.MotorReverseTime[
|
2019-07-31 19:25:30 +00:00
|
|
|
config[CONF_REVERSE_TIME]
|
|
|
|
]
|
2019-07-29 18:49:44 +00:00
|
|
|
else:
|
|
|
|
self.reverse_time = None
|
|
|
|
self._closed = None
|
|
|
|
self.state_up = False
|
|
|
|
self.state_down = False
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when entity about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
await self.address_connection.activate_status_request_handler(
|
2019-07-31 19:25:30 +00:00
|
|
|
pypck.lcn_defs.OutputPort["OUTPUTUP"]
|
|
|
|
)
|
2019-07-29 18:49:44 +00:00
|
|
|
await self.address_connection.activate_status_request_handler(
|
2019-07-31 19:25:30 +00:00
|
|
|
pypck.lcn_defs.OutputPort["OUTPUTDOWN"]
|
|
|
|
)
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self):
|
|
|
|
"""Return if the cover is closed."""
|
|
|
|
return self._closed
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs):
|
|
|
|
"""Close the cover."""
|
|
|
|
self._closed = True
|
|
|
|
|
|
|
|
state = pypck.lcn_defs.MotorStateModifier.DOWN
|
2019-07-31 19:25:30 +00:00
|
|
|
self.address_connection.control_motors_outputs(state)
|
2019-07-29 18:49:44 +00:00
|
|
|
await self.async_update_ha_state()
|
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs):
|
|
|
|
"""Open the cover."""
|
|
|
|
self._closed = False
|
|
|
|
state = pypck.lcn_defs.MotorStateModifier.UP
|
2019-07-31 19:25:30 +00:00
|
|
|
self.address_connection.control_motors_outputs(state, self.reverse_time)
|
2019-07-29 18:49:44 +00:00
|
|
|
await self.async_update_ha_state()
|
|
|
|
|
|
|
|
async def async_stop_cover(self, **kwargs):
|
|
|
|
"""Stop the cover."""
|
|
|
|
self._closed = None
|
|
|
|
state = pypck.lcn_defs.MotorStateModifier.STOP
|
2019-07-31 19:25:30 +00:00
|
|
|
self.address_connection.control_motors_outputs(state, self.reverse_time)
|
2019-07-29 18:49:44 +00:00
|
|
|
await self.async_update_ha_state()
|
|
|
|
|
|
|
|
def input_received(self, input_obj):
|
|
|
|
"""Set cover states when LCN input object (command) is received."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
not isinstance(input_obj, pypck.inputs.ModStatusOutput)
|
|
|
|
or input_obj.get_output_id() not in self.output_ids
|
|
|
|
):
|
2019-07-29 18:49:44 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if input_obj.get_output_id() == self.output_ids[0]:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.state_up = input_obj.get_percent() > 0
|
|
|
|
else: # self.output_ids[1]
|
|
|
|
self.state_down = input_obj.get_percent() > 0
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
if self.state_up and not self.state_down:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._closed = False # Cover open
|
2019-07-29 18:49:44 +00:00
|
|
|
elif self.state_down and not self.state_up:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._closed = True # Cover closed
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
class LcnRelayCover(LcnDevice, CoverDevice):
|
|
|
|
"""Representation of a LCN cover connected to relays."""
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
def __init__(self, config, address_connection):
|
|
|
|
"""Initialize the LCN cover."""
|
|
|
|
super().__init__(config, address_connection)
|
|
|
|
|
2019-05-25 09:40:44 +00:00
|
|
|
self.motor = pypck.lcn_defs.MotorPort[config[CONF_MOTOR]]
|
2019-02-23 09:13:15 +00:00
|
|
|
self.motor_port_onoff = self.motor.value * 2
|
|
|
|
self.motor_port_updown = self.motor_port_onoff + 1
|
|
|
|
|
|
|
|
self._closed = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when entity about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2019-07-31 19:25:30 +00:00
|
|
|
await self.address_connection.activate_status_request_handler(self.motor)
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self):
|
|
|
|
"""Return if the cover is closed."""
|
|
|
|
return self._closed
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs):
|
|
|
|
"""Close the cover."""
|
|
|
|
self._closed = True
|
2019-05-25 09:40:44 +00:00
|
|
|
states = [pypck.lcn_defs.MotorStateModifier.NOCHANGE] * 4
|
|
|
|
states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.DOWN
|
2019-07-29 18:49:44 +00:00
|
|
|
self.address_connection.control_motors_relays(states)
|
2019-02-23 09:13:15 +00:00
|
|
|
await self.async_update_ha_state()
|
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs):
|
|
|
|
"""Open the cover."""
|
|
|
|
self._closed = False
|
2019-05-25 09:40:44 +00:00
|
|
|
states = [pypck.lcn_defs.MotorStateModifier.NOCHANGE] * 4
|
|
|
|
states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.UP
|
2019-07-29 18:49:44 +00:00
|
|
|
self.address_connection.control_motors_relays(states)
|
2019-02-23 09:13:15 +00:00
|
|
|
await self.async_update_ha_state()
|
|
|
|
|
|
|
|
async def async_stop_cover(self, **kwargs):
|
|
|
|
"""Stop the cover."""
|
|
|
|
self._closed = None
|
2019-05-25 09:40:44 +00:00
|
|
|
states = [pypck.lcn_defs.MotorStateModifier.NOCHANGE] * 4
|
|
|
|
states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.STOP
|
2019-07-29 18:49:44 +00:00
|
|
|
self.address_connection.control_motors_relays(states)
|
2019-02-23 09:13:15 +00:00
|
|
|
await self.async_update_ha_state()
|
|
|
|
|
|
|
|
def input_received(self, input_obj):
|
|
|
|
"""Set cover states when LCN input object (command) is received."""
|
2019-05-25 09:40:44 +00:00
|
|
|
if not isinstance(input_obj, pypck.inputs.ModStatusRelays):
|
2019-02-23 09:13:15 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
states = input_obj.states # list of boolean values (relay on/off)
|
|
|
|
if states[self.motor_port_onoff]: # motor is on
|
|
|
|
self._closed = states[self.motor_port_updown] # set direction
|
|
|
|
|
|
|
|
self.async_schedule_update_ha_state()
|