Fix zwave_js Notification CC sensors and binary sensors (#46072)

* only include property key name in sensor name if it exists

* add endpoint to binary_sensor and sensor notification CC entities if > 0

* refactor to have helper method generate name

* change default behavior of generate_name

* return value for notification sensor when we can't find the state

* store generated name
pull/46094/head
Raman Gupta 2021-02-05 16:36:42 -06:00 committed by GitHub
parent 67392338da
commit 33169cf8cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 33 deletions

View File

@ -293,6 +293,10 @@ class ZWaveNotificationBinarySensor(ZWaveBaseEntity, BinarySensorEntity):
"""Initialize a ZWaveNotificationBinarySensor entity."""
super().__init__(config_entry, client, info)
self.state_key = state_key
self._name = self.generate_name(
self.info.primary_value.property_name,
[self.info.primary_value.metadata.states[self.state_key]],
)
# check if we have a custom mapping for this value
self._mapping_info = self._get_sensor_mapping()
@ -301,14 +305,6 @@ class ZWaveNotificationBinarySensor(ZWaveBaseEntity, BinarySensorEntity):
"""Return if the sensor is on or off."""
return int(self.info.primary_value.value) == int(self.state_key)
@property
def name(self) -> str:
"""Return default name from device name and value name combination."""
node_name = self.info.node.name or self.info.node.device_config.description
value_name = self.info.primary_value.property_name
state_label = self.info.primary_value.metadata.states[self.state_key]
return f"{node_name}: {value_name} - {state_label}"
@property
def device_class(self) -> Optional[str]:
"""Return device class."""

View File

@ -1,7 +1,7 @@
"""Generic Z-Wave Entity Class."""
import logging
from typing import Optional, Tuple, Union
from typing import List, Optional, Tuple, Union
from zwave_js_server.client import Client as ZwaveClient
from zwave_js_server.model.node import Node as ZwaveNode
@ -35,6 +35,7 @@ class ZWaveBaseEntity(Entity):
self.config_entry = config_entry
self.client = client
self.info = info
self._name = self.generate_name()
# entities requiring additional values, can add extra ids to this list
self.watched_value_ids = {self.info.primary_value.value_id}
@ -61,19 +62,35 @@ class ZWaveBaseEntity(Entity):
"identifiers": {get_device_id(self.client, self.info.node)},
}
@property
def name(self) -> str:
"""Return default name from device name and value name combination."""
def generate_name(
self,
alternate_value_name: Optional[str] = None,
additional_info: Optional[List[str]] = None,
) -> str:
"""Generate entity name."""
if additional_info is None:
additional_info = []
node_name = self.info.node.name or self.info.node.device_config.description
value_name = (
self.info.primary_value.metadata.label
alternate_value_name
or self.info.primary_value.metadata.label
or self.info.primary_value.property_key_name
or self.info.primary_value.property_name
)
name = f"{node_name}: {value_name}"
for item in additional_info:
if item:
name += f" - {item}"
# append endpoint if > 1
if self.info.primary_value.endpoint > 1:
value_name += f" ({self.info.primary_value.endpoint})"
return f"{node_name}: {value_name}"
name += f" ({self.info.primary_value.endpoint})"
return name
@property
def name(self) -> str:
"""Return default name from device name and value name combination."""
return self._name
@property
def unique_id(self) -> str:

View File

@ -123,6 +123,17 @@ class ZWaveStringSensor(ZwaveSensorBase):
class ZWaveNumericSensor(ZwaveSensorBase):
"""Representation of a Z-Wave Numeric sensor."""
def __init__(
self,
config_entry: ConfigEntry,
client: ZwaveClient,
info: ZwaveDiscoveryInfo,
) -> None:
"""Initialize a ZWaveNumericSensor entity."""
super().__init__(config_entry, client, info)
if self.info.primary_value.command_class == CommandClass.BASIC:
self._name = self.generate_name(self.info.primary_value.command_class_name)
@property
def state(self) -> float:
"""Return state of the sensor."""
@ -142,19 +153,23 @@ class ZWaveNumericSensor(ZwaveSensorBase):
return str(self.info.primary_value.metadata.unit)
@property
def name(self) -> str:
"""Return default name from device name and value name combination."""
if self.info.primary_value.command_class == CommandClass.BASIC:
node_name = self.info.node.name or self.info.node.device_config.description
label = self.info.primary_value.command_class_name
return f"{node_name}: {label}"
return super().name
class ZWaveListSensor(ZwaveSensorBase):
"""Representation of a Z-Wave Numeric sensor with multiple states."""
def __init__(
self,
config_entry: ConfigEntry,
client: ZwaveClient,
info: ZwaveDiscoveryInfo,
) -> None:
"""Initialize a ZWaveListSensor entity."""
super().__init__(config_entry, client, info)
self._name = self.generate_name(
self.info.primary_value.property_name,
[self.info.primary_value.property_key_name],
)
@property
def state(self) -> Optional[str]:
"""Return state of the sensor."""
@ -164,7 +179,7 @@ class ZWaveListSensor(ZwaveSensorBase):
not str(self.info.primary_value.value)
in self.info.primary_value.metadata.states
):
return None
return str(self.info.primary_value.value)
return str(
self.info.primary_value.metadata.states[str(self.info.primary_value.value)]
)
@ -174,11 +189,3 @@ class ZWaveListSensor(ZwaveSensorBase):
"""Return the device specific state attributes."""
# add the value's int value as property for multi-value (list) items
return {"value": self.info.primary_value.value}
@property
def name(self) -> str:
"""Return default name from device name and value name combination."""
node_name = self.info.node.name or self.info.node.device_config.description
prop_name = self.info.primary_value.property_name
prop_key_name = self.info.primary_value.property_key_name
return f"{node_name}: {prop_name} - {prop_key_name}"