Skip to content

Commit

Permalink
Implement external state for cover
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Jul 3, 2024
1 parent b951f8b commit c075f46
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
24 changes: 22 additions & 2 deletions zha/application/platforms/cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import asyncio
import functools
import logging
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, Any, Literal, cast

from zigpy.zcl.clusters.general import OnOff
from zigpy.zcl.foundation import Status
Expand Down Expand Up @@ -57,6 +57,10 @@ class Cover(PlatformEntity):
PLATFORM = Platform.COVER

_attr_translation_key: str = "cover"
_attr_extra_state_attribute_names: set[str] = {
"target_lift_position",
"target_tilt_position",
}

def __init__(
self,
Expand Down Expand Up @@ -84,7 +88,7 @@ def __init__(
)
self._target_lift_position: int | None = None
self._target_tilt_position: int | None = None
self._state: str
self._state: str = STATE_OPEN
self._determine_initial_state()
self._cover_cluster_handler.on_event(
CLUSTER_HANDLER_ATTRIBUTE_UPDATED,
Expand All @@ -102,10 +106,26 @@ def state(self) -> dict[str, Any]:
"is_opening": self.is_opening,
"is_closing": self.is_closing,
"is_closed": self.is_closed,
"target_lift_position": self._target_lift_position,
"target_tilt_position": self._target_tilt_position,
}
)
return response

def restore_external_state_attributes(
self,
*,
state: Literal[
"open", "opening", "closed", "closing"
], # FIXME: why must these be expanded?
target_lift_position: int | None,
target_tilt_position: int | None,
):
"""Restore external state attributes."""
self._state = state
self._target_lift_position = target_lift_position
self._target_tilt_position = target_tilt_position

Check warning on line 127 in zha/application/platforms/cover/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/cover/__init__.py#L125-L127

Added lines #L125 - L127 were not covered by tests

@property
def is_closed(self) -> bool | None:
"""Return True if the cover is closed.
Expand Down
8 changes: 4 additions & 4 deletions zha/application/platforms/cover/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
ATTR_POSITION: Final[str] = "position"
ATTR_TILT_POSITION: Final[str] = "tilt_position"

STATE_OPEN: Final[str] = "open"
STATE_OPENING: Final[str] = "opening"
STATE_CLOSED: Final[str] = "closed"
STATE_CLOSING: Final[str] = "closing"
STATE_OPEN: Final = "open"
STATE_OPENING: Final = "opening"
STATE_CLOSED: Final = "closed"
STATE_CLOSING: Final = "closing"


class CoverDeviceClass(StrEnum):
Expand Down

0 comments on commit c075f46

Please sign in to comment.