Skip to content

Commit

Permalink
trying a more direct refactor strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
dotvezz committed May 1, 2023
1 parent acb411b commit 14ea4f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
40 changes: 18 additions & 22 deletions libdyson/dyson_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,7 +17,7 @@
DysonConnectionRefused,
DysonConnectTimeout,
DysonInvalidCredential,
DysonNotConnected,
DysonNotConnected, DysonNoEnvironmentalData,
)
from .utils import mqtt_time

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -274,6 +256,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."""
Expand All @@ -296,16 +287,21 @@ 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
if value == "INIT":
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
Expand Down
4 changes: 4 additions & 0 deletions libdyson/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ class DysonConnectionRefused(DysonException):

class DysonFailedToParseWifiInfo(DysonException):
"""Represents failed to parse WiFi information."""


class DysonNoEnvironmentalData(DysonException):
"""Represents mqtt not connected."""
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = libdyson-neon
version = 0.10.2
version = 0.11.0
author = The libdyson Working Group
author_email = [email protected]
license = MIT License
Expand Down

0 comments on commit 14ea4f9

Please sign in to comment.