Skip to content

Commit

Permalink
Merge pull request #9 from jnxxx/More-properties-exposed
Browse files Browse the repository at this point in the history
Expose more properties and generic send_command function
  • Loading branch information
MTrab authored Nov 29, 2022
2 parents 5ad0c64 + e409069 commit dcaa1d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
24 changes: 16 additions & 8 deletions pydanfossally/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def handleDeviceData(self, device: dict):
"leaving_home_setting",
"pause_setting",
"holiday_setting",
"temp_set"
]:
setpoint = float(status["value"])
setpoint = setpoint / 10
Expand All @@ -89,15 +90,16 @@ def handleDeviceData(self, device: dict):
elif status["code"] == "MeasuredValue" and bHasFloorSensor: # Floor sensor
temperature = float(status["value"])
temperature = temperature / 10
self.devices[device["id"]]["floor temperature"] = temperature
elif status["code"] == "upper_temp":
temperature = float(status["value"])
temperature = temperature / 10
self.devices[device["id"]]["upper_temp"] = temperature
elif status["code"] == "lower_temp":
self.devices[device["id"]]["floor_temperature"] = temperature
elif status["code"] in [
"upper_temp",
"lower_temp",
"floor_temp_min",
"floor_temp_max"
]:
temperature = float(status["value"])
temperature = temperature / 10
self.devices[device["id"]]["lower_temp"] = temperature
self.devices[device["id"]][status["code"]] = temperature
elif status["code"] == "va_temperature":
temperature = float(status["value"])
temperature = temperature / 10
Expand All @@ -116,7 +118,7 @@ def handleDeviceData(self, device: dict):
else:
self.devices[device["id"]]["window_open"] = False

if status["code"] in ["child_lock", "mode", "work_state", "banner_ctrl"]:
if status["code"] in ["child_lock", "mode", "work_state", "banner_ctrl", "window_toggle", "switch", "switch_state"]:
self.devices[device["id"]][status["code"]] = status["value"]

def getDevice(self, device_id: str) -> None:
Expand Down Expand Up @@ -152,3 +154,9 @@ def setMode(self, device_id: str, mode: str) -> bool:
result = self._api.set_mode(device_id, mode)

return result

def sendCommand(self, device_id: str, listofcommands: list[Tuple[str, str]]) -> bool:
"""Send list of commands for given device."""
result = self._api.send_command(device_id, listofcommands)

return result
27 changes: 24 additions & 3 deletions pydanfossally/danfossallyapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _call(

try:
if payload:
_LOGGER.debug("Send command: %s: %s", path, json.dumps(payload))
req = requests.post(
API_HOST + path, json=payload, headers=headers_data, timeout=10
)
Expand All @@ -48,6 +49,8 @@ def _call(
req.raise_for_status()
except requests.exceptions.HTTPError as err:
code = err.response.status_code
if payload:
_LOGGER.debug("Http status code: %s", code)
if code == 401:
raise UnauthorizedError
if code == 404:
Expand All @@ -60,9 +63,11 @@ def _call(
except:
raise UnexpectedError

json = req.json()
print("JSON: ", json)
return json
response = req.json()
if payload:
_LOGGER.debug("Command response: %s", response)
print("JSON: ", response)
return response

def _refresh_token(self) -> bool:
"""Refresh OAuth2 token if expired."""
Expand Down Expand Up @@ -176,6 +181,22 @@ def set_mode(self, device_id: str, mode: str) -> bool:

return callData["result"]


def send_command(self, device_id: str, listofcommands: list[Tuple[str, str]]) -> bool:
"""Send commands."""

commands = []
for code,value in listofcommands:
commands += [{"code": code, "value": value}]
request_body = {"commands": commands}

callData = self._call(
"/ally/devices/" + device_id + "/commands", payload=request_body
)

return callData["result"]


@property
def token(self) -> str:
"""Return token."""
Expand Down

0 comments on commit dcaa1d4

Please sign in to comment.