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
This commit is contained in:
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

@ -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}"