1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
"""Unit test for DateTime object."""
import datetime as dt
from unittest.mock import AsyncMock, patch
from zoneinfo import ZoneInfo
from freezegun import freeze_time
import pytest
from xknx import XKNX
from xknx.devices.datetime import (
BROADCAST_MINUTES,
DateDevice,
DateTimeDevice,
TimeDevice,
)
from xknx.dpt import DPTArray
from xknx.telegram import GroupAddress, Telegram
from xknx.telegram.apci import GroupValueRead, GroupValueResponse, GroupValueWrite
from ..conftest import EventLoopClockAdvancer
class TestDateTime:
"""Test class for Time object."""
@pytest.mark.parametrize(
("test_cls", "dt_value", "raw"),
[
(TimeDevice, dt.time(9, 13, 14), (0x9, 0xD, 0xE)),
(DateDevice, dt.date(2017, 1, 7), (0x07, 0x01, 0x11)),
(
DateTimeDevice,
dt.datetime(2017, 1, 7, 9, 13, 14),
(0x75, 0x01, 0x07, 0x09, 0x0D, 0x0E, 0x24, 0x00),
),
],
)
async def test_process_set_custom_time(
self,
test_cls: type[DateDevice | DateTimeDevice | TimeDevice],
dt_value: dt.time | dt.date | dt.datetime,
raw: tuple[int],
) -> None:
"""Test setting a new time."""
xknx = XKNX()
test_device = test_cls(
xknx,
"Test",
group_address="1/2/3",
localtime=False,
)
assert test_device.value is None
await test_device.set(dt_value)
telegram = xknx.telegrams.get_nowait()
assert telegram == Telegram(
destination_address=GroupAddress("1/2/3"),
payload=GroupValueWrite(DPTArray(raw)),
)
test_device.process(telegram)
assert test_device.value == dt_value
@pytest.mark.parametrize(
("cls", "raw_length", "raw"),
[
(TimeDevice, 3, (0xC9, 0xD, 0xE)),
(DateDevice, 3, (0x07, 0x01, 0x11)),
(DateTimeDevice, 8, (0x75, 0x01, 0x07, 0xC9, 0x0D, 0x0E, 0x20, 0xC0)),
],
)
async def test_sync_localtime(
self,
cls: type[DateDevice | DateTimeDevice | TimeDevice],
raw_length: int,
raw: tuple[int],
) -> None:
"""Test sync function / sending group reads to KNX bus."""
xknx = XKNX()
test_device = cls(xknx, "Test", group_address="1/2/3")
with freeze_time("2017-01-07 09:13:14"):
await test_device.sync()
assert xknx.telegrams.qsize() == 1
telegram = xknx.telegrams.get_nowait()
assert telegram.destination_address == GroupAddress("1/2/3")
assert len(telegram.payload.value.value) == raw_length
assert telegram.payload.value.value == raw
async def test_sync_time_custom(self) -> None:
"""Test sync function / sending group reads to KNX bus."""
xknx = XKNX()
test_device = TimeDevice(
xknx,
"TestDateTime",
group_address="1/2/3",
group_address_state="1/2/4",
localtime=False,
)
assert test_device.has_group_address(GroupAddress("1/2/4"))
await test_device.sync()
telegram = xknx.telegrams.get_nowait()
assert telegram.destination_address == GroupAddress("1/2/4")
assert isinstance(telegram.payload, GroupValueRead)
@pytest.mark.parametrize(
("expected", "localtime"),
[
((0xC9, 0xD, 0xE), True),
((0xCA, 0xD, 0xE), ZoneInfo("Europe/Vienna")),
],
)
async def test_process_read_localtime_time(
self, expected: tuple[int, int, int], localtime: bool | dt.tzinfo
) -> None:
"""Test test process a read telegram from KNX bus."""
xknx = XKNX()
test_device = TimeDevice(
xknx, "TestTime", group_address="1/2/3", localtime=localtime
)
telegram_read = Telegram(
destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()
)
with freeze_time("2017-01-07 09:13:14"):
test_device.process(telegram_read)
telegram = xknx.telegrams.get_nowait()
assert telegram == Telegram(
destination_address=GroupAddress("1/2/3"),
payload=GroupValueResponse(DPTArray(expected)),
)
@pytest.mark.parametrize(
("expected", "localtime"),
[
(
(0x75, 0x07, 0x0B, 0x49, 0x0D, 0x0E, 0x20, 0xC0),
True,
),
(
(0x75, 0x07, 0x0B, 0x4B, 0x0D, 0x0E, 0x21, 0xC0),
ZoneInfo("Europe/Vienna"),
),
],
)
async def test_process_read_localtime_datetime(
self, expected: tuple[int, int, int], localtime: bool | dt.tzinfo
) -> None:
"""Test test process a read telegram from KNX bus."""
xknx = XKNX()
test_device = DateTimeDevice(
xknx, "TestDateTime", group_address="1/2/3", localtime=localtime
)
telegram_read = Telegram(
destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()
)
with freeze_time("2017-07-11 09:13:14"): # summer time
test_device.process(telegram_read)
telegram = xknx.telegrams.get_nowait()
assert telegram == Telegram(
destination_address=GroupAddress("1/2/3"),
payload=GroupValueResponse(DPTArray(expected)),
)
async def test_process_read_custom_time(self) -> None:
"""Test test process a read telegram from KNX bus."""
xknx = XKNX()
test_device = TimeDevice(
xknx,
"TestDateTime",
group_address="1/2/3",
localtime=False,
respond_to_read=True,
)
await test_device.set(dt.time(9, 13, 14))
telegram_set = xknx.telegrams.get_nowait()
assert telegram_set == Telegram(
destination_address=GroupAddress("1/2/3"),
payload=GroupValueWrite(DPTArray((0x9, 0xD, 0xE))),
)
test_device.process(telegram_set)
telegram_read = Telegram(
destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()
)
test_device.process(telegram_read)
telegram = xknx.telegrams.get_nowait()
assert telegram == Telegram(
destination_address=GroupAddress("1/2/3"),
payload=GroupValueResponse(DPTArray((0x9, 0xD, 0xE))),
)
#
# TEST HAS GROUP ADDRESS
#
async def test_has_group_address_localtime(self) -> None:
"""Test if has_group_address function works."""
xknx = XKNX()
test_device = DateDevice(
xknx,
"TestDateTime",
group_address="1/2/3",
group_address_state="1/2/4",
localtime=True,
)
assert test_device.has_group_address(GroupAddress("1/2/3"))
# group_address_state ignored when using localtime
assert not test_device.has_group_address(GroupAddress("1/2/4"))
async def test_has_group_address_custom_time(self) -> None:
"""Test if has_group_address function works."""
xknx = XKNX()
test_device = DateDevice(
xknx,
"TestDateTime",
group_address="1/2/3",
group_address_state="1/2/4",
localtime=False,
)
assert test_device.has_group_address(GroupAddress("1/2/3"))
assert test_device.has_group_address(GroupAddress("1/2/4"))
#
# TEST BACKGROUND TASK
#
@patch("xknx.core.TelegramQueue.process_telegram_outgoing", new_callable=AsyncMock)
async def test_background_task(
self,
process_telegram_outgoing_mock: AsyncMock,
time_travel: EventLoopClockAdvancer,
xknx_no_interface: XKNX,
) -> None:
"""Test if background task works."""
xknx = xknx_no_interface
test_device = TimeDevice(xknx, "TestDateTime", group_address="1/2/3")
xknx.devices.async_add(test_device)
async with xknx:
# initial time telegram
await time_travel(0)
process_telegram_outgoing_mock.assert_called_once()
process_telegram_outgoing_mock.reset_mock()
# repeated time telegram
await time_travel(BROADCAST_MINUTES * 60)
process_telegram_outgoing_mock.assert_called_once()
process_telegram_outgoing_mock.reset_mock()
# remove device - no more telegrams
xknx.devices.async_remove(test_device)
await time_travel(BROADCAST_MINUTES * 60)
process_telegram_outgoing_mock.assert_not_called()
@patch("xknx.core.TelegramQueue.process_telegram_outgoing", new_callable=AsyncMock)
async def test_no_background_task(
self,
process_telegram_outgoing_mock: AsyncMock,
time_travel: EventLoopClockAdvancer,
xknx_no_interface: XKNX,
) -> None:
"""Test if background task is not started when not using `localtime`."""
xknx = xknx_no_interface
test_device = TimeDevice(
xknx,
"TestDateTime",
group_address="1/2/3",
localtime=False,
)
xknx.devices.async_add(test_device)
async with xknx:
assert test_device._broadcast_task is None
# no initial time telegram
await time_travel(0)
process_telegram_outgoing_mock.assert_not_called()
# no repeated time telegram
await time_travel(BROADCAST_MINUTES * 60)
process_telegram_outgoing_mock.assert_not_called()
|