Skip to content

Commit

Permalink
Fix local time on PVVX and Qingping devices (#13)
Browse files Browse the repository at this point in the history
* Fix local time on PVVX and Qingping devices

* Skip tests that need timezone data on Windows
  • Loading branch information
koenvervloesem authored Feb 1, 2023
1 parent 52d3235 commit a60a004
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ testing =
pytest
pytest-cov
time-machine
backports-zoneinfo; python_version<"3.9"
tzdata

[options.entry_points]
console_scripts =
Expand Down
3 changes: 2 additions & 1 deletion src/bluetooth_clocks/devices/pvvx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import struct
from time import localtime
from uuid import UUID

from bleak.backends.device import BLEDevice
Expand Down Expand Up @@ -65,5 +66,5 @@ def get_bytes_from_time(self, timestamp: float, ampm: bool = False) -> bytes:
return struct.pack(
self.TIME_SET_FORMAT,
0x23,
int(timestamp),
int(timestamp + localtime(timestamp).tm_gmtoff),
)
8 changes: 7 additions & 1 deletion src/bluetooth_clocks/devices/qingping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from struct import pack
from time import localtime
from uuid import UUID

from bluetooth_clocks import BluetoothClock
Expand Down Expand Up @@ -51,4 +52,9 @@ def get_bytes_from_time(self, timestamp: float, ampm: bool = False) -> bytes:
Returns:
bytes: The bytes needed to set the time of the device to `timestamp`.
"""
return pack(self.TIME_SET_FORMAT, 0x05, 0x09, int(timestamp))
return pack(
self.TIME_SET_FORMAT,
0x05,
0x09,
int(timestamp + localtime(timestamp).tm_gmtoff),
)
14 changes: 12 additions & 2 deletions tests/test_pvvx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test PVVX devices."""
import sys
from datetime import datetime
from time import time

import pytest
Expand All @@ -10,10 +12,17 @@
from bluetooth_clocks.devices.pvvx import PVVX
from bluetooth_clocks.exceptions import TimeNotReadableError

if sys.version_info >= (3, 9):
from zoneinfo import ZoneInfo
else:
from backports.zoneinfo import ZoneInfo

__author__ = "Koen Vervloesem"
__copyright__ = "Koen Vervloesem"
__license__ = "MIT"

CET_TZ = ZoneInfo("Europe/Brussels")


def test_in_supported_devices() -> None:
"""Test whether PVVX is in the list of supported devices."""
Expand Down Expand Up @@ -69,9 +78,10 @@ def test_get_time_from_bytes() -> None:
)


@time_machine.travel("2023-01-07 18:41:33 +0000")
@pytest.mark.skipif(sys.platform.startswith("win"), reason="timezone problem")
@time_machine.travel(datetime(2023, 1, 7, 18, 41, tzinfo=CET_TZ), tick=False)
def test_get_bytes_from_time() -> None:
"""Test the command to set the time."""
assert PVVX(BLEDevice("A4:C1:38:D9:01:10")).get_bytes_from_time(time()) == bytes(
[0x23, 0xDD, 0xBC, 0xB9, 0x63]
[0x23, 0xBC, 0xBC, 0xB9, 0x63]
)
14 changes: 12 additions & 2 deletions tests/test_qingping.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test Qingping devices."""
import sys
from datetime import datetime
from time import time

import pytest
Expand All @@ -10,10 +12,17 @@
from bluetooth_clocks.devices.qingping import CGC1
from bluetooth_clocks.exceptions import TimeNotReadableError

if sys.version_info >= (3, 9):
from zoneinfo import ZoneInfo
else:
from backports.zoneinfo import ZoneInfo

__author__ = "Koen Vervloesem"
__copyright__ = "Koen Vervloesem"
__license__ = "MIT"

CET_TZ = ZoneInfo("Europe/Brussels")


def test_in_supported_devices() -> None:
"""Test whether the Qingping BT Clock Lite is in the list of supported devices."""
Expand Down Expand Up @@ -71,9 +80,10 @@ def test_get_time_from_bytes() -> None:
)


@time_machine.travel("2022-12-30 15:30:24 +0000")
@pytest.mark.skipif(sys.platform.startswith("win"), reason="timezone problem")
@time_machine.travel(datetime(2022, 12, 30, 16, 30, tzinfo=CET_TZ), tick=False)
def test_get_bytes_from_time() -> None:
"""Test the command to set the time."""
assert CGC1(BLEDevice("58:2D:34:54:2D:2C")).get_bytes_from_time(time()) == bytes(
[0x05, 0x09, 0x10, 0x04, 0xAF, 0x63]
[0x05, 0x09, 0x08, 0x12, 0xAF, 0x63]
)
20 changes: 15 additions & 5 deletions tests/test_xiaomi.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
"""Test Xiaomi devices."""
import sys
from datetime import datetime
from time import time

import pytest
import time_machine
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData

from bluetooth_clocks import supported_devices
from bluetooth_clocks.devices.xiaomi import LYWSD02
from bluetooth_clocks.exceptions import InvalidTimeBytesError

if sys.version_info >= (3, 9):
from zoneinfo import ZoneInfo
else:
from backports.zoneinfo import ZoneInfo

__author__ = "Koen Vervloesem"
__copyright__ = "Koen Vervloesem"
__license__ = "MIT"

CET_TZ = ZoneInfo("Europe/Brussels")


def test_in_supported_devices() -> None:
"""Test whether the Xiaomi LYWSD02 is in the list of supported devices."""
Expand Down Expand Up @@ -82,10 +92,10 @@ def test_get_time_from_bytes_invalid() -> None:
LYWSD02(BLEDevice("E7:2E:00:B1:38:96")).get_time_from_bytes(bytes([0x2A]))


@pytest.mark.skipif(sys.platform.startswith("win"), reason="timezone problem")
@time_machine.travel(datetime(2023, 1, 7, 18, 41, tzinfo=CET_TZ), tick=False)
def test_get_bytes_from_time() -> None:
"""Test the command to set the time."""
timestamp = datetime.fromisoformat("2023-01-07 18:41:33+00:00").timestamp()
# Ignore the timezone offset
assert LYWSD02(BLEDevice("E7:2E:00:B1:38:96")).get_bytes_from_time(timestamp)[
:-1
] == bytes([0xDD, 0xBC, 0xB9, 0x63])
assert LYWSD02(BLEDevice("E7:2E:00:B1:38:96")).get_bytes_from_time(time()) == bytes(
[0xAC, 0xAE, 0xB9, 0x63, 0x01]
)

0 comments on commit a60a004

Please sign in to comment.