Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Switchbot cover is_closed, is_opening, is_closing logic #86833

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions homeassistant/components/switchbot/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,15 @@ class SwitchBotBlindTiltEntity(SwitchbotEntity, CoverEntity, RestoreEntity):
"""Representation of a Switchbot."""

_device: switchbot.SwitchbotBlindTilt
_attr_device_class = CoverDeviceClass.CURTAIN
_attr_device_class = CoverDeviceClass.BLIND
_attr_supported_features = (
CoverEntityFeature.OPEN_TILT
| CoverEntityFeature.CLOSE_TILT
| CoverEntityFeature.STOP_TILT
| CoverEntityFeature.SET_TILT_POSITION
)
CLOSED_UP_THRESHOLD = 80
CLOSED_DOWN_THRESHOLD = 20

def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
"""Initialize the Switchbot."""
Expand All @@ -137,6 +139,10 @@ async def async_added_to_hass(self) -> None:
ATTR_CURRENT_TILT_POSITION
)
self._last_run_success = last_state.attributes.get("last_run_success")
if (_tilt := self._attr_current_cover_position) is not None:
self._attr_is_closed = (_tilt < self.CLOSED_DOWN_THRESHOLD) or (
_tilt > self.CLOSED_UP_THRESHOLD
)

async def async_open_cover_tilt(self, **kwargs: Any) -> None:
"""Open the tilt."""
Expand Down Expand Up @@ -170,5 +176,26 @@ async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_current_cover_tilt_position = self.parsed_data["tilt"]
_tilt = self.parsed_data["tilt"]
_moving_up = (
self.parsed_data["motionDirection"]["up"] and self.parsed_data["inMotion"]
)
_moving_down = (
self.parsed_data["motionDirection"]["down"] and self.parsed_data["inMotion"]
)
# NOTE: when motion is down, motion up is also set to true for some reason
if _moving_up:
_opening = bool(_tilt > 50)
_closing = not _opening
elif _moving_down:
_opening = bool(_tilt < 50)
_closing = not _opening
else:
_opening = _closing = False
self._attr_current_cover_tilt_position = _tilt
self._attr_is_closed = (_tilt < self.CLOSED_DOWN_THRESHOLD) or (
_tilt > self.CLOSED_UP_THRESHOLD
)
self._attr_is_opening = _opening
self._attr_is_closing = _closing
self.async_write_ha_state()