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 AnalogInput cluster #147

Closed
wants to merge 6 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: 27 additions & 6 deletions zha/application/platforms/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from zha.application.platforms.climate.const import HVACAction
from zha.application.platforms.helpers import validate_device_class
from zha.application.platforms.number.const import UNITS
from zha.application.platforms.sensor.const import SensorDeviceClass, SensorStateClass
from zha.application.registries import PLATFORM_ENTITIES
from zha.decorators import periodic
Expand Down Expand Up @@ -476,16 +477,36 @@ def formatter(self, value: int) -> str | None:
return self._enum(value).name


@MULTI_MATCH(
cluster_handler_names=CLUSTER_HANDLER_ANALOG_INPUT,
manufacturers="Digi",
stop_on_match_group=CLUSTER_HANDLER_ANALOG_INPUT,
)
class AnalogInput(Sensor):
@CONFIG_DIAGNOSTIC_MATCH(cluster_handler_names=CLUSTER_HANDLER_ANALOG_INPUT)
class AnalogInputSensor(Sensor):
"""Sensor that displays analog input values."""

_attribute_name = "present_value"
_attr_translation_key: str = "analog_input"
_attr_entity_registry_enabled_default = False
_attr_extra_state_attribute_names: set[str] = {
"description",
"max_present_value",
"min_present_value",
"out_of_service",
"reliability",
"resolution",
"status_flags",
"application_type",
}

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)
engineering_units = self._cluster_handler.engineering_units
self._attr_native_unit_of_measurement = UNITS.get(engineering_units)


@MULTI_MATCH(cluster_handler_names=CLUSTER_HANDLER_POWER_CONFIGURATION)
Expand Down
70 changes: 70 additions & 0 deletions zha/zigbee/cluster_handlers/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,77 @@ class AnalogInputClusterHandler(ClusterHandler):
attr=AnalogInput.AttributeDefs.present_value.name,
config=REPORT_CONFIG_DEFAULT,
),
AttrReportConfig(
attr=AnalogInput.AttributeDefs.status_flags.name,
config=REPORT_CONFIG_DEFAULT,
),
)
ZCL_INIT_ATTRS = {
AnalogInput.AttributeDefs.description.name: True,
AnalogInput.AttributeDefs.max_present_value.name: True,
AnalogInput.AttributeDefs.min_present_value.name: True,
AnalogInput.AttributeDefs.out_of_service.name: True,
AnalogInput.AttributeDefs.reliability.name: True,
AnalogInput.AttributeDefs.resolution.name: True,
AnalogInput.AttributeDefs.engineering_units.name: True,
AnalogInput.AttributeDefs.application_type.name: True,
}

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

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

@property
def max_present_value(self) -> float | None:
"""Return cached value of max_present_value."""
return self.cluster.get(AnalogInput.AttributeDefs.max_present_value.name)

@property
def min_present_value(self) -> float | None:
"""Return cached value of min_present_value."""
return self.cluster.get(AnalogInput.AttributeDefs.min_present_value.name)

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

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

@property
def resolution(self) -> float | None:
"""Return cached value of resolution."""
return self.cluster.get(AnalogInput.AttributeDefs.resolution.name)

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

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

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

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


@registries.BINDABLE_CLUSTERS.register(AnalogOutput.cluster_id)
Expand Down
Loading