Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to translate ENUM sensor states in Unifi integration #131921

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Kane610 marked this conversation as resolved.
Show resolved Hide resolved


@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