Skip to content

Commit

Permalink
fix: handle empty last_message_received
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Oct 5, 2023
1 parent 3d17614 commit a3e3709
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion custom_components/weenect/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def native_value(self) -> StateType:
self.entity_description.key
]
if self.device_class == str(SensorDeviceClass.TIMESTAMP):
return dt.parse_datetime(value)
if value:
return dt.parse_datetime(value)
return value
return None
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ def get_trackers_last_freq_mode_none_fixture():
side_effect=AsyncMock(return_value=response),
):
yield


@pytest.fixture(name="get_trackers_last_message_none")
def get_trackers_last_message_none_fixture():
"""Static result when retrieving data from weenect."""
response = copy.deepcopy(GET_RACKERS_RESPONSE)
response["items"][0]["position"][0]["last_message"] = None
with patch(
"aioweenect.AioWeenect.get_trackers",
side_effect=AsyncMock(return_value=response),
):
yield
15 changes: 14 additions & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def test_sensor(hass):


@pytest.mark.usefixtures("get_trackers")
async def test_device_class_does_not_return_strin_for_its_state(hass, caplog):
async def test_device_class_does_not_return_string_for_its_state(hass, caplog):
"""Test that sensor works."""
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)
Expand All @@ -58,3 +58,16 @@ async def test_device_class_does_not_return_strin_for_its_state(hass, caplog):
"is providing a string for its state, while the device class is"
not in caplog.text
)


@pytest.mark.usefixtures("get_trackers_last_message_none")
async def test_sensor_with_last_message_none(hass):
"""Test that the special timestamp sensor works for a None value ."""
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)

await hass.config_entries.async_setup(config_entry.entry_id)

await hass.async_block_till_done()

assert hass.states.get("sensor.test_last_message_received").state == "unknown"

0 comments on commit a3e3709

Please sign in to comment.