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 icon and state translations for zwave_js sensors #109186

Merged
merged 1 commit into from
Jan 31, 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
24 changes: 24 additions & 0 deletions homeassistant/components/zwave_js/icons.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
30 changes: 3 additions & 27 deletions homeassistant/components/zwave_js/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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,
Expand Down Expand Up @@ -91,20 +91,6 @@

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[
Expand Down Expand Up @@ -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
Expand All @@ -793,7 +780,6 @@ def __init__(
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
Expand All @@ -815,11 +801,6 @@ def _status_changed(self, _: dict) -> None:
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.
Expand Down Expand Up @@ -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."""
Expand All @@ -861,7 +843,6 @@ def __init__(self, config_entry: ConfigEntry, driver: Driver) -> None:
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
Expand All @@ -883,11 +864,6 @@ def _status_changed(self, _: dict) -> None:
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.
Expand Down
22 changes: 22 additions & 0 deletions homeassistant/components/zwave_js/strings.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
13 changes: 0 additions & 13 deletions tests/components/zwave_js/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
PERCENTAGE,
STATE_UNAVAILABLE,
Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Loading