Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pyhilo/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def update(self, **kwargs: Dict[str, Union[str, int, Dict]]) -> None:
elif att == "model_number":
att = "model"
new_val = val # type: ignore
if att == "identifier" and new_val is None:
# The cloud returns a null serial for some devices (observed
# for the Hilo gateway since ~2026-09-10). Consumers build

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this date "2026-09-10" is today? Or is it September 10th?

If it is today, I'd strongly recommend always using ISO date format to avoid confusion.

# unique ids as f"{device.identifier.lower()}-..." and crash
# on None, so coerce back to the historic empty-string value
# that existing device registries were built from.
# mypy: the declared kwargs type excludes None, but the
# cloud really does send nulls — which is exactly the case
# being guarded here, hence the ignore below.
new_val = "" # type: ignore[unreachable]
setattr(self, att, new_val)
if self.model:
self.model = self.model.replace("Model_", "")
Expand Down
37 changes: 28 additions & 9 deletions tests/test_device.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from datetime import datetime, timezone
from datetime import datetime
from unittest.mock import MagicMock

import pytest

from pyhilo.const import HILO_READING_TYPES
from pyhilo.device import DeviceAttribute, DeviceReading, HiloDevice, get_device_attributes
from pyhilo.device import (
DeviceAttribute,
DeviceReading,
HiloDevice,
get_device_attributes,
)


class TestDeviceAttribute:
Expand Down Expand Up @@ -97,7 +100,9 @@ def test_equality_by_attr_name(self):

def test_inequality_different_attr(self):
r1 = self._make_reading(device_attribute=DeviceAttribute("Power", "Watt"))
r2 = self._make_reading(device_attribute=DeviceAttribute("Intensity", "Percentage"))
r2 = self._make_reading(
device_attribute=DeviceAttribute("Intensity", "Percentage")
)
assert r1 != r2

def test_repr(self):
Expand Down Expand Up @@ -227,13 +232,19 @@ def test_update_readings_replaces_existing(self):
d = HiloDevice(api, id=1)
da = DeviceAttribute("Power", "Watt")
r1 = DeviceReading(
deviceId=1, hiloId="h-1", locationId=10,
timeStampUTC="2024-01-15T10:30:00Z", value=100.0,
deviceId=1,
hiloId="h-1",
locationId=10,
timeStampUTC="2024-01-15T10:30:00Z",
value=100.0,
device_attribute=da,
)
r2 = DeviceReading(
deviceId=1, hiloId="h-1", locationId=10,
timeStampUTC="2024-01-15T11:00:00Z", value=200.0,
deviceId=1,
hiloId="h-1",
locationId=10,
timeStampUTC="2024-01-15T11:00:00Z",
value=200.0,
device_attribute=da,
)
d.update_readings(r1)
Expand Down Expand Up @@ -325,3 +336,11 @@ def test_get_attribute_unknown(self):
d = HiloDevice(api, id=1)
result = d.get_attribute("nonexistent")
assert result is None

def test_identifier_none_coerces_to_empty_string(self):
"""The cloud returns a null identifier for some devices (observed for
the Hilo gateway since ~2026-09-10); consumers build unique ids from
device.identifier and must not receive None."""
api = _mock_api()
d = HiloDevice(api, id=1, identifier=None)
assert d.identifier == ""