Skip to content

Commit

Permalink
fix(cover): hotfix for 2025.1 update and handle boolean actions state (
Browse files Browse the repository at this point in the history
…#490)

* Auto configure current position for garage
* hotfix for latest update, commands not working.
* isupper check fix.
* handle is_closed for cover.
* handle states better
  • Loading branch information
xZetsubou authored Jan 17, 2025
1 parent 3a63542 commit 801bd9e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
12 changes: 6 additions & 6 deletions custom_components/localtuya/core/ha_entities/covers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ def localtuya_cover(cmd_set, position_mode=None, inverted=False, timed=25):
LocalTuyaEntity(
id=DPCode.SWITCH_1,
name="Door",
custom_configs=localtuya_cover("open_close_stop", "None", True),
current_state=DPCode.DOORCONTACT_STATE,
custom_configs=localtuya_cover("open_close_stop", "none", True),
current_position_dp=DPCode.DOORCONTACT_STATE,
device_class=CoverDeviceClass.GARAGE,
),
LocalTuyaEntity(
id=DPCode.SWITCH_2,
name="Door 2",
custom_configs=localtuya_cover("open_close_stop", "None", True),
current_state=DPCode.DOORCONTACT_STATE_2,
custom_configs=localtuya_cover("open_close_stop", "none", True),
current_position_dp=DPCode.DOORCONTACT_STATE_2,
device_class=CoverDeviceClass.GARAGE,
),
LocalTuyaEntity(
id=DPCode.SWITCH_3,
name="Door 3",
custom_configs=localtuya_cover("open_close_stop", "None", True),
current_state=DPCode.DOORCONTACT_STATE_3,
custom_configs=localtuya_cover("open_close_stop", "none", True),
current_position_dp=DPCode.DOORCONTACT_STATE_3,
device_class=CoverDeviceClass.GARAGE,
),
),
Expand Down
30 changes: 16 additions & 14 deletions custom_components/localtuya/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ def supported_features(self):
def _current_state(self) -> str:
"""Return the current state of the cover."""
state = self._current_state_action
curr_pos = self.current_cover_position
curr_pos = self._current_cover_position
# Reset STATE when cover is fully closed or fully opened.
if (state == STATE_CLOSING and curr_pos == 0) or (
state == STATE_OPENING and curr_pos == 100
if state == STATE_STOPPED or (
state in (STATE_CLOSING, STATE_OPENING) and curr_pos in (0, 100)
):
self._current_state_action = STATE_STOPPED
# in case cover moving by set position cmd.
Expand All @@ -147,20 +147,20 @@ def current_cover_position(self):
@property
def is_opening(self):
"""Return if cover is opening."""
state = self._current_state
return state == STATE_SET_OPENING or state == STATE_OPENING
return self._current_state in (STATE_OPENING, STATE_SET_OPENING)

@property
def is_closing(self):
"""Return if cover is closing."""
state = self._current_state
return state == STATE_SET_CLOSING or state == STATE_CLOSING
return self._current_state in (STATE_CLOSING, STATE_SET_CLOSING)

@property
def is_closed(self):
"""Return if the cover is closed or not."""
if isinstance(self._open_cmd, bool):
return self._state == self._close_cmd
return self._current_cover_position == 0 and (
self._current_state == STATE_STOPPED
)
if self._config[CONF_POSITIONING_MODE] == MODE_NONE:
return None
return self.current_cover_position == 0 and self._current_state == STATE_STOPPED
Expand Down Expand Up @@ -268,7 +268,7 @@ def connection_made(self):
super().connection_made()

match self.dp_value(self._dp_id):
case str() as i if i.upper():
case str() as i if i.isupper():
self._open_cmd = self._open_cmd.upper()
self._close_cmd = self._close_cmd.upper()
self._stop_cmd = self._stop_cmd.upper()
Expand All @@ -283,10 +283,14 @@ def status_updated(self):

if self.has_config(CONF_CURRENT_POSITION_DP):
curr_pos = self.dp_value(CONF_CURRENT_POSITION_DP)
if isinstance(curr_pos, bool):
curr_pos = 0 if curr_pos else 100
elif isinstance(curr_pos, str):
curr_pos = 0 if curr_pos in ("fully_close") else 100
if self._position_inverted:
self._current_cover_position = 100 - curr_pos
else:
self._current_cover_position = curr_pos
curr_pos = 100 - curr_pos

self._current_cover_position = curr_pos
if (
self._config[CONF_POSITIONING_MODE] == MODE_TIME_BASED
and self._state != self._previous_state
Expand Down Expand Up @@ -320,8 +324,6 @@ def status_updated(self):
def update_state(self, action, position=None):
"""Update cover current states."""
state = self._current_state_action
if isinstance(self._open_cmd, bool):
return
# using Commands.
if position is None:
self._current_state_action = action
Expand Down

0 comments on commit 801bd9e

Please sign in to comment.