diff --git a/homeassistant/components/unifiprotect/icons.json b/homeassistant/components/unifiprotect/icons.json index df84c20d7b43..9fefe51ff47a 100644 --- a/homeassistant/components/unifiprotect/icons.json +++ b/homeassistant/components/unifiprotect/icons.json @@ -334,6 +334,9 @@ "sensitivity": { "default": "mdi:walk" }, + "signal_quality": { + "default": "mdi:signal" + }, "storage_utilization": { "default": "mdi:harddisk" }, diff --git a/homeassistant/components/unifiprotect/sensor.py b/homeassistant/components/unifiprotect/sensor.py index d4481c4af1c3..8c652aa6687a 100644 --- a/homeassistant/components/unifiprotect/sensor.py +++ b/homeassistant/components/unifiprotect/sensor.py @@ -370,6 +370,15 @@ SENSE_SENSORS: tuple[ProtectSensorEntityDescription, ...] = ( ufp_public_value="tampering_detected_at_dt", entity_registry_enabled_default=False, ), + ProtectSensorEntityDescription( + key="signal_quality", + translation_key="signal_quality", + native_unit_of_measurement=PERCENTAGE, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + state_class=SensorStateClass.MEASUREMENT, + ufp_public_value="wireless_connection_state.signal_state.signal_quality", + ), ProtectSensorEntityDescription( key="sensitivity", translation_key="sensitivity", @@ -601,6 +610,13 @@ def _fob_signal_strength(fob: Fob) -> int | None: return None +def _fob_signal_quality(fob: Fob) -> int | None: + """Return the key fob signal quality, if it has been reported.""" + if (signal := fob.wireless_connection_state.signal_state) is not None: + return signal.signal_quality + return None + + def _fob_status(fob: Fob) -> str | None: """Return the key fob presence state. @@ -638,6 +654,15 @@ FOB_SENSORS: tuple[ProtectFobSensorEntityDescription, ...] = ( state_class=SensorStateClass.MEASUREMENT, value_fn=_fob_signal_strength, ), + ProtectFobSensorEntityDescription( + key="signal_quality", + translation_key="signal_quality", + native_unit_of_measurement=PERCENTAGE, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + state_class=SensorStateClass.MEASUREMENT, + value_fn=_fob_signal_quality, + ), ProtectFobSensorEntityDescription( key="status", translation_key="fob_status", diff --git a/homeassistant/components/unifiprotect/strings.json b/homeassistant/components/unifiprotect/strings.json index a1a6cee0f231..24e7b3a19ddd 100644 --- a/homeassistant/components/unifiprotect/strings.json +++ b/homeassistant/components/unifiprotect/strings.json @@ -690,6 +690,9 @@ "resolution_hd_video": { "name": "Resolution: HD video" }, + "signal_quality": { + "name": "Signal quality" + }, "storage_used": { "name": "Storage used" }, diff --git a/tests/components/unifiprotect/test_fob.py b/tests/components/unifiprotect/test_fob.py index b54a5e23d0bc..d167a0c5c76a 100644 --- a/tests/components/unifiprotect/test_fob.py +++ b/tests/components/unifiprotect/test_fob.py @@ -45,6 +45,7 @@ FOB_NAME = "Front Door Fob" BATTERY_SENSOR = "sensor.front_door_fob_battery" SIGNAL_SENSOR = "sensor.front_door_fob_signal_strength" +SIGNAL_QUALITY_SENSOR = "sensor.front_door_fob_signal_quality" STATUS_SENSOR = "sensor.front_door_fob_status" BATTERY_LOW_BINARY = "binary_sensor.front_door_fob_battery" BUTTON_EVENT = "event.front_door_fob_button" @@ -57,6 +58,7 @@ def _make_fob( percentage: int | None = 80, is_low: bool = False, signal_strength: int | None = -55, + signal_quality: int | None = 90, state: DeviceState = DeviceState.CONNECTED, name: str | None = FOB_NAME, ) -> Mock: @@ -75,7 +77,7 @@ def _make_fob( percentage=percentage, is_low=is_low ), signal_state=PublicSignalState( - signal_strength=signal_strength, signal_quality=None + signal_strength=signal_strength, signal_quality=signal_quality ), ) return fob @@ -345,7 +347,7 @@ async def test_fob_present_at_startup_not_added_twice( if entry.unique_id.startswith(FOB_MAC) ] ) - == 5 + == 6 ) @@ -424,19 +426,28 @@ async def test_fob_battery_none_is_unknown( assert state.state == "unknown" +@pytest.mark.parametrize( + ("entity_id", "expected"), + [ + pytest.param(SIGNAL_SENSOR, "-55", id="strength"), + pytest.param(SIGNAL_QUALITY_SENSOR, "90", id="quality"), + ], +) async def test_fob_signal_sensor_when_enabled( hass: HomeAssistant, ufp_with_fob: tuple[MockUFPFixture, Mock], + entity_id: str, + expected: str, ) -> None: - """Enabling the signal-strength sensor exposes the fob's signal value.""" + """Enabling a signal sensor exposes the fob's signal value.""" ufp, _fob = ufp_with_fob await init_entry(hass, ufp, []) - await enable_entity(hass, ufp.entry.entry_id, SIGNAL_SENSOR) + await enable_entity(hass, ufp.entry.entry_id, entity_id) - state = hass.states.get(SIGNAL_SENSOR) + state = hass.states.get(entity_id) assert state is not None - assert state.state == "-55" + assert state.state == expected async def test_fob_event_entity_created_with_empty_feature_flags( @@ -531,6 +542,9 @@ async def test_fob_without_wireless_data_is_unknown( await enable_entity(hass, ufp.entry.entry_id, SIGNAL_SENSOR) assert hass.states.get(SIGNAL_SENSOR).state == "unknown" + await enable_entity(hass, ufp.entry.entry_id, SIGNAL_QUALITY_SENSOR) + assert hass.states.get(SIGNAL_QUALITY_SENSOR).state == "unknown" + async def test_fob_unavailable_when_public_bootstrap_lost( hass: HomeAssistant, @@ -570,8 +584,9 @@ async def test_fob_entity_counts( for entry in entity_registry.entities.values() if entry.unique_id.startswith(FOB_MAC) ] - # battery sensor, signal sensor, status sensor, battery-low binary, button event - assert len(fob_entities) == 5 + # battery, signal strength, signal quality and status sensors, battery-low + # binary, button event + assert len(fob_entities) == 6 assert sum(not entry.disabled for entry in fob_entities) == 4 @@ -619,7 +634,7 @@ async def test_fob_added_at_runtime( if entry.unique_id.startswith(FOB_MAC) ] ) - == 5 + == 6 ) diff --git a/tests/components/unifiprotect/test_sensor.py b/tests/components/unifiprotect/test_sensor.py index d3e0be09b508..b5c27903a234 100644 --- a/tests/components/unifiprotect/test_sensor.py +++ b/tests/components/unifiprotect/test_sensor.py @@ -99,11 +99,11 @@ async def test_sensor_sensor_remove( ufp.api.bootstrap.nvr.system_info.ustorage = None await init_entry(hass, ufp, [sensor_all]) - assert_entity_counts(hass, Platform.SENSOR, 22, 14) + assert_entity_counts(hass, Platform.SENSOR, 23, 14) await remove_entities(hass, ufp, [sensor_all]) assert_entity_counts(hass, Platform.SENSOR, 12, 9) await adopt_devices(hass, ufp, [sensor_all]) - assert_entity_counts(hass, Platform.SENSOR, 22, 14) + assert_entity_counts(hass, Platform.SENSOR, 23, 14) async def test_sensor_sense_capability_creation_filter( @@ -166,9 +166,9 @@ async def test_sensor_setup_sensor( ) -> None: """Test sensor entity setup for sensor devices.""" - setup_public_sensor(ufp) + setup_public_sensor(ufp, signal_quality=87) await init_entry(hass, ufp, [sensor_all]) - assert_entity_counts(hass, Platform.SENSOR, 22, 14) + assert_entity_counts(hass, Platform.SENSOR, 23, 14) expected_values = ( "10", @@ -213,6 +213,16 @@ async def test_sensor_setup_sensor( assert state.state == "-50" assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION + # Signal quality + _, entity_id = await ids_from_device_description( + hass, + Platform.SENSOR, + sensor_all, + get_sensor_by_key(SENSE_SENSORS, "signal_quality"), + ) + await enable_entity(hass, ufp.entry.entry_id, entity_id) + assert hass.states.get(entity_id).state == "87" + async def test_sensor_setup_sensor_none( hass: HomeAssistant, @@ -224,7 +234,7 @@ async def test_sensor_setup_sensor_none( setup_public_sensor(ufp) await init_entry(hass, ufp, [sensor]) - assert_entity_counts(hass, Platform.SENSOR, 22, 14) + assert_entity_counts(hass, Platform.SENSOR, 23, 14) expected_values = ( "10", @@ -633,7 +643,7 @@ async def test_sensor_update_alarm( """Test sensor motion entity.""" await init_entry(hass, ufp, [sensor_all]) - assert_entity_counts(hass, Platform.SENSOR, 22, 14) + assert_entity_counts(hass, Platform.SENSOR, 23, 14) _, entity_id = await ids_from_device_description( hass, @@ -687,7 +697,7 @@ async def test_sensor_update_alarm_with_last_trip_time( setup_public_sensor(ufp, tampering_detected_at=fixed_now - timedelta(hours=3)) await init_entry(hass, ufp, [sensor_all]) - assert_entity_counts(hass, Platform.SENSOR, 22, 22) + assert_entity_counts(hass, Platform.SENSOR, 23, 23) # Last Trip Time unique_id, entity_id = await ids_from_device_description( @@ -744,7 +754,7 @@ async def test_sensor_precision( """Test sensor precision value is respected.""" await init_entry(hass, ufp, [sensor_all]) - assert_entity_counts(hass, Platform.SENSOR, 22, 14) + assert_entity_counts(hass, Platform.SENSOR, 23, 14) nvr: NVR = ufp.api.bootstrap.nvr _, entity_id = await ids_from_device_description( @@ -821,7 +831,12 @@ async def test_public_only_sensor_sense_end_to_end( assert ufp_public_only.entry.state is ConfigEntryState.LOADED keys = registered_keys(entity_registry, Platform.SENSOR, sensor_all.mac) - assert {"battery_level", "temperature_level", "motion_last_trip_time"} <= keys + assert { + "battery_level", + "temperature_level", + "motion_last_trip_time", + "signal_quality", + } <= keys assert not keys & {"alarm_sound", "sensitivity", "mount_type", "paired_camera"} assert "humidity_level" not in keys diff --git a/tests/components/unifiprotect/utils.py b/tests/components/unifiprotect/utils.py index 8d58406c5f5b..d8d8f41b9ce8 100644 --- a/tests/components/unifiprotect/utils.py +++ b/tests/components/unifiprotect/utils.py @@ -48,6 +48,7 @@ from uiprotect.data.public_devices import ( PublicSensorMotionSettingsRead, PublicSensorStats, PublicSensorThresholdSettings, + PublicSignalState, PublicSmartDetectSettings, PublicWirelessBatteryStatus, PublicWirelessConnectionState, @@ -381,6 +382,7 @@ def make_public_sensor( humidity_value: float | None = None, temperature_value: float | None = None, tampering_detected_at: datetime | None = None, + signal_quality: int | None = None, ) -> Mock: """Build a public-API sensor mirroring a private sensor's migrated fields. @@ -462,7 +464,9 @@ def make_public_sensor( sensor.battery_status.percentage if percentage is None else percentage ), is_low=sensor.battery_status.is_low if is_low is None else is_low, - ) + ), + # The private sensor has no signal quality to mirror. + signal_state=PublicSignalState(signal_quality=signal_quality), ) # The fixture reports the same number for all three metrics, so a test that # has to tell the value paths apart passes its own.