Skip to content

Commit

Permalink
Bump zigpy from 0.66.0 to 0.67.0 (#212)
Browse files Browse the repository at this point in the history
* Bump zigpy from 0.66.0 to 0.67.0

Bumps [zigpy](https://github.com/zigpy/zigpy) from 0.66.0 to 0.67.0.
- [Release notes](https://github.com/zigpy/zigpy/releases)
- [Commits](zigpy/zigpy@0.66.0...0.67.0)

---
updated-dependencies:
- dependency-name: zigpy
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* Fix discovery tests

* Support `fallback_name`

* Add a unit test for `fallback_name`

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: puddly <[email protected]>
  • Loading branch information
dependabot[bot] and puddly authored Sep 25, 2024
1 parent 65b22a0 commit 62ec158
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 75 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
license = {text = "Apache-2.0"}
requires-python = ">=3.12"
dependencies = [
"zigpy==0.66.0",
"zigpy==0.67.0",
"bellows==0.40.6",
"zigpy-znp==0.12.4",
"zigpy-deconz==0.23.3",
Expand Down
115 changes: 41 additions & 74 deletions tests/test_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ async def test_quirks_v2_entity_discovery(
step=1,
unit=UnitOfTime.SECONDS,
multiplier=1,
translation_key="off_wait_time",
fallback_name="Off wait time",
)
.add_to_registry()
)
Expand Down Expand Up @@ -580,15 +582,24 @@ class FakeXiaomiAqaraDriverE1(XiaomiAqaraDriverE1):
BasicCluster.cluster_id,
entity_platform=Platform.SENSOR,
entity_type=EntityType.DIAGNOSTIC,
translation_key="power_source",
fallback_name="Power source",
)
.enum(
"hooks_state",
AqaraE1HookState,
FakeXiaomiAqaraDriverE1.cluster_id,
entity_platform=Platform.SENSOR,
entity_type=EntityType.DIAGNOSTIC,
translation_key="hooks_state",
fallback_name="Hooks state",
)
.binary_sensor(
"error_detected",
FakeXiaomiAqaraDriverE1.cluster_id,
translation_key="error_detected",
fallback_name="Error detected",
)
.binary_sensor("error_detected", FakeXiaomiAqaraDriverE1.cluster_id)
.add_to_registry()
)

Expand Down Expand Up @@ -800,7 +811,7 @@ async def test_quirks_v2_entity_discovery_errors(
"entity_type=<EntityType.CONFIG: 'config'>, cluster_id=6, endpoint_id=1, "
"cluster_type=<ClusterType.Server: 0>, initially_disabled=False, "
"attribute_initialized_from_cache=True, translation_key='analog_input', "
"attribute_name='off_wait_time', divisor=1, multiplier=1, "
"fallback_name=None, attribute_name='off_wait_time', divisor=1, multiplier=1, "
"unit=None, device_class=None, state_class=None)}"
)
# fmt: on
Expand Down Expand Up @@ -877,33 +888,6 @@ def validate_translation_keys_device_class(
raise ValueError(f"{m1}{m2}{quirk}")


def bad_device_class_unit_combination(
quirk_builder: QuirkBuilder,
) -> QuirkBuilder:
"""Introduce a bad device class and unit combination."""
return quirk_builder.sensor(
zigpy.zcl.clusters.general.OnOff.AttributeDefs.off_wait_time.name,
zigpy.zcl.clusters.general.OnOff.cluster_id,
entity_type=EntityType.CONFIG,
unit="invalid",
device_class="invalid",
translation_key="analog_input",
)


def bad_device_class_translation_key_usage(
quirk_builder: QuirkBuilder,
) -> QuirkBuilder:
"""Introduce a bad device class and translation key combination."""
return quirk_builder.sensor(
zigpy.zcl.clusters.general.OnOff.AttributeDefs.off_wait_time.name,
zigpy.zcl.clusters.general.OnOff.cluster_id,
entity_type=EntityType.CONFIG,
translation_key="invalid",
device_class="invalid",
)


def validate_metadata(validator: Callable) -> None:
"""Ensure v2 quirks metadata does not violate HA rules."""
all_v2_quirks = itertools.chain.from_iterable(
Expand All @@ -916,51 +900,6 @@ def validate_metadata(validator: Callable) -> None:
validator(quirk, entity_metadata, platform, translations)


@pytest.mark.parametrize(
("augment_method", "validate_method", "expected_exception_string"),
[
(
bad_device_class_unit_combination,
validate_device_class_unit,
"cannot have both unit and device_class",
),
(
bad_device_class_translation_key_usage,
validate_translation_keys_device_class,
"cannot have both a translation_key and a device_class",
),
],
)
async def test_quirks_v2_metadata_errors(
zha_gateway: Gateway, # pylint: disable=unused-argument
zigpy_device_mock,
device_joined: Callable[[zigpy.device.Device], Awaitable[Device]],
augment_method: Callable[[QuirkBuilder], QuirkBuilder],
validate_method: Callable,
expected_exception_string: str,
) -> None:
"""Ensure all v2 quirks translation keys exist."""

# no error yet
validate_metadata(validate_method)

# ensure the error is caught and raised
with pytest.raises(ValueError, match=expected_exception_string):
# introduce an error
zigpy_device = _get_test_device(
zigpy_device_mock,
"Ikea of Sweden4",
"TRADFRI remote control4",
augment_method=augment_method,
)
await device_joined(zigpy_device)

validate_metadata(validate_method)
# if the device was created we remove it
# so we don't pollute the rest of the tests
zigpy.quirks._DEVICE_REGISTRY.remove(zigpy_device)


class BadDeviceClass(enum.Enum):
"""Bad device class."""

Expand Down Expand Up @@ -1048,6 +987,34 @@ async def test_quirks_v2_metadata_bad_device_classes(
zigpy.quirks._DEVICE_REGISTRY.remove(zigpy_device)


async def test_quirks_v2_fallback_name(
zha_gateway: Gateway, # pylint: disable=unused-argument
zigpy_device_mock,
device_joined: Callable[[zigpy.device.Device], Awaitable[Device]],
) -> None:
"""Test quirks v2 fallback name."""

zigpy_device = _get_test_device(
zigpy_device_mock,
"Ikea of Sweden6",
"TRADFRI remote control6",
augment_method=lambda builder: builder.sensor(
attribute_name=zigpy.zcl.clusters.general.OnOff.AttributeDefs.off_wait_time.name,
cluster_id=zigpy.zcl.clusters.general.OnOff.cluster_id,
translation_key="some_sensor",
fallback_name="Fallback name",
),
)
zha_device = await device_joined(zigpy_device)

entity = get_entity(
zha_device,
platform=Platform.SENSOR,
qualifier_func=lambda e: e.fallback_name == "Fallback name",
)
assert entity.fallback_name == "Fallback name"


def pytest_generate_tests(metafunc):
"""Generate tests for all device files."""
if "file_path" in metafunc.fixturenames:
Expand Down
4 changes: 4 additions & 0 deletions zha/application/platforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,12 @@ def _init_from_quirks_metadata(self, entity_metadata: EntityMetadata) -> None:
has_device_class = hasattr(entity_metadata, "device_class")
has_attribute_name = hasattr(entity_metadata, "attribute_name")
has_command_name = hasattr(entity_metadata, "command_name")
has_fallback_name = hasattr(entity_metadata, "fallback_name")

if not has_device_class or entity_metadata.device_class is None:
if has_fallback_name:
self._attr_fallback_name = entity_metadata.fallback_name

if entity_metadata.translation_key:
self._attr_translation_key = entity_metadata.translation_key
elif has_attribute_name:
Expand Down

0 comments on commit 62ec158

Please sign in to comment.