From 048d649b4dff20aff2a0baa81cfdb9c7d3ce71c6 Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Tue, 30 Jan 2024 12:57:45 -0500 Subject: [PATCH] Create a `window_covering_mode` attribute and simplify inversion switch --- .../zha/core/cluster_handlers/closures.py | 5 +++++ homeassistant/components/zha/switch.py | 22 ++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/zha/core/cluster_handlers/closures.py b/homeassistant/components/zha/core/cluster_handlers/closures.py index 879765aec3c..5782e1745ae 100644 --- a/homeassistant/components/zha/core/cluster_handlers/closures.py +++ b/homeassistant/components/zha/core/cluster_handlers/closures.py @@ -270,3 +270,8 @@ class WindowCoveringClusterHandler(ClusterHandler): def window_covering_type(self) -> WindowCovering.WindowCoveringType | None: """Return the window covering type.""" return self.cluster.get(WindowCovering.AttributeDefs.window_covering_type.name) + + @property + def window_covering_mode(self) -> WindowCovering.WindowCoveringMode | None: + """Return the window covering mode.""" + return self.cluster.get(WindowCovering.AttributeDefs.window_covering_mode.name) diff --git a/homeassistant/components/zha/switch.py b/homeassistant/components/zha/switch.py index 57b84bd1aa1..1a0fbae9f9a 100644 --- a/homeassistant/components/zha/switch.py +++ b/homeassistant/components/zha/switch.py @@ -635,17 +635,19 @@ class WindowCoveringInversionSwitch(ZHASwitchConfigurationEntity): async def _async_on_off(self, invert: bool) -> None: """Turn the entity on or off.""" - name: str = WindowCovering.AttributeDefs.window_covering_mode.name - current_mode: WindowCoveringMode = WindowCoveringMode( - self._cluster_handler.cluster.get(name) - ) - send_command: bool = False + if (current_mode := self._cluster_handler.window_covering_mode) is None: + await self.async_update() + + current_mode = self._cluster_handler.window_covering_mode + if invert and WindowCoveringMode.Motor_direction_reversed not in current_mode: current_mode |= WindowCoveringMode.Motor_direction_reversed - send_command = True elif not invert and WindowCoveringMode.Motor_direction_reversed in current_mode: current_mode &= ~WindowCoveringMode.Motor_direction_reversed - send_command = True - if send_command: - await self._cluster_handler.write_attributes_safe({name: current_mode}) - await self.async_update() + else: + return + + await self._cluster_handler.write_attributes_safe( + {WindowCovering.AttributeDefs.window_covering_mode.name: current_mode} + ) + await self.async_update()