-
Notifications
You must be signed in to change notification settings - Fork 75
Add attribute_converter/value_converter to number entities
#851
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| import functools | ||
| import logging | ||
|
|
@@ -259,13 +260,17 @@ class NumberConfigurationEntity(BaseNumber): | |
| _attr_native_step: float = 1.0 | ||
| _multiplier: float = 1 | ||
| _attribute_name: str | ||
| _attribute_converter: Callable[[Any], Any] | None = None | ||
| _value_converter: Callable[[Any], Any] | None = None | ||
|
|
||
| def __init__( | ||
| self, | ||
| endpoint: Endpoint, | ||
| device: Device, | ||
| *, | ||
| attribute_name: str | None = None, | ||
| attribute_converter: Callable[[Any], Any] | None = None, | ||
| value_converter: Callable[[Any], Any] | None = None, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| min_value: float | None = None, | ||
| max_value: float | None = None, | ||
| step: float | None = None, | ||
|
|
@@ -278,6 +283,10 @@ def __init__( | |
| """Init this number configuration entity.""" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document the converters here: which direction each one goes, that they take precedence over
|
||
| if attribute_name is not None: | ||
| self._attribute_name = attribute_name | ||
| if attribute_converter is not None: | ||
| self._attribute_converter = attribute_converter | ||
| if value_converter is not None: | ||
| self._value_converter = value_converter | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please mirror quirks#5215's both-or-neither validation here.
The asymmetry is a property of this class's behavior, so the invariant reads better here — raise in |
||
| if min_value is not None: | ||
| self._attr_native_min_value = min_value | ||
| if max_value is not None: | ||
|
|
@@ -344,13 +353,17 @@ def native_value(self) -> float | None: | |
| value = self._cluster.get(self._attribute_name) | ||
| if value is None: | ||
| return None | ||
| if self._attribute_converter: | ||
| return self._attribute_converter(value) | ||
| return value * self._multiplier | ||
|
|
||
| async def async_set_native_value(self, value: float) -> None: | ||
| """Update the current value from HA.""" | ||
| await write_attributes_safe( | ||
| self._cluster, {self._attribute_name: int(value / self._multiplier)} | ||
| ) | ||
| if self._value_converter: | ||
| attr_value = self._value_converter(value) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth documenting here: the default branch below forces So a converter written the obvious way, A blanket |
||
| else: | ||
| attr_value = int(value / self._multiplier) | ||
| await write_attributes_safe(self._cluster, {self._attribute_name: attr_value}) | ||
| self.maybe_emit_state_changed_event() | ||
|
|
||
| async def async_update(self) -> None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests construct the entity directly, which skips the plumbing the feature actually ships through:
zhaquirks/builder/discovery.py::_platform_kwargs, i.e. exactly the lines quirks#5215 adds. Right now nothing in either repo covers that mapping — #5215's tests stop atNumberMetadata.The precedent in this repo goes end to end:
test_quirks_sensor_attr_converter(tests/test_sensor.py:1911) andtest_quirks_binary_sensor_attr_converter(tests/test_binary_sensor.py:259) both build throughQuirkBuilder(...).sensor(attribute_converter=...)→registry.resolve→join_zigpy_device→get_entity.I don't think that's achievable in this PR —
QuirkBuilder.number()won't accept the kwargs until #5215 ships, and #5215 needs this released first — so keeping the direct-construction tests here is reasonable. Please add theQuirkBuilder-based test once the ordering allows (a zha follow-up, or in #5215 after the zha release), so the discovery mapping doesn't stay permanently untested.