Skip to content

Commit

Permalink
Add set_datetime method
Browse files Browse the repository at this point in the history
  • Loading branch information
j-a-n committed Sep 17, 2022
1 parent 1dc8dc3 commit b36ca42
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ from moehlenhoff_alpha2 import Alpha2Base
async def main():
base = Alpha2Base("192.168.1.1")
await base.update_data()
# Set current date and time in base
await base.set_datetime()
# Increase the temperature of heatarea by 0.2 degrees
heat_area = list(base.heat_areas)[0]
t_target = heat_area["T_TARGET"] + 0.2
await base.update_heat_area(heat_area["ID"], {"T_TARGET": t_target})
Expand Down
10 changes: 9 additions & 1 deletion moehlenhoff_alpha2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import asyncio
import aiohttp
import xmltodict
from datetime import datetime


logger = logging.getLogger(__name__)

__version__ = "1.1.2"
__version__ = "1.3.0"


class Alpha2Base:
Expand Down Expand Up @@ -285,3 +286,10 @@ async def update_heat_area(self, heat_area_id: Union[str, int], attributes: dict
command += "</HEATAREA>"
async with self._update_lock:
await self._send_command(device_id, command)

async def set_datetime(self, value: datetime = None) -> None:
"""Set base date and time"""
value = value or datetime.now()
command = f"<DATETIME>{value.strftime('%Y-%m-%dT%H:%M:%S')}</DATETIME>"
async with self._update_lock:
await self._send_command(self.id, command)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "moehlenhoff-alpha2"
version = "1.2.1"
version = "1.3.0"
description = "Python client for the Moehlenhoff Alpha2 underfloor heating system"
authors = ["Jan Schneider <[email protected]>"]
license = "GPL"
Expand Down
22 changes: 22 additions & 0 deletions tests/test_moehlenhoff_alpha2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import asyncio
from unittest.mock import patch
from datetime import datetime

import pytest

Expand Down Expand Up @@ -113,6 +114,7 @@ async def _fetch_static_data(_self):


@pytest.mark.asyncio
@pytest.mark.skipif(not ALPHA2_BASE_ADDRESS, reason="ALPHA2_BASE_ADDRESS not set in environment")
async def test_ensure_static_data():
"""Test _ensure_static_data"""
base = Alpha2Base(ALPHA2_BASE_ADDRESS)
Expand Down Expand Up @@ -216,3 +218,23 @@ async def test_update_lock():
base.update_data()
]
await asyncio.gather(*coros)

@pytest.mark.asyncio
@pytest.mark.skipif(not ALPHA2_BASE_ADDRESS, reason="ALPHA2_BASE_ADDRESS not set in environment")
async def test_set_datetime():
"""Test set datetime"""
base = Alpha2Base(ALPHA2_BASE_ADDRESS)
await base.update_data()

value = datetime(2010, 1, 1, 0, 0, 0)
await base.set_datetime(value)
await asyncio.sleep(5)
await base.update_data()
base_dt = datetime.strptime(base.static_data["Devices"]["Device"]["DATETIME"], "%Y-%m-%dT%H:%M:%S")
assert abs((base_dt - value).total_seconds()) < 10

await base.set_datetime()
await asyncio.sleep(5)
await base.update_data()
base_dt = datetime.strptime(base.static_data["Devices"]["Device"]["DATETIME"], "%Y-%m-%dT%H:%M:%S")
assert abs((base_dt - datetime.now()).total_seconds()) < 10

0 comments on commit b36ca42

Please sign in to comment.