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

Use enum sensor device class in Z-Wave #92029

Merged
merged 2 commits into from
Apr 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 44 additions & 17 deletions homeassistant/components/zwave_js/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@


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__(
self,
Expand All @@ -509,19 +509,31 @@
# Entity class attributes
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

Check warning on line 516 in homeassistant/components/zwave_js/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/zwave_js/sensor.py#L516

Added line #L516 was not covered by tests
if self.info.primary_value.metadata.states:
return SensorDeviceClass.ENUM
raman325 marked this conversation as resolved.
Show resolved Hide resolved
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
def native_value(self) -> str | None:
"""Return state of the sensor."""
if self.info.primary_value.value is None:
return None
if (
str(self.info.primary_value.value)
not in self.info.primary_value.metadata.states
):
return str(self.info.primary_value.value)
return str(
self.info.primary_value.metadata.states[str(self.info.primary_value.value)]
)
key = str(self.info.primary_value.value)
if key not in self.info.primary_value.metadata.states:
return key

Check warning on line 535 in homeassistant/components/zwave_js/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/zwave_js/sensor.py#L535

Added line #L535 was not covered by tests
return str(self.info.primary_value.metadata.states[key])

@property
def extra_state_attributes(self) -> dict[str, str] | None:
Expand Down Expand Up @@ -557,22 +569,37 @@
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

Check warning on line 576 in homeassistant/components/zwave_js/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/zwave_js/sensor.py#L576

Added line #L576 was not covered by tests
if (
self._primary_value.configuration_value_type
== ConfigurationValueType.ENUMERATED
):
return SensorDeviceClass.ENUM
return None

Check warning on line 582 in homeassistant/components/zwave_js/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/zwave_js/sensor.py#L582

Added line #L582 was not covered by tests

@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

Check warning on line 589 in homeassistant/components/zwave_js/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/zwave_js/sensor.py#L589

Added line #L589 was not covered by tests

@property
def native_value(self) -> str | None:
"""Return state of the sensor."""
if self.info.primary_value.value is None:
return None
key = str(self.info.primary_value.value)

Check warning on line 596 in homeassistant/components/zwave_js/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/zwave_js/sensor.py#L596

Added line #L596 was not covered by tests
if (
self._primary_value.configuration_value_type == ConfigurationValueType.RANGE
or (
str(self.info.primary_value.value)
not in self.info.primary_value.metadata.states
)
or (key not in self.info.primary_value.metadata.states)
):
return str(self.info.primary_value.value)
return str(
self.info.primary_value.metadata.states[str(self.info.primary_value.value)]
)
return key
return str(self.info.primary_value.metadata.states[key])

Check warning on line 602 in homeassistant/components/zwave_js/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/zwave_js/sensor.py#L601-L602

Added lines #L601 - L602 were not covered by tests

@property
def extra_state_attributes(self) -> dict[str, str] | None:
Expand Down