mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use enum sensor device class in Z-Wave (#92029)
* Use enum sensor device class where appropriate * update docstring
This commit is contained in:
parent
65837c9075
commit
96455c74f0
@ -491,7 +491,7 @@ class ZWaveMeterSensor(ZWaveNumericSensor):
|
|||||||
|
|
||||||
|
|
||||||
class ZWaveListSensor(ZwaveSensorBase):
|
class ZWaveListSensor(ZwaveSensorBase):
|
||||||
"""Representation of a Z-Wave Numeric sensor with multiple states."""
|
"""Representation of a Z-Wave List sensor with multiple states."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -509,19 +509,31 @@ class ZWaveListSensor(ZwaveSensorBase):
|
|||||||
# Entity class attributes
|
# Entity class attributes
|
||||||
self._attr_name = self.generate_name(include_value_name=True)
|
self._attr_name = self.generate_name(include_value_name=True)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self) -> SensorDeviceClass | None:
|
||||||
|
"""Return sensor device class."""
|
||||||
|
if super().device_class is not None:
|
||||||
|
return super().device_class
|
||||||
|
if self.info.primary_value.metadata.states:
|
||||||
|
return SensorDeviceClass.ENUM
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def options(self) -> list[str] | None:
|
||||||
|
"""Return options for enum sensor."""
|
||||||
|
if self.device_class == SensorDeviceClass.ENUM:
|
||||||
|
return list(self.info.primary_value.metadata.states.values())
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> str | None:
|
def native_value(self) -> str | None:
|
||||||
"""Return state of the sensor."""
|
"""Return state of the sensor."""
|
||||||
if self.info.primary_value.value is None:
|
if self.info.primary_value.value is None:
|
||||||
return None
|
return None
|
||||||
if (
|
key = str(self.info.primary_value.value)
|
||||||
str(self.info.primary_value.value)
|
if key not in self.info.primary_value.metadata.states:
|
||||||
not in self.info.primary_value.metadata.states
|
return key
|
||||||
):
|
return str(self.info.primary_value.metadata.states[key])
|
||||||
return str(self.info.primary_value.value)
|
|
||||||
return str(
|
|
||||||
self.info.primary_value.metadata.states[str(self.info.primary_value.value)]
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, str] | None:
|
def extra_state_attributes(self) -> dict[str, str] | None:
|
||||||
@ -557,22 +569,37 @@ class ZWaveConfigParameterSensor(ZwaveSensorBase):
|
|||||||
name_prefix="Config parameter",
|
name_prefix="Config parameter",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self) -> SensorDeviceClass | None:
|
||||||
|
"""Return sensor device class."""
|
||||||
|
if super().device_class is not None:
|
||||||
|
return super().device_class
|
||||||
|
if (
|
||||||
|
self._primary_value.configuration_value_type
|
||||||
|
== ConfigurationValueType.ENUMERATED
|
||||||
|
):
|
||||||
|
return SensorDeviceClass.ENUM
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def options(self) -> list[str] | None:
|
||||||
|
"""Return options for enum sensor."""
|
||||||
|
if self.device_class == SensorDeviceClass.ENUM:
|
||||||
|
return list(self.info.primary_value.metadata.states.values())
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> str | None:
|
def native_value(self) -> str | None:
|
||||||
"""Return state of the sensor."""
|
"""Return state of the sensor."""
|
||||||
if self.info.primary_value.value is None:
|
if self.info.primary_value.value is None:
|
||||||
return None
|
return None
|
||||||
|
key = str(self.info.primary_value.value)
|
||||||
if (
|
if (
|
||||||
self._primary_value.configuration_value_type == ConfigurationValueType.RANGE
|
self._primary_value.configuration_value_type == ConfigurationValueType.RANGE
|
||||||
or (
|
or (key not in self.info.primary_value.metadata.states)
|
||||||
str(self.info.primary_value.value)
|
|
||||||
not in self.info.primary_value.metadata.states
|
|
||||||
)
|
|
||||||
):
|
):
|
||||||
return str(self.info.primary_value.value)
|
return key
|
||||||
return str(
|
return str(self.info.primary_value.metadata.states[key])
|
||||||
self.info.primary_value.metadata.states[str(self.info.primary_value.value)]
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, str] | None:
|
def extra_state_attributes(self) -> dict[str, str] | None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user