Skip to content

Commit fc30b94

Browse files
committed
Add datetime provider to cube constructor
Allow to pass a datetime provider to the constructor of MaxCube to support HASS, where operating system timezone is always UTC and the correct timezone is configured in HA core.
1 parent 2ef7ef6 commit fc30b94

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

maxcube/cube.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import base64
2+
from datetime import datetime
23
import json
34
import logging
45
import struct
6+
from typing import Callable
57

68
from maxcube.device import (
79
MAX_CUBE,
@@ -42,14 +44,20 @@
4244

4345

4446
class MaxCube(MaxDevice):
45-
def __init__(self, host: str, port: int = DEFAULT_PORT):
47+
def __init__(
48+
self,
49+
host: str,
50+
port: int = DEFAULT_PORT,
51+
now: Callable[[], datetime] = datetime.now,
52+
):
4653
super(MaxCube, self).__init__()
4754
self.__commander = Commander(host, port)
4855
self.name = "Cube"
4956
self.type = MAX_CUBE
5057
self.firmware_version = None
5158
self.devices = []
5259
self.rooms = []
60+
self._now: Callable[[], datetime] = now
5361
self.update()
5462
self.log()
5563

@@ -301,8 +309,8 @@ def set_temperature_mode(self, thermostat, temperature, mode):
301309
if temperature > 0:
302310
thermostat.target_temperature = int(temperature * 2) / 2.0
303311
elif mode == MAX_DEVICE_MODE_AUTOMATIC:
304-
thermostat.target_temperature = (
305-
thermostat.get_current_temp_in_auto_mode()
312+
thermostat.target_temperature = thermostat.get_programmed_temp_at(
313+
self._now()
306314
)
307315
return True
308316
return False

maxcube/thermostat.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from time import localtime
1+
from datetime import datetime
22
from typing import Dict, List
33

44
from maxcube.device import MODE_NAMES, MaxDevice
@@ -39,11 +39,15 @@ def __str__(self):
3939
f"valve={self.valve_position}",
4040
)
4141

42-
def get_current_temp_in_auto_mode(self):
43-
t = localtime()
44-
weekday = PROG_DAYS[t.tm_wday]
45-
time = f"{t.tm_hour:02}:{t.tm_min:02}"
42+
def get_programmed_temp_at(self, dt: datetime):
43+
"""Retrieve the programmed temperature at the given instant."""
44+
weekday = PROG_DAYS[dt.weekday()]
45+
time = f"{dt.hour:02}:{dt.minute:02}"
4646
for point in self.programme.get(weekday, []):
4747
if time < point["until"]:
4848
return point["temp"]
4949
return None
50+
51+
def get_current_temp_in_auto_mode(self):
52+
"""DEPRECATED: use get_programmed_temp_at instead."""
53+
return self.get_programmed_temp_at(datetime.now())

tests/test_cube.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from time import strptime
1+
from datetime import datetime
22
from typing import List
33
from unittest import TestCase
44
from unittest.mock import patch
@@ -104,7 +104,7 @@ def init(self, ClassMock, responses):
104104
self.commander = ClassMock.return_value
105105
self.commander.update.return_value = responses
106106

107-
self.cube = MaxCube("host", 1234)
107+
self.cube = MaxCube("host", 1234, now=lambda: datetime(2012, 10, 22, 5, 30))
108108

109109
self.commander.update.assert_called_once()
110110
self.commander.update.reset_mock()
@@ -424,10 +424,7 @@ def test_get_device_as_dict(self, ClassMock):
424424
],
425425
)
426426

427-
@patch("maxcube.thermostat.localtime")
428-
def test_set_auto_mode_read_temp_from_program(self, localtime_mock, ClassMock):
429-
localtime_mock.return_value = strptime("2012-10-22T05:30", "%Y-%m-%dT%H:%M")
430-
print(localtime_mock.return_value)
427+
def test_set_auto_mode_read_temp_from_program(self, ClassMock):
431428
self.init(ClassMock, INIT_RESPONSE_2)
432429
device = self.cube.devices[0]
433430
self.assertEqual(8.0, device.target_temperature)

0 commit comments

Comments
 (0)