From 4d2411af490c23b2f379d03e59fb81aed5704df8 Mon Sep 17 00:00:00 2001 From: Ben Vezzani Date: Thu, 13 Apr 2023 13:11:57 -0400 Subject: [PATCH] trying a more direct refactor strategy --- libdyson/dyson_device.py | 48 ++++++++++++++++++++++------------------ libdyson/exceptions.py | 4 ++++ setup.cfg | 2 +- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/libdyson/dyson_device.py b/libdyson/dyson_device.py index 64c4aa1..cf21ffd 100644 --- a/libdyson/dyson_device.py +++ b/libdyson/dyson_device.py @@ -3,7 +3,7 @@ import json import logging import threading -from typing import Any, Optional, List, Dict +from typing import Any, Optional, List, Dict, Union import paho.mqtt.client as mqtt @@ -17,7 +17,7 @@ DysonConnectionRefused, DysonConnectTimeout, DysonInvalidCredential, - DysonNotConnected, + DysonNotConnected, DysonNoEnvironmentalData, ) from .utils import mqtt_time @@ -198,24 +198,6 @@ def __init__(self, serial: str, credential: str, device_type: str): self._environmental_data = {} self._environmental_data_available = threading.Event() - def interview(self) -> None: - if not self._status_data_available.is_set(): - self.request_environmental_data() - self._environmental_data_available.wait(timeout=TIMEOUT) - - def get_test(self): - """ For testing since I don't have formaldehyde devices """ - return 1 - setattr(DysonFanDevice, "test", get_test) - - _LOGGER.warning("checking for hcho") - if "hcho" in self._environmental_data: - _LOGGER.warning("Found hcho") - def get_formaldehyde(self): - """Return formaldehyde reading.""" - return int(self._get_environmental_field_value("hcho")) - setattr(DysonFanDevice, "formaldehyde", property(get_formaldehyde)) - @property def device_type(self) -> str: """Device type.""" @@ -239,6 +221,14 @@ def speed(self) -> Optional[int]: return None return int(speed) + def wait_for_environmental_data(self): + if not self._status_data_available.is_set(): + self.request_current_status() + self._environmental_data_available.wait(timeout=TIMEOUT) + + if not self._environmental_data_available.is_set(): + raise DysonNoEnvironmentalData + @property @abstractmethod def is_on(self) -> bool: @@ -274,6 +264,15 @@ def warning_code(self) -> str: """Return warning code.""" return self._get_field_value(self._status, "wacd") + @property + def formaldehyde(self) -> Optional[int]: + """Return formaldehyde reading.""" + val = self._get_environmental_field_value("hcho") + if val is None: + return None + + return int(val) + @property def humidity(self) -> int: """Return humidity in percentage.""" @@ -296,9 +295,12 @@ def sleep_timer(self) -> int: @staticmethod def _get_field_value(state: Dict[str, Any], field: str): - return state[field][1] if isinstance(state[field], list) else state[field] + try: + return state[field][1] if isinstance(state[field], list) else state[field] + except: + return None - def _get_environmental_field_value(self, field, divisor=1): + def _get_environmental_field_value(self, field, divisor=1) -> Optional[Union[int, float]]: value = self._get_field_value(self._environmental_data, field) if value == "OFF": return ENVIRONMENTAL_OFF @@ -306,6 +308,8 @@ def _get_environmental_field_value(self, field, divisor=1): return ENVIRONMENTAL_INIT if value == "FAIL": return ENVIRONMENTAL_FAIL + if value == "NONE" or value is None: + return None if divisor == 1: return int(value) return float(value) / divisor diff --git a/libdyson/exceptions.py b/libdyson/exceptions.py index 037ef8a..1abeb27 100644 --- a/libdyson/exceptions.py +++ b/libdyson/exceptions.py @@ -55,3 +55,7 @@ class DysonConnectionRefused(DysonException): class DysonFailedToParseWifiInfo(DysonException): """Represents failed to parse WiFi information.""" + + +class DysonNoEnvironmentalData(DysonException): + """Represents mqtt not connected.""" diff --git a/setup.cfg b/setup.cfg index 6dd84ec..ab168f5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = libdyson-neon -version = 0.10.2 +version = 0.11.0 author = The libdyson Working Group author_email = dotvezz@gmail.com license = MIT License