Skip to content

Commit

Permalink
Add ability to translate ENUM sensor states in Unifi integration (#13…
Browse files Browse the repository at this point in the history
  • Loading branch information
bieniu authored Dec 18, 2024
1 parent 943b1d9 commit 3132700
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 97 deletions.
25 changes: 12 additions & 13 deletions homeassistant/components/unifi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
4 changes: 2 additions & 2 deletions homeassistant/components/unifi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions homeassistant/components/unifi/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
156 changes: 75 additions & 81 deletions tests/components/unifi/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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': <ANY>,
Expand Down Expand Up @@ -103,27 +102,26 @@
'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': <ANY>,
'entity_id': 'sensor.device_state',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'Connected',
'state': 'connected',
})
# ---
# name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.device_temperature-entry]
Expand Down Expand Up @@ -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': <ANY>,
Expand Down Expand Up @@ -584,27 +581,26 @@
'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': <ANY>,
'entity_id': 'sensor.dummy_usp_pdu_pro_state',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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]
Expand Down Expand Up @@ -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': <ANY>,
Expand Down Expand Up @@ -1649,27 +1644,26 @@
'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': <ANY>,
'entity_id': 'sensor.mock_name_state',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'Connected',
'state': 'connected',
})
# ---
# name: test_entity_and_device_data[wlan_payload0-device_payload0-client_payload0-config_entry_options0][sensor.mock_name_uptime-entry]
Expand Down
5 changes: 4 additions & 1 deletion tests/components/unifi/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from homeassistant.const import (
ATTR_DEVICE_CLASS,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
EntityCategory,
Platform,
)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 3132700

Please sign in to comment.