From d710b8d5ad37cf49b29d7fb9aaafd5474baa02a8 Mon Sep 17 00:00:00 2001 From: BelowAverageDev Date: Sat, 28 Jan 2023 07:33:10 +0000 Subject: [PATCH 1/2] add is_closed, is_opening, is_closing logic --- homeassistant/components/switchbot/cover.py | 32 +++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switchbot/cover.py b/homeassistant/components/switchbot/cover.py index a4749fe1f112a..40d25dd2f1d9f 100644 --- a/homeassistant/components/switchbot/cover.py +++ b/homeassistant/components/switchbot/cover.py @@ -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.""" @@ -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.""" @@ -170,5 +176,27 @@ 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. Checking + # motion down first works around this peculiarity + 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() From fa46bc2e2e5981528c7c9f94656a597af0d87f9a Mon Sep 17 00:00:00 2001 From: BelowAverageDev Date: Sat, 28 Jan 2023 08:12:30 +0000 Subject: [PATCH 2/2] remove outdated comment about motion direction --- homeassistant/components/switchbot/cover.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/switchbot/cover.py b/homeassistant/components/switchbot/cover.py index 40d25dd2f1d9f..70272c4bd8468 100644 --- a/homeassistant/components/switchbot/cover.py +++ b/homeassistant/components/switchbot/cover.py @@ -183,8 +183,7 @@ def _handle_coordinator_update(self) -> None: _moving_down = ( self.parsed_data["motionDirection"]["down"] and self.parsed_data["inMotion"] ) - # NOTE: when motion is down, motion up is also set to true. Checking - # motion down first works around this peculiarity + # 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