mirror of
https://github.com/micahqcade/custom_vesync.git
synced 2025-02-15 11:38:59 +01:00
Refactor code. (#10)
This commit is contained in:
parent
dde8644627
commit
fab77a025a
@ -123,12 +123,11 @@ class VeSyncAirBypass(VeSyncBaseDevice):
|
|||||||
logger.error(
|
logger.error(
|
||||||
"Please set modes for %s in the configuration", self.device_type
|
"Please set modes for %s in the configuration", self.device_type
|
||||||
)
|
)
|
||||||
raise Exception
|
raise RuntimeError(
|
||||||
|
"Please set modes for %s in the configuration", self.device_type
|
||||||
|
)
|
||||||
self.modes = self.config_dict["modes"]
|
self.modes = self.config_dict["modes"]
|
||||||
if "air_quality" in self.features:
|
self.air_quality_feature = "air_quality" in self.features
|
||||||
self.air_quality_feature = True
|
|
||||||
else:
|
|
||||||
self.air_quality_feature = False
|
|
||||||
self.details: Dict[str, Union[str, int, float, bool]] = {
|
self.details: Dict[str, Union[str, int, float, bool]] = {
|
||||||
"filter_life": 0,
|
"filter_life": 0,
|
||||||
"mode": "manual",
|
"mode": "manual",
|
||||||
@ -137,7 +136,7 @@ class VeSyncAirBypass(VeSyncBaseDevice):
|
|||||||
"child_lock": False,
|
"child_lock": False,
|
||||||
"night_light": "off",
|
"night_light": "off",
|
||||||
}
|
}
|
||||||
if self.air_quality_feature is True:
|
if self.air_quality_feature:
|
||||||
self.details["ait_quality"] = 0
|
self.details["ait_quality"] = 0
|
||||||
self.config: Dict[str, Union[str, int, float, bool]] = {
|
self.config: Dict[str, Union[str, int, float, bool]] = {
|
||||||
"display": False,
|
"display": False,
|
||||||
@ -175,10 +174,7 @@ class VeSyncAirBypass(VeSyncBaseDevice):
|
|||||||
def build_purifier_dict(self, dev_dict: dict) -> None:
|
def build_purifier_dict(self, dev_dict: dict) -> None:
|
||||||
"""Build Bypass purifier status dictionary."""
|
"""Build Bypass purifier status dictionary."""
|
||||||
self.enabled = dev_dict.get("enabled", False)
|
self.enabled = dev_dict.get("enabled", False)
|
||||||
if self.enabled:
|
self.device_status = "on" if self.enabled else "off"
|
||||||
self.device_status = "on"
|
|
||||||
else:
|
|
||||||
self.device_status = "off"
|
|
||||||
self.details["filter_life"] = dev_dict.get("filter_life", 0)
|
self.details["filter_life"] = dev_dict.get("filter_life", 0)
|
||||||
self.mode = dev_dict.get("mode", "manual")
|
self.mode = dev_dict.get("mode", "manual")
|
||||||
self.speed = dev_dict.get("level", 0)
|
self.speed = dev_dict.get("level", 0)
|
||||||
@ -216,10 +212,7 @@ class VeSyncAirBypass(VeSyncBaseDevice):
|
|||||||
logger.debug("Error in purifier response")
|
logger.debug("Error in purifier response")
|
||||||
return
|
return
|
||||||
outer_result = r.get("result", {})
|
outer_result = r.get("result", {})
|
||||||
inner_result = None
|
inner_result = r.get("result", {}).get("result") if outer_result else None
|
||||||
|
|
||||||
if outer_result:
|
|
||||||
inner_result = r.get("result", {}).get("result")
|
|
||||||
if inner_result is not None and Helpers.code_check(r):
|
if inner_result is not None and Helpers.code_check(r):
|
||||||
if outer_result.get("code") == 0:
|
if outer_result.get("code") == 0:
|
||||||
self.build_purifier_dict(inner_result)
|
self.build_purifier_dict(inner_result)
|
||||||
@ -250,8 +243,7 @@ class VeSyncAirBypass(VeSyncBaseDevice):
|
|||||||
new_speed = speed
|
new_speed = speed
|
||||||
if current_speed == new_speed:
|
if current_speed == new_speed:
|
||||||
return True
|
return True
|
||||||
else:
|
elif current_speed == speeds[-1]:
|
||||||
if current_speed == speeds[-1]:
|
|
||||||
new_speed = speeds[0]
|
new_speed = speeds[0]
|
||||||
else:
|
else:
|
||||||
current_index = speeds.index(current_speed)
|
current_index = speeds.index(current_speed)
|
||||||
@ -394,10 +386,7 @@ class VeSyncAirBypass(VeSyncBaseDevice):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if r is not None and Helpers.code_check(r):
|
if r is not None and Helpers.code_check(r):
|
||||||
if toggle:
|
self.device_status = "on" if toggle else "off"
|
||||||
self.device_status = "on"
|
|
||||||
else:
|
|
||||||
self.device_status = "off"
|
|
||||||
return True
|
return True
|
||||||
logger.debug("Error toggling purifier - %s", self.device_name)
|
logger.debug("Error toggling purifier - %s", self.device_name)
|
||||||
return False
|
return False
|
||||||
@ -631,7 +620,8 @@ class VeSyncAir131(VeSyncBaseDevice):
|
|||||||
|
|
||||||
def turn_on(self) -> bool:
|
def turn_on(self) -> bool:
|
||||||
"""Turn Air Purifier on."""
|
"""Turn Air Purifier on."""
|
||||||
if self.device_status != "on":
|
if self.device_status == "on":
|
||||||
|
return False
|
||||||
body = Helpers.req_body(self.manager, "devicestatus")
|
body = Helpers.req_body(self.manager, "devicestatus")
|
||||||
body["uuid"] = self.uuid
|
body["uuid"] = self.uuid
|
||||||
body["status"] = "on"
|
body["status"] = "on"
|
||||||
@ -646,11 +636,11 @@ class VeSyncAir131(VeSyncBaseDevice):
|
|||||||
return True
|
return True
|
||||||
logger.debug("Error turning %s on", self.device_name)
|
logger.debug("Error turning %s on", self.device_name)
|
||||||
return False
|
return False
|
||||||
return False
|
|
||||||
|
|
||||||
def turn_off(self) -> bool:
|
def turn_off(self) -> bool:
|
||||||
"""Turn Air Purifier Off."""
|
"""Turn Air Purifier Off."""
|
||||||
if self.device_status == "on":
|
if self.device_status != "on":
|
||||||
|
return True
|
||||||
body = Helpers.req_body(self.manager, "devicestatus")
|
body = Helpers.req_body(self.manager, "devicestatus")
|
||||||
body["uuid"] = self.uuid
|
body["uuid"] = self.uuid
|
||||||
body["status"] = "off"
|
body["status"] = "off"
|
||||||
@ -665,7 +655,6 @@ class VeSyncAir131(VeSyncBaseDevice):
|
|||||||
return True
|
return True
|
||||||
logger.debug("Error turning %s off", self.device_name)
|
logger.debug("Error turning %s off", self.device_name)
|
||||||
return False
|
return False
|
||||||
return True
|
|
||||||
|
|
||||||
def auto_mode(self) -> bool:
|
def auto_mode(self) -> bool:
|
||||||
"""Set mode to auto."""
|
"""Set mode to auto."""
|
||||||
@ -703,17 +692,13 @@ class VeSyncAir131(VeSyncBaseDevice):
|
|||||||
if speed is not None:
|
if speed is not None:
|
||||||
if speed == level:
|
if speed == level:
|
||||||
return True
|
return True
|
||||||
if speed in [1, 2, 3]:
|
if speed in {1, 2, 3}:
|
||||||
body["level"] = speed
|
body["level"] = speed
|
||||||
else:
|
else:
|
||||||
logger.debug("Invalid fan speed for %s", self.device_name)
|
logger.debug("Invalid fan speed for %s", self.device_name)
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
if (level + 1) > 3:
|
body["level"] = 1 if level > 2 else int(level + 1)
|
||||||
body["level"] = 1
|
|
||||||
else:
|
|
||||||
body["level"] = int(level + 1)
|
|
||||||
|
|
||||||
r, _ = Helpers.call_api(
|
r, _ = Helpers.call_api(
|
||||||
"/131airPurifier/v1/device/updateSpeed", "put", json=body, headers=head
|
"/131airPurifier/v1/device/updateSpeed", "put", json=body, headers=head
|
||||||
)
|
)
|
||||||
@ -729,7 +714,7 @@ class VeSyncAir131(VeSyncBaseDevice):
|
|||||||
head = Helpers.req_headers(self.manager)
|
head = Helpers.req_headers(self.manager)
|
||||||
body = Helpers.req_body(self.manager, "devicestatus")
|
body = Helpers.req_body(self.manager, "devicestatus")
|
||||||
body["uuid"] = self.uuid
|
body["uuid"] = self.uuid
|
||||||
if mode != self.mode and mode in ["sleep", "auto", "manual"]:
|
if mode != self.mode and mode in {"sleep", "auto", "manual"}:
|
||||||
body["mode"] = mode
|
body["mode"] = mode
|
||||||
if mode == "manual":
|
if mode == "manual":
|
||||||
body["level"] = 1
|
body["level"] = 1
|
||||||
@ -797,10 +782,7 @@ class VeSyncHumid200300S(VeSyncBaseDevice):
|
|||||||
else:
|
else:
|
||||||
self.warm_mist_feature = False
|
self.warm_mist_feature = False
|
||||||
self.warm_mist_levels = []
|
self.warm_mist_levels = []
|
||||||
if "nightlight" in self.config_dict.get("features", []):
|
self.night_light = "nightlight" in self.config_dict.get("features", [])
|
||||||
self.night_light = True
|
|
||||||
else:
|
|
||||||
self.night_light = False
|
|
||||||
self.details: Dict[str, Union[str, int, float]] = {
|
self.details: Dict[str, Union[str, int, float]] = {
|
||||||
"humidity": 0,
|
"humidity": 0,
|
||||||
"mist_virtual_level": 0,
|
"mist_virtual_level": 0,
|
||||||
@ -812,7 +794,7 @@ class VeSyncHumid200300S(VeSyncBaseDevice):
|
|||||||
"display": False,
|
"display": False,
|
||||||
"automatic_stop_reach_target": False,
|
"automatic_stop_reach_target": False,
|
||||||
}
|
}
|
||||||
if self.night_light is True:
|
if self.night_light:
|
||||||
self.details["night_light_brightness"] = 0
|
self.details["night_light_brightness"] = 0
|
||||||
self.config: Dict[str, Union[str, int, float]] = {
|
self.config: Dict[str, Union[str, int, float]] = {
|
||||||
"auto_target_humidity": 0,
|
"auto_target_humidity": 0,
|
||||||
@ -940,11 +922,7 @@ class VeSyncHumid200300S(VeSyncBaseDevice):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if r is not None and Helpers.code_check(r):
|
if r is not None and Helpers.code_check(r):
|
||||||
if toggle:
|
self.device_status = "on" if toggle else "off"
|
||||||
self.device_status = "on"
|
|
||||||
else:
|
|
||||||
self.device_status = "off"
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
logger.debug("Error toggling 300S humidifier - %s", self.device_name)
|
logger.debug("Error toggling 300S humidifier - %s", self.device_name)
|
||||||
return False
|
return False
|
||||||
@ -1154,8 +1132,7 @@ class VeSyncHumid200300S(VeSyncBaseDevice):
|
|||||||
self.device_type,
|
self.device_type,
|
||||||
)
|
)
|
||||||
call_str = "auto"
|
call_str = "auto"
|
||||||
set_auto = self.set_humidity_mode(call_str)
|
return self.set_humidity_mode(call_str)
|
||||||
return set_auto
|
|
||||||
|
|
||||||
def set_manual_mode(self):
|
def set_manual_mode(self):
|
||||||
"""Set humifier to manual mode with 1 mist level."""
|
"""Set humifier to manual mode with 1 mist level."""
|
||||||
@ -1212,16 +1189,12 @@ class VeSyncHumid200300S(VeSyncBaseDevice):
|
|||||||
@property
|
@property
|
||||||
def auto_enabled(self):
|
def auto_enabled(self):
|
||||||
"""Auto mode is enabled."""
|
"""Auto mode is enabled."""
|
||||||
if self.details.get("mode") == "auto" or self.details.get("mode") == "humidity":
|
return self.details.get("mode") in ["auto", "humidity"]
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def warm_mist_enabled(self):
|
def warm_mist_enabled(self):
|
||||||
"""Warm mist feature enabled."""
|
"""Warm mist feature enabled."""
|
||||||
if self.warm_mist_feature:
|
return self.details["warm_mist_enabled"] if self.warm_mist_feature else False
|
||||||
return self.details["warm_mist_enabled"]
|
|
||||||
return False
|
|
||||||
|
|
||||||
def display(self) -> None:
|
def display(self) -> None:
|
||||||
"""Return formatted device info to stdout."""
|
"""Return formatted device info to stdout."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user