2019-02-23 09:13:15 +00:00
|
|
|
"""Support for LCN covers."""
|
2019-04-24 02:20:20 +00:00
|
|
|
import pypck
|
|
|
|
|
2020-04-25 16:07:15 +00:00
|
|
|
from homeassistant.components.cover import CoverEntity
|
2019-02-23 09:13:15 +00:00
|
|
|
from homeassistant.const import CONF_ADDRESS
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
from . import LcnEntity
|
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
|
|
|
|
2020-11-29 15:30:17 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
class LcnOutputsCover(LcnEntity, CoverEntity):
|
2019-07-29 18:49:44 +00:00
|
|
|
"""Representation of a LCN cover connected to output ports."""
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
def __init__(self, config, device_connection):
|
2019-07-29 18:49:44 +00:00
|
|
|
"""Initialize the LCN cover."""
|
2020-12-05 11:57:49 +00:00
|
|
|
super().__init__(config, device_connection)
|
2019-07-29 18:49:44 +00:00
|
|
|
|
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
|
2020-06-24 13:10:56 +00:00
|
|
|
|
|
|
|
self._is_closed = False
|
|
|
|
self._is_closing = False
|
|
|
|
self._is_opening = False
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when entity about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2020-12-05 11:57:49 +00:00
|
|
|
await self.device_connection.activate_status_request_handler(
|
2019-07-31 19:25:30 +00:00
|
|
|
pypck.lcn_defs.OutputPort["OUTPUTUP"]
|
|
|
|
)
|
2020-12-05 11:57:49 +00:00
|
|
|
await self.device_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."""
|
2020-06-24 13:10:56 +00:00
|
|
|
return self._is_closed
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_opening(self):
|
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
return self._is_opening
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closing(self):
|
|
|
|
"""Return if the cover is closing or not."""
|
|
|
|
return self._is_closing
|
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return True if unable to access real state of the entity."""
|
|
|
|
return True
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs):
|
|
|
|
"""Close the cover."""
|
2020-11-29 15:30:17 +00:00
|
|
|
state = pypck.lcn_defs.MotorStateModifier.DOWN
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_motors_outputs(
|
2020-11-29 15:30:17 +00:00
|
|
|
state, self.reverse_time
|
|
|
|
):
|
|
|
|
return
|
2020-06-24 13:10:56 +00:00
|
|
|
self._is_opening = False
|
|
|
|
self._is_closing = True
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs):
|
|
|
|
"""Open the cover."""
|
2020-11-29 15:30:17 +00:00
|
|
|
state = pypck.lcn_defs.MotorStateModifier.UP
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_motors_outputs(
|
2020-11-29 15:30:17 +00:00
|
|
|
state, self.reverse_time
|
|
|
|
):
|
|
|
|
return
|
2020-06-24 13:10:56 +00:00
|
|
|
self._is_closed = False
|
|
|
|
self._is_opening = True
|
|
|
|
self._is_closing = False
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
async def async_stop_cover(self, **kwargs):
|
|
|
|
"""Stop the cover."""
|
2020-11-29 15:30:17 +00:00
|
|
|
state = pypck.lcn_defs.MotorStateModifier.STOP
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_motors_outputs(state):
|
2020-11-29 15:30:17 +00:00
|
|
|
return
|
2020-06-24 13:10:56 +00:00
|
|
|
self._is_closing = False
|
|
|
|
self._is_opening = False
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-06-24 13:10:56 +00:00
|
|
|
if input_obj.get_percent() > 0: # motor is on
|
|
|
|
if input_obj.get_output_id() == self.output_ids[0]:
|
|
|
|
self._is_opening = True
|
|
|
|
self._is_closing = False
|
|
|
|
else: # self.output_ids[1]
|
|
|
|
self._is_opening = False
|
|
|
|
self._is_closing = True
|
|
|
|
self._is_closed = self._is_closing
|
|
|
|
else: # motor is off
|
|
|
|
# cover is assumed to be closed if we were in closing state before
|
|
|
|
self._is_closed = self._is_closing
|
|
|
|
self._is_closing = False
|
|
|
|
self._is_opening = False
|
2019-07-29 18:49:44 +00:00
|
|
|
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2019-07-29 18:49:44 +00:00
|
|
|
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
class LcnRelayCover(LcnEntity, CoverEntity):
|
2019-07-29 18:49:44 +00:00
|
|
|
"""Representation of a LCN cover connected to relays."""
|
2019-02-23 09:13:15 +00:00
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
def __init__(self, config, device_connection):
|
2019-02-23 09:13:15 +00:00
|
|
|
"""Initialize the LCN cover."""
|
2020-12-05 11:57:49 +00:00
|
|
|
super().__init__(config, device_connection)
|
2019-02-23 09:13:15 +00:00
|
|
|
|
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
|
|
|
|
|
2020-05-27 21:08:51 +00:00
|
|
|
self._is_closed = False
|
|
|
|
self._is_closing = False
|
|
|
|
self._is_opening = False
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when entity about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2021-01-06 15:47:02 +00:00
|
|
|
await self.device_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."""
|
2020-05-27 21:08:51 +00:00
|
|
|
return self._is_closed
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_opening(self):
|
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
return self._is_opening
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closing(self):
|
|
|
|
"""Return if the cover is closing or not."""
|
|
|
|
return self._is_closing
|
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return True if unable to access real state of the entity."""
|
|
|
|
return True
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs):
|
|
|
|
"""Close the cover."""
|
2019-05-25 09:40:44 +00:00
|
|
|
states = [pypck.lcn_defs.MotorStateModifier.NOCHANGE] * 4
|
|
|
|
states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.DOWN
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_motors_relays(states):
|
2020-11-29 15:30:17 +00:00
|
|
|
return
|
|
|
|
self._is_opening = False
|
|
|
|
self._is_closing = True
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs):
|
|
|
|
"""Open the cover."""
|
2020-11-29 15:30:17 +00:00
|
|
|
states = [pypck.lcn_defs.MotorStateModifier.NOCHANGE] * 4
|
|
|
|
states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.UP
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_motors_relays(states):
|
2020-11-29 15:30:17 +00:00
|
|
|
return
|
2020-05-27 21:08:51 +00:00
|
|
|
self._is_closed = False
|
|
|
|
self._is_opening = True
|
|
|
|
self._is_closing = False
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
async def async_stop_cover(self, **kwargs):
|
|
|
|
"""Stop the cover."""
|
2019-05-25 09:40:44 +00:00
|
|
|
states = [pypck.lcn_defs.MotorStateModifier.NOCHANGE] * 4
|
|
|
|
states[self.motor.value] = pypck.lcn_defs.MotorStateModifier.STOP
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_motors_relays(states):
|
2020-11-29 15:30:17 +00:00
|
|
|
return
|
|
|
|
self._is_closing = False
|
|
|
|
self._is_opening = False
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-02-23 09:13:15 +00:00
|
|
|
|
|
|
|
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
|
2020-05-27 21:08:51 +00:00
|
|
|
self._is_opening = not states[self.motor_port_updown] # set direction
|
|
|
|
self._is_closing = states[self.motor_port_updown] # set direction
|
2020-06-24 13:10:56 +00:00
|
|
|
else: # motor is off
|
2020-05-27 21:08:51 +00:00
|
|
|
self._is_opening = False
|
|
|
|
self._is_closing = False
|
2020-06-24 13:10:56 +00:00
|
|
|
self._is_closed = states[self.motor_port_updown]
|
2019-02-23 09:13:15 +00:00
|
|
|
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|