Skip to content

Commit 27dd4e6

Browse files
authored
Merge pull request #11 from simonengelhardt/fix-setting-preset-mode
Fix setting preset mode
2 parents 05c6d1b + f0b23c7 commit 27dd4e6

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

pydanfossally/danfossallyapi.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
_LOGGER = logging.getLogger(__name__)
2121

22+
_MODE_TO_LAST_CLICK_TIME_FORMAT_MAP = {
23+
# The format was reverse engineered from experimentation with the API and the Danfoss Ally app
24+
"at_home": "010000",
25+
"leaving_home": "000101",
26+
}
27+
"""Map mode to obscure format required by `last_click_time`"""
28+
2229

2330
class DanfossAllyAPI:
2431
def __init__(self) -> None:
@@ -175,12 +182,27 @@ def set_temperature(
175182

176183
def set_mode(self, device_id: str, mode: str) -> bool:
177184
"""Set device operating mode."""
178-
request_body = {"commands": [{"code": "mode", "value": mode}]}
185+
commands = [{"code": "mode", "value": mode}]
186+
187+
# Strangely, some modes require `last_click_time` to also be set to "YYYYmmddHHMM{{mode_in_last_click_time_format}}"
188+
if mode in _MODE_TO_LAST_CLICK_TIME_FORMAT_MAP:
189+
commands.append(
190+
{
191+
"code": "last_click_time",
192+
"value": f"{datetime.datetime.now():%Y%m%d%H%M}{_MODE_TO_LAST_CLICK_TIME_FORMAT_MAP[mode]}",
193+
}
194+
)
195+
196+
request_body = {"commands": commands}
179197

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

202+
_LOGGER.debug(
203+
"Set mode for device %s: %s", device_id, json.dumps(request_body)
204+
)
205+
184206
return callData["result"]
185207

186208
def send_command(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
"Bug Tracker": "https://github.com/mtrab/pydanfossally/issues",
2525
},
2626
install_requires=["requests>=2.28.0"],
27-
version="0.0.29",
27+
version="0.0.30",
2828
)

0 commit comments

Comments
 (0)