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 sensor entities for the MultistateInput cluster #167

Closed
wants to merge 4 commits into from
Closed
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
33 changes: 33 additions & 0 deletions zha/application/platforms/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
CLUSTER_HANDLER_HUMIDITY,
CLUSTER_HANDLER_ILLUMINANCE,
CLUSTER_HANDLER_LEAF_WETNESS,
CLUSTER_HANDLER_MULTISTATE_INPUT,
CLUSTER_HANDLER_POWER_CONFIGURATION,
CLUSTER_HANDLER_PRESSURE,
CLUSTER_HANDLER_SMARTENERGY_METERING,
Expand Down Expand Up @@ -502,6 +503,38 @@ def formatter(self, value: int) -> str | None:
return self._enum(value).name


@CONFIG_DIAGNOSTIC_MATCH(cluster_handler_names=CLUSTER_HANDLER_MULTISTATE_INPUT)
class MultiStateInputSensor(EnumSensor):
"""Sensor that displays enumerated values."""

_attribute_name = "present_value"
_unique_id_suffix = "present_value"
_attr_translation_key: str = "multistate_input"
_attr_entity_registry_enabled_default = False
_attr_extra_state_attribute_names: set[str] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_attr_extra_state_attribute_names are going away and should be converted into entities themselves, if they're necessary.

"state_text",
"description",
"number_of_states",
"out_of_service",
"reliability",
"status_flags",
"application_type",
}
_enum = enum.Enum("Empty", names=())

def __init__(
self,
unique_id: str,
cluster_handlers: list[ClusterHandler],
endpoint: Endpoint,
device: Device,
**kwargs: Any,
) -> None:
"""Init this sensor."""
super().__init__(unique_id, cluster_handlers, endpoint, device, **kwargs)
self._enum = enum.Enum("state_text", self._cluster_handler.state_text.value)


@MULTI_MATCH(
cluster_handler_names=CLUSTER_HANDLER_ANALOG_INPUT,
manufacturers="Digi",
Expand Down
55 changes: 55 additions & 0 deletions zha/zigbee/cluster_handlers/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,61 @@ class MultistateInputClusterHandler(ClusterHandler):
config=REPORT_CONFIG_DEFAULT,
),
)
ZCL_INIT_ATTRS = {
MultistateInput.AttributeDefs.state_text.name,
MultistateInput.AttributeDefs.description.name,
MultistateInput.AttributeDefs.number_of_states.name,
MultistateInput.AttributeDefs.out_of_service.name,
MultistateInput.AttributeDefs.reliability.name,
MultistateInput.AttributeDefs.status_flags.name,
MultistateInput.AttributeDefs.application_type.name,
}

@property
def present_value(self) -> int | None:
"""Return cached value of present_value."""
return self.cluster.get(MultistateInput.AttributeDefs.present_value.name)

@property
def state_text(self) -> t.LVList | None:
"""Return cached value of state_text."""
return self.cluster.get(MultistateInput.AttributeDefs.state_text.name)

@property
def description(self) -> str | None:
"""Return cached value of description."""
return self.cluster.get(MultistateInput.AttributeDefs.description.name)

@property
def number_of_states(self) -> int | None:
"""Return cached value of number_of_states."""
return self.cluster.get(MultistateInput.AttributeDefs.number_of_states.name)

@property
def out_of_service(self) -> bool | None:
"""Return cached value of out_of_service."""
return self.cluster.get(MultistateInput.AttributeDefs.out_of_service.name)

@property
def reliability(self) -> int | None:
"""Return cached value of reliability."""
return self.cluster.get(MultistateInput.AttributeDefs.reliability.name)

@property
def status_flags(self) -> int | None:
"""Return cached value of status_flags."""
return self.cluster.get(MultistateInput.AttributeDefs.status_flags.name)

@property
def application_type(self) -> int | None:
"""Return cached value of application_type."""
return self.cluster.get(MultistateInput.AttributeDefs.application_type.name)

async def async_update(self):
"""Update cluster value attribute."""
await self.get_attribute_value(
MultistateInput.AttributeDefs.present_value.name, from_cache=False
)


@registries.CLUSTER_HANDLER_REGISTRY.register(MultistateOutput.cluster_id)
Expand Down
Loading