Skip to content

Commit

Permalink
Merge pull request #1 from koenvervloesem/fix_various_issues
Browse files Browse the repository at this point in the history
Fix various issues
  • Loading branch information
koenvervloesem authored Jan 20, 2023
2 parents 4a0227c + 5638413 commit fcf1cc4
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ repos:
additional_dependencies:
- bleak
- pytest
- time-machine

- repo: https://github.com/econchick/interrogate
rev: 1.5.0
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ testing =
setuptools
pytest
pytest-cov
time-machine

[options.entry_points]
console_scripts =
Expand Down
2 changes: 1 addition & 1 deletion src/bluetooth_clocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pathlib import Path
from pkgutil import iter_modules
from time import time
from typing import ClassVar
from typing import ClassVar, Type # noqa: F401
from uuid import UUID

from bleak import BleakClient
Expand Down
3 changes: 3 additions & 0 deletions tests/test_current_time_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Test Current Time Service devices."""
from __future__ import annotations

from datetime import datetime
from typing import Type # noqa: F401 pylint: disable=unused-import

import pytest
from bleak.backends.device import BLEDevice
Expand Down
11 changes: 6 additions & 5 deletions tests/test_pvvx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Test PVVX devices."""
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

Expand Down Expand Up @@ -64,13 +65,13 @@ def test_get_time_from_bytes() -> None:
"""Test the conversion from bytes to a timestamp."""
with pytest.raises(TimeNotReadableError):
PVVX(BLEDevice("A4:C1:38:D9:01:10")).get_time_from_bytes(
bytes([0x23, 0xCD, 0xAE, 0xB9, 0x63])
bytes([0x23, 0xDD, 0xBC, 0xB9, 0x63])
)


@time_machine.travel("2023-01-07 18:41:33 +0000")
def test_get_bytes_from_time() -> None:
"""Test the command to set the time."""
timestamp = datetime.fromisoformat("2023-01-07 18:41:33").timestamp()
assert PVVX(BLEDevice("A4:C1:38:D9:01:10")).get_bytes_from_time(timestamp) == bytes(
[0x23, 0xCD, 0xAE, 0xB9, 0x63]
assert PVVX(BLEDevice("A4:C1:38:D9:01:10")).get_bytes_from_time(time()) == bytes(
[0x23, 0xDD, 0xBC, 0xB9, 0x63]
)
9 changes: 5 additions & 4 deletions tests/test_qingping.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Test Qingping devices."""
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

Expand Down Expand Up @@ -70,9 +71,9 @@ def test_get_time_from_bytes() -> None:
)


@time_machine.travel("2022-12-30 15:30:24 +0000")
def test_get_bytes_from_time() -> None:
"""Test the command to set the time."""
timestamp = datetime.fromisoformat("2022-12-30 15:30:24").timestamp()
assert CGC1(BLEDevice("58:2D:34:54:2D:2C")).get_bytes_from_time(timestamp) == bytes(
[0x05, 0x09, 0x00, 0xF6, 0xAE, 0x63]
assert CGC1(BLEDevice("58:2D:34:54:2D:2C")).get_bytes_from_time(time()) == bytes(
[0x05, 0x09, 0x10, 0x04, 0xAF, 0x63]
)
1 change: 1 addition & 0 deletions tests/test_thermopro.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from datetime import datetime
from typing import Type # noqa: F401 pylint: disable=unused-import

import pytest
from bleak.backends.device import BLEDevice
Expand Down
17 changes: 8 additions & 9 deletions tests/test_xiaomi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Test Xiaomi devices."""
from datetime import datetime
from time import localtime

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

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

Expand Down Expand Up @@ -68,10 +67,10 @@ def test_readable() -> None:

def test_get_time_from_bytes() -> None:
"""Test the conversion from bytes to a timestamp."""
timestamp = datetime.fromisoformat("2023-01-07 18:41:33").timestamp()
timestamp = datetime.fromisoformat("2023-01-07 18:41:33+00:00").timestamp()
assert (
LYWSD02(BLEDevice("E7:2E:00:B1:38:96")).get_time_from_bytes(
bytes([0xCD, 0xAE, 0xB9, 0x63, 0x01])
bytes([0xDD, 0xBC, 0xB9, 0x63, 0x00])
)
== timestamp
)
Expand All @@ -85,8 +84,8 @@ def test_get_time_from_bytes_invalid() -> None:

def test_get_bytes_from_time() -> None:
"""Test the command to set the time."""
timestamp = datetime.fromisoformat("2023-01-07 18:41:33").timestamp()
timezone_offset = localtime().tm_gmtoff // SECONDS_IN_HOUR
assert LYWSD02(BLEDevice("E7:2E:00:B1:38:96")).get_bytes_from_time(
timestamp
) == bytes([0xCD, 0xAE, 0xB9, 0x63, timezone_offset])
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])

0 comments on commit fcf1cc4

Please sign in to comment.