Skip to content

Commit

Permalink
Use description attribute for fallback_name
Browse files Browse the repository at this point in the history
  • Loading branch information
prairiesnpr committed Sep 7, 2024
1 parent c6edfd4 commit 8d78297
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ async def test_number(
assert cluster.read_attributes.call_count == 3

assert entity.description == "PWM1"
assert entity.fallback_name == "PWM1"
assert entity.translation_key is None

# test that the state is 15.0
assert entity.state["state"] == 15.0
Expand Down Expand Up @@ -179,6 +181,35 @@ async def test_number(
assert entity.state["state"] == 30.0


async def test_number_missing_description_attr(
zigpy_analog_output_device: ZigpyDevice, # pylint: disable=redefined-outer-name
device_joined: Callable[[ZigpyDevice], Awaitable[Device]],
zha_gateway: Gateway,
) -> None:
"""Test zha number platform - missing description attribute."""
cluster: general.AnalogOutput = zigpy_analog_output_device.endpoints.get(
1
).analog_output
cluster.PLUGGED_ATTR_READS = {
"max_present_value": 100.0,
"min_present_value": 1.0,
"relinquish_default": 50.0,
"resolution": 1.1,
"engineering_units": 98,
"application_type": 4 * 0x10000,
}
update_attribute_cache(cluster)

zha_device = await device_joined(zigpy_analog_output_device)

entity: PlatformEntity = get_entity(zha_device, platform=Platform.NUMBER)
assert isinstance(entity, PlatformEntity)

assert entity.description is None
assert entity.fallback_name is None
assert entity.translation_key == "number"


@pytest.mark.parametrize(
("attr", "initial_value", "new_value", "max_value"),
(
Expand Down
6 changes: 6 additions & 0 deletions zha/application/platforms/binary_sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def __init__(
CLUSTER_HANDLER_ATTRIBUTE_UPDATED,
self.handle_cluster_handler_attribute_updated,
)
if (
hasattr(self._cluster_handler, "description")
and self._cluster_handler.description is not None
):
self._attr_translation_key = None
self._attr_fallback_name: str = self._cluster_handler.description

Check warning on line 85 in zha/application/platforms/binary_sensor/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/binary_sensor/__init__.py#L84-L85

Added lines #L84 - L85 were not covered by tests

def _init_from_quirks_metadata(self, entity_metadata: BinarySensorMetadata) -> None:
"""Init this entity from the quirks metadata."""
Expand Down
8 changes: 8 additions & 0 deletions zha/application/platforms/number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ def __init__(
CLUSTER_HANDLER_ATTRIBUTE_UPDATED,
self.handle_cluster_handler_attribute_updated,
)
if (
hasattr(self._analog_output_cluster_handler, "description")
and self._analog_output_cluster_handler.description is not None
):
self._attr_translation_key = None
self._attr_fallback_name: str = (
self._analog_output_cluster_handler.description
)

@functools.cached_property
def info_object(self) -> NumberEntityInfo:
Expand Down
6 changes: 6 additions & 0 deletions zha/application/platforms/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ def __init__(
CLUSTER_HANDLER_ATTRIBUTE_UPDATED,
self.handle_cluster_handler_attribute_updated,
)
if (
hasattr(self._cluster_handler, "description")
and self._cluster_handler.description is not None
):
self._attr_translation_key = None
self._attr_fallback_name: str = self._cluster_handler.description

Check warning on line 201 in zha/application/platforms/sensor/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/sensor/__init__.py#L200-L201

Added lines #L200 - L201 were not covered by tests

def _init_from_quirks_metadata(self, entity_metadata: ZCLSensorMetadata) -> None:
"""Init this entity from the quirks metadata."""
Expand Down

0 comments on commit 8d78297

Please sign in to comment.