Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Remove API calls from objects
Browse files Browse the repository at this point in the history
  • Loading branch information
peternijssen committed Oct 30, 2021
1 parent 4c87bbb commit 13ef7dd
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 160 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
@@ -1,4 +1,3 @@
exclude: 'test_spiderapi.py'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
Expand Down
12 changes: 1 addition & 11 deletions .pylintrc
Expand Up @@ -10,16 +10,6 @@ disable=
missing-module-docstring,
missing-class-docstring,
missing-function-docstring,
no-self-use,
too-few-public-methods,
too-many-public-methods,
too-many-arguments,
bad-continuation,
import-error,
unsubscriptable-object,
line-too-long,
too-many-instance-attribute,
too-many-instance-attributes,
fixme,
pointless-string-statement,
redefined-builtin
too-many-locals,
3 changes: 1 addition & 2 deletions spiderpy/devices/base.py
Expand Up @@ -2,8 +2,7 @@


class SpiderDevice:
def __init__(self, data: Dict[Any, Any], api: Any) -> None:
self.api = api
def __init__(self, data: Dict[Any, Any]) -> None:
self.data = data

@property
Expand Down
14 changes: 8 additions & 6 deletions spiderpy/devices/powerplug.py
Expand Up @@ -26,15 +26,17 @@ def today_energy_consumption(self) -> float:

return float(today_usage)

def turn_on(self) -> None:
if self.is_online is True:
def turn_on(self) -> bool:
if self.is_online:
self.data["isSwitchedOn"] = True
self.api.turn_power_plug_on(self.id)
return True
return False

def turn_off(self) -> None:
if self.is_online is True:
def turn_off(self) -> bool:
if self.is_online:
self.data["isSwitchedOn"] = False
self.api.turn_power_plug_off(self.id)
return True
return False

def __str__(self) -> str:
return f"{self.id} {self.name} {self.model} {self.manufacturer} {self.type} {self.is_online} {self.is_on} {self.is_available} {self.current_energy_consumption} {self.today_energy_consumption}"
120 changes: 78 additions & 42 deletions spiderpy/devices/thermostat.py
@@ -1,63 +1,66 @@
from datetime import datetime
from typing import Any, Dict, List

from spiderpy.devices.base import SpiderDevice


class SpiderThermostat(SpiderDevice):
def __init__(self, data: Dict[Any, Any], api: Any) -> None:
super().__init__(data, api)
self.properties: List[Any] = []

@property
def properties(self) -> List[Any]:
if self.data.get("properties") is not None:
self.properties = self.data["properties"]
return self.data["properties"]

@staticmethod
def get_values(prop: Dict[Any, Any]) -> List[str]:
values = []
for choice in prop["scheduleChoices"]:
if not choice["disabled"]:
values.append(choice["value"])
return values
return []

@property
def operation_mode(self) -> str:
def current_operation_mode(self) -> str:
for prop in self.properties:
if prop["id"] == "OperationMode":
return str(prop["status"])

return "Idle"

@property
def operation_values(self) -> List[str]:
values = []
def has_operation_mode(self) -> bool:
for prop in self.properties:
if prop["id"] == "OperationMode":
values = self.get_values(prop)
return values
return True

return False

@property
def has_operation_mode(self) -> bool:
def available_operation_modes(self) -> List[str]:
operation_modes = []
for prop in self.properties:
if prop["id"] == "OperationMode":
return True
operation_modes = self.get_values(prop)

return False
return operation_modes

@property
def has_fan_mode(self) -> bool:
def current_fan_speed_mode(self) -> str:
for prop in self.properties:
if prop["id"] == "FanSpeed":
return str(prop["status"])

return "Idle"

@property
def has_fan_speed_mode(self) -> bool:
for prop in self.properties:
if prop["id"] == "FanSpeed":
return True

return False

@property
def fan_speed_values(self) -> List[str]:
values = []
def available_fan_speed_modes(self) -> List[str]:
fan_speeds = []
for prop in self.properties:
if prop["id"] == "FanSpeed":
values = self.get_values(prop)
return values
fan_speeds = self.get_values(prop)

return fan_speeds

@property
def current_temperature(self) -> float:
Expand Down Expand Up @@ -99,26 +102,59 @@ def temperature_steps(self) -> float:

return 0.0

@property
def current_fan_speed(self) -> str:
for prop in self.properties:
if prop["id"] == "FanSpeed":
return str(prop["status"])

return "Idle"
def set_operation_mode(self, operation_mode: str) -> bool:
""" Set the operation mode. Either 'Heat' or 'Cool'"""
if self.is_online:
self.reset_last_modified()

def set_temperature(self, temperature: str) -> None:
if self.is_online is True:
self.api.set_temperature(self.data, temperature)
for prop in self.properties:
if prop["id"] == "OperationMode":
prop["status"] = operation_mode[0].upper() + operation_mode[1:]
prop["statusModified"] = True
prop["statusLastUpdated"] = str(datetime.now())
return True

def set_operation_mode(self, operation: str) -> None:
""" Set the operation mode. Either 'Heat' or 'Cool'"""
if self.is_online is True:
self.api.set_operation_mode(self.data, operation)
return False

def set_fan_speed(self, fan_speed: str) -> bool:
""" Set the fanspeed. Either 'Auto', 'Low', 'Medium', 'High', 'Boost 10', 'Boost 20', 'Boost 30'"""
return self.is_online & self.api.set_fan_speed(self.data, fan_speed)
""" Set the fan speed. Either 'Auto', 'Low', 'Medium', 'High', 'Boost 10', 'Boost 20', 'Boost 30'"""
if self.is_online:
self.reset_last_modified()

for prop in self.properties:
if prop["id"] == "FanSpeed":
prop["status"] = fan_speed[0].upper() + fan_speed[1:]
prop["statusModified"] = True
prop["statusLastUpdated"] = str(datetime.now())
return True

return False

def set_temperature(self, temperature: float) -> bool:
if self.is_online:
self.reset_last_modified()

for prop in self.properties:
if prop["id"] == "SetpointTemperature":
prop["status"] = str(temperature)
prop["statusModified"] = True
prop["statusLastUpdated"] = str(datetime.now())
return True

return False

@staticmethod
def get_values(prop: Dict[Any, Any]) -> List[str]:
values = []
for choice in prop["scheduleChoices"]:
if not choice["disabled"]:
values.append(choice["value"])
return values

def reset_last_modified(self) -> None:
for prop in self.properties:
if prop.get("statusModified", False):
prop["statusModified"] = False

def __str__(self) -> str:
return f"{self.id} {self.name} {self.model} {self.manufacturer} {self.type} {self.is_online} {self.operation_mode} {self.operation_values} {self.has_operation_mode} {self.has_fan_mode} {self.fan_speed_values} {self.current_temperature} {self.target_temperature} {self.minimum_temperature} {self.maximum_temperature} {self.temperature_steps} {self.current_fan_speed}"
return f"{self.id} {self.name} {self.model} {self.manufacturer} {self.type} {self.is_online} {self.current_operation_mode} {self.has_operation_mode} {self.available_operation_modes} {self.current_fan_speed_mode} {self.has_fan_speed_mode} {self.available_fan_speed_modes} {self.current_temperature} {self.target_temperature} {self.minimum_temperature} {self.maximum_temperature} {self.temperature_steps}"

0 comments on commit 13ef7dd

Please sign in to comment.