11"""Support for LCN covers."""
22
3+ import asyncio
34from collections .abc import Iterable
5+ from datetime import timedelta
46from functools import partial
57from typing import Any
68
2729from .helpers import InputType , LcnConfigEntry
2830
2931PARALLEL_UPDATES = 0
32+ SCAN_INTERVAL = timedelta (minutes = 1 )
3033
3134
3235def add_lcn_entities (
@@ -73,7 +76,7 @@ async def async_setup_entry(
7376class LcnOutputsCover (LcnEntity , CoverEntity ):
7477 """Representation of a LCN cover connected to output ports."""
7578
76- _attr_is_closed = False
79+ _attr_is_closed = True
7780 _attr_is_closing = False
7881 _attr_is_opening = False
7982 _attr_assumed_state = True
@@ -93,28 +96,6 @@ def __init__(self, config: ConfigType, config_entry: LcnConfigEntry) -> None:
9396 else :
9497 self .reverse_time = None
9598
96- async def async_added_to_hass (self ) -> None :
97- """Run when entity about to be added to hass."""
98- await super ().async_added_to_hass ()
99- if not self .device_connection .is_group :
100- await self .device_connection .activate_status_request_handler (
101- pypck .lcn_defs .OutputPort ["OUTPUTUP" ]
102- )
103- await self .device_connection .activate_status_request_handler (
104- pypck .lcn_defs .OutputPort ["OUTPUTDOWN" ]
105- )
106-
107- async def async_will_remove_from_hass (self ) -> None :
108- """Run when entity will be removed from hass."""
109- await super ().async_will_remove_from_hass ()
110- if not self .device_connection .is_group :
111- await self .device_connection .cancel_status_request_handler (
112- pypck .lcn_defs .OutputPort ["OUTPUTUP" ]
113- )
114- await self .device_connection .cancel_status_request_handler (
115- pypck .lcn_defs .OutputPort ["OUTPUTDOWN" ]
116- )
117-
11899 async def async_close_cover (self , ** kwargs : Any ) -> None :
119100 """Close the cover."""
120101 state = pypck .lcn_defs .MotorStateModifier .DOWN
@@ -147,6 +128,18 @@ async def async_stop_cover(self, **kwargs: Any) -> None:
147128 self ._attr_is_opening = False
148129 self .async_write_ha_state ()
149130
131+ async def async_update (self ) -> None :
132+ """Update the state of the entity."""
133+ if not self .device_connection .is_group :
134+ await asyncio .gather (
135+ self .device_connection .request_status_output (
136+ pypck .lcn_defs .OutputPort ["OUTPUTUP" ], SCAN_INTERVAL .seconds
137+ ),
138+ self .device_connection .request_status_output (
139+ pypck .lcn_defs .OutputPort ["OUTPUTDOWN" ], SCAN_INTERVAL .seconds
140+ ),
141+ )
142+
150143 def input_received (self , input_obj : InputType ) -> None :
151144 """Set cover states when LCN input object (command) is received."""
152145 if (
@@ -175,7 +168,7 @@ def input_received(self, input_obj: InputType) -> None:
175168class LcnRelayCover (LcnEntity , CoverEntity ):
176169 """Representation of a LCN cover connected to relays."""
177170
178- _attr_is_closed = False
171+ _attr_is_closed = True
179172 _attr_is_closing = False
180173 _attr_is_opening = False
181174 _attr_assumed_state = True
@@ -206,20 +199,6 @@ def __init__(self, config: ConfigType, config_entry: LcnConfigEntry) -> None:
206199 self ._is_closing = False
207200 self ._is_opening = False
208201
209- async def async_added_to_hass (self ) -> None :
210- """Run when entity about to be added to hass."""
211- await super ().async_added_to_hass ()
212- if not self .device_connection .is_group :
213- await self .device_connection .activate_status_request_handler (
214- self .motor , self .positioning_mode
215- )
216-
217- async def async_will_remove_from_hass (self ) -> None :
218- """Run when entity will be removed from hass."""
219- await super ().async_will_remove_from_hass ()
220- if not self .device_connection .is_group :
221- await self .device_connection .cancel_status_request_handler (self .motor )
222-
223202 async def async_close_cover (self , ** kwargs : Any ) -> None :
224203 """Close the cover."""
225204 if not await self .device_connection .control_motor_relays (
@@ -274,6 +253,17 @@ async def async_set_cover_position(self, **kwargs: Any) -> None:
274253
275254 self .async_write_ha_state ()
276255
256+ async def async_update (self ) -> None :
257+ """Update the state of the entity."""
258+ coros = [self .device_connection .request_status_relays (SCAN_INTERVAL .seconds )]
259+ if self .positioning_mode == pypck .lcn_defs .MotorPositioningMode .BS4 :
260+ coros .append (
261+ self .device_connection .request_status_motor_position (
262+ self .motor , self .positioning_mode , SCAN_INTERVAL .seconds
263+ )
264+ )
265+ await asyncio .gather (* coros )
266+
277267 def input_received (self , input_obj : InputType ) -> None :
278268 """Set cover states when LCN input object (command) is received."""
279269 if isinstance (input_obj , pypck .inputs .ModStatusRelays ):
0 commit comments