Add icon and state translations for zwave_js sensors (#109186)

This commit is contained in:
Raman Gupta 2024-01-30 23:38:27 -05:00 committed by GitHub
parent ac8f555a70
commit b8c9da4705
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 40 deletions

View File

@ -0,0 +1,24 @@
{
"entity": {
"sensor": {
"controller_status": {
"default": "mdi:help-rhombus",
"state": {
"ready": "mdi:check",
"unresponsive": "mdi:bell-off",
"jammed": "mdi:lock"
}
},
"node_status": {
"default": "mdi:help-rhombus",
"state": {
"alive": "mdi:heart-pulse",
"asleep": "mdi:sleep",
"awake": "mdi:eye",
"dead": "mdi:robot-dead",
"unknown": "mdi:help-rhombus"
}
}
}
}
}

View File

@ -6,7 +6,7 @@ from typing import cast
import voluptuous as vol
from zwave_js_server.client import Client as ZwaveClient
from zwave_js_server.const import CommandClass, ControllerStatus, NodeStatus
from zwave_js_server.const import CommandClass
from zwave_js_server.const.command_class.meter import (
RESET_METER_OPTION_TARGET_VALUE,
RESET_METER_OPTION_TYPE,
@ -91,20 +91,6 @@ from .helpers import get_device_info, get_valueless_base_unique_id
PARALLEL_UPDATES = 0
CONTROLLER_STATUS_ICON: dict[ControllerStatus, str] = {
ControllerStatus.READY: "mdi:check",
ControllerStatus.UNRESPONSIVE: "mdi:bell-off",
ControllerStatus.JAMMED: "mdi:lock",
}
NODE_STATUS_ICON: dict[NodeStatus, str] = {
NodeStatus.ALIVE: "mdi:heart-pulse",
NodeStatus.ASLEEP: "mdi:sleep",
NodeStatus.AWAKE: "mdi:eye",
NodeStatus.DEAD: "mdi:robot-dead",
NodeStatus.UNKNOWN: "mdi:help-rhombus",
}
# These descriptions should include device class.
ENTITY_DESCRIPTION_KEY_DEVICE_CLASS_MAP: dict[
@ -784,6 +770,7 @@ class ZWaveNodeStatusSensor(SensorEntity):
_attr_should_poll = False
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_has_entity_name = True
_attr_translation_key = "node_status"
def __init__(
self, config_entry: ConfigEntry, driver: Driver, node: ZwaveNode
@ -793,7 +780,6 @@ class ZWaveNodeStatusSensor(SensorEntity):
self.node = node
# Entity class attributes
self._attr_name = "Node status"
self._base_unique_id = get_valueless_base_unique_id(driver, node)
self._attr_unique_id = f"{self._base_unique_id}.node_status"
# device may not be precreated in main handler yet
@ -815,11 +801,6 @@ class ZWaveNodeStatusSensor(SensorEntity):
self._attr_native_value = self.node.status.name.lower()
self.async_write_ha_state()
@property
def icon(self) -> str | None:
"""Icon of the entity."""
return NODE_STATUS_ICON[self.node.status]
async def async_added_to_hass(self) -> None:
"""Call when entity is added."""
# Add value_changed callbacks.
@ -852,6 +833,7 @@ class ZWaveControllerStatusSensor(SensorEntity):
_attr_should_poll = False
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_has_entity_name = True
_attr_translation_key = "controller_status"
def __init__(self, config_entry: ConfigEntry, driver: Driver) -> None:
"""Initialize a generic Z-Wave device entity."""
@ -861,7 +843,6 @@ class ZWaveControllerStatusSensor(SensorEntity):
assert node
# Entity class attributes
self._attr_name = "Status"
self._base_unique_id = get_valueless_base_unique_id(driver, node)
self._attr_unique_id = f"{self._base_unique_id}.controller_status"
# device may not be precreated in main handler yet
@ -883,11 +864,6 @@ class ZWaveControllerStatusSensor(SensorEntity):
self._attr_native_value = self.controller.status.name.lower()
self.async_write_ha_state()
@property
def icon(self) -> str | None:
"""Icon of the entity."""
return CONTROLLER_STATUS_ICON[self.controller.status]
async def async_added_to_hass(self) -> None:
"""Call when entity is added."""
# Add value_changed callbacks.

View File

@ -1,4 +1,26 @@
{
"entity": {
"sensor": {
"node_status": {
"name": "Node status",
"state": {
"alive": "Alive",
"asleep": "Asleep",
"awake": "Awake",
"dead": "Dead",
"unknown": "Unknown"
}
},
"controller_status": {
"name": "Status",
"state": {
"ready": "Ready",
"unresponsive": "Unresponsive",
"jammed": "Jammed"
}
}
}
},
"config": {
"flow_title": "{name}",
"step": {

View File

@ -25,7 +25,6 @@ from homeassistant.components.zwave_js.helpers import get_valueless_base_unique_
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
PERCENTAGE,
STATE_UNAVAILABLE,
@ -318,7 +317,6 @@ async def test_controller_status_sensor(
state = hass.states.get(entity_id)
assert state
assert state.state == "ready"
assert state.attributes[ATTR_ICON] == "mdi:check"
event = Event(
"status changed",
@ -328,7 +326,6 @@ async def test_controller_status_sensor(
state = hass.states.get(entity_id)
assert state
assert state.state == "unresponsive"
assert state.attributes[ATTR_ICON] == "mdi:bell-off"
# Test transitions work
event = Event(
@ -339,7 +336,6 @@ async def test_controller_status_sensor(
state = hass.states.get(entity_id)
assert state
assert state.state == "jammed"
assert state.attributes[ATTR_ICON] == "mdi:lock"
# Disconnect the client and make sure the entity is still available
await client.disconnect()
@ -365,33 +361,24 @@ async def test_node_status_sensor(
)
node.receive_event(event)
assert hass.states.get(node_status_entity_id).state == "dead"
assert (
hass.states.get(node_status_entity_id).attributes[ATTR_ICON] == "mdi:robot-dead"
)
event = Event(
"wake up", data={"source": "node", "event": "wake up", "nodeId": node.node_id}
)
node.receive_event(event)
assert hass.states.get(node_status_entity_id).state == "awake"
assert hass.states.get(node_status_entity_id).attributes[ATTR_ICON] == "mdi:eye"
event = Event(
"sleep", data={"source": "node", "event": "sleep", "nodeId": node.node_id}
)
node.receive_event(event)
assert hass.states.get(node_status_entity_id).state == "asleep"
assert hass.states.get(node_status_entity_id).attributes[ATTR_ICON] == "mdi:sleep"
event = Event(
"alive", data={"source": "node", "event": "alive", "nodeId": node.node_id}
)
node.receive_event(event)
assert hass.states.get(node_status_entity_id).state == "alive"
assert (
hass.states.get(node_status_entity_id).attributes[ATTR_ICON]
== "mdi:heart-pulse"
)
# Disconnect the client and make sure the entity is still available
await client.disconnect()