From 3132700492bea7ab9ba6c42ba0689ef18a6a55e6 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Wed, 18 Dec 2024 14:02:44 +0000 Subject: [PATCH] Add ability to translate ENUM sensor states in Unifi integration (#131921) --- homeassistant/components/unifi/const.py | 25 ++- homeassistant/components/unifi/sensor.py | 4 +- homeassistant/components/unifi/strings.json | 20 +++ .../unifi/snapshots/test_sensor.ambr | 156 +++++++++--------- tests/components/unifi/test_sensor.py | 5 +- 5 files changed, 113 insertions(+), 97 deletions(-) diff --git a/homeassistant/components/unifi/const.py b/homeassistant/components/unifi/const.py index 2b16895a9a85c4..bbd03b070a477c 100644 --- a/homeassistant/components/unifi/const.py +++ b/homeassistant/components/unifi/const.py @@ -50,17 +50,16 @@ OUTLET_SWITCH = "outlet" DEVICE_STATES = { - DeviceState.DISCONNECTED: "Disconnected", - DeviceState.CONNECTED: "Connected", - DeviceState.PENDING: "Pending", - DeviceState.FIRMWARE_MISMATCH: "Firmware Mismatch", - DeviceState.UPGRADING: "Upgrading", - DeviceState.PROVISIONING: "Provisioning", - DeviceState.HEARTBEAT_MISSED: "Heartbeat Missed", - DeviceState.ADOPTING: "Adopting", - DeviceState.DELETING: "Deleting", - DeviceState.INFORM_ERROR: "Inform Error", - DeviceState.ADOPTION_FALIED: "Adoption Failed", - DeviceState.ISOLATED: "Isolated", - DeviceState.UNKNOWN: "Unknown", + DeviceState.DISCONNECTED: "disconnected", + DeviceState.CONNECTED: "connected", + DeviceState.PENDING: "pending", + DeviceState.FIRMWARE_MISMATCH: "firmware_mismatch", + DeviceState.UPGRADING: "upgrading", + DeviceState.PROVISIONING: "provisioning", + DeviceState.HEARTBEAT_MISSED: "heartbeat_missed", + DeviceState.ADOPTING: "adopting", + DeviceState.DELETING: "deleting", + DeviceState.INFORM_ERROR: "inform_error", + DeviceState.ADOPTION_FALIED: "adoption_failed", + DeviceState.ISOLATED: "isolated", } diff --git a/homeassistant/components/unifi/sensor.py b/homeassistant/components/unifi/sensor.py index 74d49db6e4e7b1..194a8575174404 100644 --- a/homeassistant/components/unifi/sensor.py +++ b/homeassistant/components/unifi/sensor.py @@ -205,9 +205,9 @@ def async_client_is_connected_fn(hub: UnifiHub, obj_id: str) -> bool: @callback -def async_device_state_value_fn(hub: UnifiHub, device: Device) -> str: +def async_device_state_value_fn(hub: UnifiHub, device: Device) -> str | None: """Retrieve the state of the device.""" - return DEVICE_STATES[device.state] + return DEVICE_STATES.get(device.state) @callback diff --git a/homeassistant/components/unifi/strings.json b/homeassistant/components/unifi/strings.json index 1c7317c42678c8..f9315318d1ec46 100644 --- a/homeassistant/components/unifi/strings.json +++ b/homeassistant/components/unifi/strings.json @@ -33,6 +33,26 @@ "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } }, + "entity": { + "sensor": { + "device_state": { + "state": { + "disconnected": "[%key:common::state::disconnected%]", + "connected": "[%key:common::state::connected%]", + "pending": "Pending", + "firmware_mismatch": "Firmware mismatch", + "upgrading": "Upgrading", + "provisioning": "Provisioning", + "heartbeat_missed": "Heartbeat missed", + "adopting": "Adopting", + "deleting": "Deleting", + "inform_error": "Inform error", + "adoption_failed": "Adoption failed", + "isolated": "Isolated" + } + } + } + }, "options": { "abort": { "integration_not_setup": "UniFi integration is not set up" diff --git a/tests/components/unifi/snapshots/test_sensor.ambr b/tests/components/unifi/snapshots/test_sensor.ambr index fc86a57a294d82..e14658b2b96276 100644 --- a/tests/components/unifi/snapshots/test_sensor.ambr +++ b/tests/components/unifi/snapshots/test_sensor.ambr @@ -55,19 +55,18 @@ 'area_id': None, 'capabilities': dict({ 'options': list([ - 'Disconnected', - 'Connected', - 'Pending', - 'Firmware Mismatch', - 'Upgrading', - 'Provisioning', - 'Heartbeat Missed', - 'Adopting', - 'Deleting', - 'Inform Error', - 'Adoption Failed', - 'Isolated', - 'Unknown', + 'disconnected', + 'connected', + 'pending', + 'firmware_mismatch', + 'upgrading', + 'provisioning', + 'heartbeat_missed', + 'adopting', + 'deleting', + 'inform_error', + 'adoption_failed', + 'isolated', ]), }), 'config_entry_id': , @@ -103,19 +102,18 @@ 'device_class': 'enum', 'friendly_name': 'Device State', 'options': list([ - 'Disconnected', - 'Connected', - 'Pending', - 'Firmware Mismatch', - 'Upgrading', - 'Provisioning', - 'Heartbeat Missed', - 'Adopting', - 'Deleting', - 'Inform Error', - 'Adoption Failed', - 'Isolated', - 'Unknown', + 'disconnected', + 'connected', + 'pending', + 'firmware_mismatch', + 'upgrading', + 'provisioning', + 'heartbeat_missed', + 'adopting', + 'deleting', + 'inform_error', + 'adoption_failed', + 'isolated', ]), }), 'context': , @@ -123,7 +121,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'Connected', + 'state': 'connected', }) # --- # name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.device_temperature-entry] @@ -536,19 +534,18 @@ 'area_id': None, 'capabilities': dict({ 'options': list([ - 'Disconnected', - 'Connected', - 'Pending', - 'Firmware Mismatch', - 'Upgrading', - 'Provisioning', - 'Heartbeat Missed', - 'Adopting', - 'Deleting', - 'Inform Error', - 'Adoption Failed', - 'Isolated', - 'Unknown', + 'disconnected', + 'connected', + 'pending', + 'firmware_mismatch', + 'upgrading', + 'provisioning', + 'heartbeat_missed', + 'adopting', + 'deleting', + 'inform_error', + 'adoption_failed', + 'isolated', ]), }), 'config_entry_id': , @@ -584,19 +581,18 @@ 'device_class': 'enum', 'friendly_name': 'Dummy USP-PDU-Pro State', 'options': list([ - 'Disconnected', - 'Connected', - 'Pending', - 'Firmware Mismatch', - 'Upgrading', - 'Provisioning', - 'Heartbeat Missed', - 'Adopting', - 'Deleting', - 'Inform Error', - 'Adoption Failed', - 'Isolated', - 'Unknown', + 'disconnected', + 'connected', + 'pending', + 'firmware_mismatch', + 'upgrading', + 'provisioning', + 'heartbeat_missed', + 'adopting', + 'deleting', + 'inform_error', + 'adoption_failed', + 'isolated', ]), }), 'context': , @@ -604,7 +600,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'Connected', + 'state': 'connected', }) # --- # name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.dummy_usp_pdu_pro_uptime-entry] @@ -1601,19 +1597,18 @@ 'area_id': None, 'capabilities': dict({ 'options': list([ - 'Disconnected', - 'Connected', - 'Pending', - 'Firmware Mismatch', - 'Upgrading', - 'Provisioning', - 'Heartbeat Missed', - 'Adopting', - 'Deleting', - 'Inform Error', - 'Adoption Failed', - 'Isolated', - 'Unknown', + 'disconnected', + 'connected', + 'pending', + 'firmware_mismatch', + 'upgrading', + 'provisioning', + 'heartbeat_missed', + 'adopting', + 'deleting', + 'inform_error', + 'adoption_failed', + 'isolated', ]), }), 'config_entry_id': , @@ -1649,19 +1644,18 @@ 'device_class': 'enum', 'friendly_name': 'mock-name State', 'options': list([ - 'Disconnected', - 'Connected', - 'Pending', - 'Firmware Mismatch', - 'Upgrading', - 'Provisioning', - 'Heartbeat Missed', - 'Adopting', - 'Deleting', - 'Inform Error', - 'Adoption Failed', - 'Isolated', - 'Unknown', + 'disconnected', + 'connected', + 'pending', + 'firmware_mismatch', + 'upgrading', + 'provisioning', + 'heartbeat_missed', + 'adopting', + 'deleting', + 'inform_error', + 'adoption_failed', + 'isolated', ]), }), 'context': , @@ -1669,7 +1663,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'Connected', + 'state': 'connected', }) # --- # name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.mock_name_uptime-entry] diff --git a/tests/components/unifi/test_sensor.py b/tests/components/unifi/test_sensor.py index 3c94d12018d8a1..5e47d26307981b 100644 --- a/tests/components/unifi/test_sensor.py +++ b/tests/components/unifi/test_sensor.py @@ -30,6 +30,7 @@ from homeassistant.const import ( ATTR_DEVICE_CLASS, STATE_UNAVAILABLE, + STATE_UNKNOWN, EntityCategory, Platform, ) @@ -891,7 +892,9 @@ async def test_device_state( for i in list(map(int, DeviceState)): device["state"] = i mock_websocket_message(message=MessageKey.DEVICE, data=device) - assert hass.states.get("sensor.device_state").state == DEVICE_STATES[i] + assert hass.states.get("sensor.device_state").state == DEVICE_STATES.get( + i, STATE_UNKNOWN + ) @pytest.mark.parametrize(