Restore sensorpush state when device becomes available (#98420)

pull/98480/head
J. Nick Koston 2023-08-15 09:29:25 -05:00 committed by GitHub
parent 3de402bd15
commit e209f3723e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 9 deletions

View File

@ -110,7 +110,9 @@ async def async_setup_entry(
SensorPushBluetoothSensorEntity, async_add_entities
)
)
entry.async_on_unload(coordinator.async_register_processor(processor))
entry.async_on_unload(
coordinator.async_register_processor(processor, SensorEntityDescription)
)
class SensorPushBluetoothSensorEntity(

View File

@ -32,3 +32,14 @@ HTPWX_SERVICE_INFO = BluetoothServiceInfo(
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"],
source="local",
)
HTPWX_EMPTY_SERVICE_INFO = BluetoothServiceInfo(
name="SensorPush HTP.xw F4D",
address="4125DDBA-2774-4851-9889-6AADDD4CAC3D",
rssi=-56,
manufacturer_data={},
service_data={},
service_uuids=["ef090000-11d6-42ba-93b8-9dd7ec090ab0"],
source="local",
)

View File

@ -1,17 +1,33 @@
"""Test the SensorPush sensors."""
from datetime import timedelta
import time
from unittest.mock import patch
from homeassistant.components.bluetooth import (
FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS,
)
from homeassistant.components.sensor import ATTR_STATE_CLASS
from homeassistant.components.sensorpush.const import DOMAIN
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util
from . import HTPWX_SERVICE_INFO
from . import HTPWX_EMPTY_SERVICE_INFO, HTPWX_SERVICE_INFO
from tests.common import MockConfigEntry
from tests.components.bluetooth import inject_bluetooth_service_info
from tests.common import MockConfigEntry, async_fire_time_changed
from tests.components.bluetooth import (
inject_bluetooth_service_info,
patch_all_discovered_devices,
)
async def test_sensors(hass: HomeAssistant) -> None:
"""Test setting up creates the sensors."""
start_monotonic = time.monotonic()
entry = MockConfigEntry(
domain=DOMAIN,
unique_id="4125DDBA-2774-4851-9889-6AADDD4CAC3D",
@ -27,11 +43,39 @@ async def test_sensors(hass: HomeAssistant) -> None:
assert len(hass.states.async_all()) == 3
temp_sensor = hass.states.get("sensor.htp_xw_f4d_temperature")
temp_sensor_attribtes = temp_sensor.attributes
temp_sensor_attributes = temp_sensor.attributes
assert temp_sensor.state == "20.11"
assert temp_sensor_attributes[ATTR_FRIENDLY_NAME] == "HTP.xw F4D Temperature"
assert temp_sensor_attributes[ATTR_UNIT_OF_MEASUREMENT] == "°C"
assert temp_sensor_attributes[ATTR_STATE_CLASS] == "measurement"
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
# Fastforward time without BLE advertisements
monotonic_now = start_monotonic + FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1
with patch(
"homeassistant.components.bluetooth.manager.MONOTONIC_TIME",
return_value=monotonic_now,
), patch_all_discovered_devices([]):
async_fire_time_changed(
hass,
dt_util.utcnow()
+ timedelta(seconds=FALLBACK_MAXIMUM_STALE_ADVERTISEMENT_SECONDS + 1),
)
await hass.async_block_till_done()
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
temp_sensor = hass.states.get("sensor.htp_xw_f4d_temperature")
assert temp_sensor.state == STATE_UNAVAILABLE
inject_bluetooth_service_info(hass, HTPWX_EMPTY_SERVICE_INFO)
await hass.async_block_till_done()
temp_sensor = hass.states.get("sensor.htp_xw_f4d_temperature")
assert temp_sensor.state == "20.11"
assert temp_sensor_attribtes[ATTR_FRIENDLY_NAME] == "HTP.xw F4D Temperature"
assert temp_sensor_attribtes[ATTR_UNIT_OF_MEASUREMENT] == "°C"
assert temp_sensor_attribtes[ATTR_STATE_CLASS] == "measurement"
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()