From 854dba112e587a69574f45586548501d470a9c18 Mon Sep 17 00:00:00 2001 From: Jeff Steinbok Date: Wed, 19 Jul 2023 02:15:25 -0700 Subject: [PATCH] More forgiving of missing preset modes. Bit more error logging. --- custom_components/dreo/fan.py | 10 ++++++++-- custom_components/dreo/pydreo/models.py | 4 ++-- .../dreo/pydreo/pydreoaircirculatorfan.py | 4 ++++ custom_components/dreo/pydreo/pydreobasedevice.py | 2 +- custom_components/dreo/pydreo/pydreofan.py | 4 ++++ custom_components/dreo/pydreo/pydreotowerfan.py | 4 ++++ 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/custom_components/dreo/fan.py b/custom_components/dreo/fan.py index 5f5845f..d3907b1 100644 --- a/custom_components/dreo/fan.py +++ b/custom_components/dreo/fan.py @@ -81,7 +81,10 @@ def preset_modes(self) -> list[str]: @property def preset_mode(self) -> str | None: """Get the current preset mode.""" - return self.device.preset_mode + if (self.device.supports_preset_modes): + return self.device.preset_mode + else: + return None @property def extra_state_attributes(self) -> dict[str, Any]: @@ -94,7 +97,10 @@ def extra_state_attributes(self) -> dict[str, Any]: @property def supported_features(self) -> int: """Return the list of supported features.""" - supported_features = FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE + supported_features = FanEntityFeature.SET_SPEED + + if (self.device.supports_preset_modes): + supported_features = supported_features | FanEntityFeature.PRESET_MODE if (self.device.supports_oscillation): supported_features = supported_features | FanEntityFeature.OSCILLATE diff --git a/custom_components/dreo/pydreo/models.py b/custom_components/dreo/pydreo/models.py index 9241eb6..fcc0d9f 100644 --- a/custom_components/dreo/pydreo/models.py +++ b/custom_components/dreo/pydreo/models.py @@ -38,7 +38,7 @@ class DreoFanDetails: "DR-HTF008S": DreoFanDetails( preset_modes=[FAN_MODE_NORMAL, FAN_MODE_NATURAL, FAN_MODE_SLEEP, FAN_MODE_AUTO], speed_range=(1, 5), - ), + ), } SUPPORTED_AIR_CIRCULATOR_FANS = { @@ -53,5 +53,5 @@ class DreoFanDetails: "DR-HAF004S": DreoFanDetails( preset_modes=[FAN_MODE_NORMAL, FAN_MODE_NATURAL, FAN_MODE_SLEEP, FAN_MODE_AUTO, FAN_MODE_TURBO], speed_range=(1, 9) - ) + ), } diff --git a/custom_components/dreo/pydreo/pydreoaircirculatorfan.py b/custom_components/dreo/pydreo/pydreoaircirculatorfan.py index 4e2021a..88562e9 100644 --- a/custom_components/dreo/pydreo/pydreoaircirculatorfan.py +++ b/custom_components/dreo/pydreo/pydreoaircirculatorfan.py @@ -47,6 +47,10 @@ def is_on(self): """Returns `True` if the device is on, `False` otherwise.""" return self._is_on + @property + def supports_preset_modes(self): + return self._wind_mode is not None + @property def preset_mode(self): return self._fan_definition.preset_modes[self._wind_mode - 1] diff --git a/custom_components/dreo/pydreo/pydreobasedevice.py b/custom_components/dreo/pydreo/pydreobasedevice.py index 324d1f2..4e2d862 100644 --- a/custom_components/dreo/pydreo/pydreobasedevice.py +++ b/custom_components/dreo/pydreo/pydreobasedevice.py @@ -73,7 +73,7 @@ def get_state_update_value(self, state: dict, key: str): if (keyValObject is not None): return keyValObject[STATE_KEY] - _LOGGER.debug("Expected state value {0} not present. Device: {1}".format(key, self.name)) + _LOGGER.error("Expected state value ({0}) not present. Device: {1}".format(key, self.name)) return None def update_state(self, state: dict): diff --git a/custom_components/dreo/pydreo/pydreofan.py b/custom_components/dreo/pydreo/pydreofan.py index 1e8a34e..0518a8e 100644 --- a/custom_components/dreo/pydreo/pydreofan.py +++ b/custom_components/dreo/pydreo/pydreofan.py @@ -65,6 +65,10 @@ def preset_mode(self): def temperature(self): return self._temperature + @property + def supports_preset_modes(self): + pass + @property def supports_oscillation(self): pass diff --git a/custom_components/dreo/pydreo/pydreotowerfan.py b/custom_components/dreo/pydreo/pydreotowerfan.py index 4974dc8..d4e1bb2 100644 --- a/custom_components/dreo/pydreo/pydreotowerfan.py +++ b/custom_components/dreo/pydreo/pydreotowerfan.py @@ -45,6 +45,10 @@ def handle_server_update(self, message: dict): valShakeHorizon = self.get_server_update_key_value(message, SHAKEHORIZON_KEY) if (isinstance(valShakeHorizon, bool)): self._oscillating = valShakeHorizon + + @property + def supports_preset_modes(self): + return self._windType is not None @property def preset_mode(self):