Skip to content

Commit

Permalink
Missing Update on occasions
Browse files Browse the repository at this point in the history
Fixes #96
  • Loading branch information
sHedC committed Mar 23, 2023
1 parent 543c83e commit cef382f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
25 changes: 17 additions & 8 deletions masterthermconnect/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(
# "module_id_unit_id": {
# "last_data_update": <datetime>,
# "last_info_update": <datetime>,
# "last_full_load": <datetime>,
# "last_update_time": "1192282722"
# "info": { Various Information },
# "data": { Normalized Data Information },
Expand Down Expand Up @@ -210,23 +211,28 @@ async def __get_hp_updates(self, full_load: bool = False) -> None:
# to try and keep frequency of requests down.all(iterable)
last_data_update = None
last_update_time = 0

# Check to see if full data load is required.
if "last_full_load" in device:
last_full_load = device["last_full_load"]

if datetime.now() >= last_full_load + timedelta(
minutes=self.__full_refresh_min
):
full_load = True
else:
full_load = True

if "last_data_update" in device and not full_load:
last_data_update = device["last_data_update"]
last_update_time = device["last_update_time"]
last_update_time = last_update_time - self.__data_offset_sec

if (
last_data_update is None
or datetime.now()
>= last_data_update + timedelta(seconds=self.__data_update_sec)
):
# Check to see if full data load is required.
if (
last_data_update is None
or datetime.now()
>= last_data_update + timedelta(minutes=self.__full_refresh_min)
):
last_update_time = None

try:
# Refresh Device Data.
device_data = await self.__api.get_device_data(
Expand All @@ -238,6 +244,9 @@ async def __get_hp_updates(self, full_load: bool = False) -> None:
# Check that we have data, sometimes nothing is returned.
if device_data["data"]:
device["last_update_time"] = device_data["timestamp"]
if full_load:
device["last_full_load"] = datetime.now()

device["api_update_data"] = device_data["data"]["varData"][
str(unit_id).zfill(3)
].copy()
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_device_data(self, module_id, unit_id, last_update_time=None):
last_update_time = "0"

device_id = module_id + "_" + unit_id
if self.__devices[device_id]["data"] is None or last_update_time != "0":
if self.__devices[device_id]["data"] is None or last_update_time != 0:
data = json.loads(
load_fixture(
f"{self.__subfolder}pumpdata_{module_id}_{unit_id}_{last_update_time}.json"
Expand Down
32 changes: 32 additions & 0 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,38 @@ async def test_getdata_update():
assert data["actual_temp"] == 40.7


async def test_periodic_full_load():
"""Test getting full data on refresh time."""
controller = MasterthermController(
VALID_LOGIN["uname"], VALID_LOGIN["upwd"], ClientSession()
)
mockconnect = ConnectionMock()

with patch(
"masterthermconnect.api.MasterthermAPI.connect",
return_value=mockconnect.connect(),
) as mock_apiconnect, patch(
"masterthermconnect.api.MasterthermAPI.get_device_info",
side_effect=mockconnect.get_device_info,
) as mock_get_device_info, patch(
"masterthermconnect.api.MasterthermAPI.get_device_data",
side_effect=mockconnect.get_device_data,
) as mock_get_device_data:
assert await controller.connect() is True
controller.set_refresh_rate(
data_refresh_seconds=0, data_offset_seconds=0, full_refresh_minutes=0
)
assert await controller.refresh() is True

data = controller.get_device_data("1234", "1")
assert data["actual_temp"] == 32.1
assert await controller.refresh() is True

assert len(mock_apiconnect.mock_calls) == 1
assert len(mock_get_device_info.mock_calls) == 1
assert len(mock_get_device_data.mock_calls) == 2


async def test_season_winter():
"""Test the Controller Season."""
controller = MasterthermController(
Expand Down

0 comments on commit cef382f

Please sign in to comment.