Skip to content

Commit

Permalink
Expose fallback_name in v2 quirks (zigpy#1474)
Browse files Browse the repository at this point in the history
* Expose `fallback_name` in v2 quirks

* Make the keys mandatory

* Revert "Make the keys mandatory"

This reverts commit 1ace648.

* Drop device class validation and mandate specifying translation keys and fallback names
  • Loading branch information
puddly authored Sep 25, 2024
1 parent 6493d57 commit 563dcfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 58 deletions.
38 changes: 0 additions & 38 deletions tests/test_quirks_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,44 +467,6 @@ async def test_quirks_v2_sensor(device_mock):
assert sensor_metadata.multiplier == 1


async def test_quirks_v2_sensor_validation_failure_translation_key(device_mock):
"""Test translation key and device class both set causes exception."""
registry = DeviceRegistry()

with pytest.raises(
ValueError, match="cannot have both a translation_key and a device_class"
):
(
QuirkBuilder(device_mock.manufacturer, device_mock.model, registry=registry)
.adds(OnOff.cluster_id)
.sensor(
OnOff.AttributeDefs.on_time.name,
OnOff.cluster_id,
device_class="bad",
translation_key="bad",
)
.add_to_registry()
)


async def test_quirks_v2_sensor_validation_failure_unit(device_mock):
"""Test unit and device class both set causes exception."""
registry = DeviceRegistry()

with pytest.raises(ValueError, match="cannot have both unit and device_class"):
(
QuirkBuilder(device_mock.manufacturer, device_mock.model, registry=registry)
.adds(OnOff.cluster_id)
.sensor(
OnOff.AttributeDefs.on_time.name,
OnOff.cluster_id,
device_class="bad",
unit="bad",
)
.add_to_registry()
)


async def test_quirks_v2_switch(device_mock):
"""Test adding a quirk that defines a switch to the registry."""
registry = DeviceRegistry()
Expand Down
36 changes: 16 additions & 20 deletions zigpy/quirks/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,33 +242,15 @@ class EntityMetadata:
cluster_type: ClusterType = attrs.field(default=ClusterType.Server)
initially_disabled: bool = attrs.field(default=False)
attribute_initialized_from_cache: bool = attrs.field(default=True)
translation_key: str | None = attrs.field(default=None)

def __attrs_post_init__(self) -> None:
self._validate()
translation_key: str = attrs.field()
fallback_name: str = attrs.field()

def __call__(self, device: CustomDeviceV2) -> None:
"""Add the entity metadata to the quirks v2 device."""
self._validate()
device.exposes_metadata[
(self.endpoint_id, self.cluster_id, self.cluster_type)
].append(self)

def _validate(self) -> None:
"""Validate the entity metadata."""
has_unit: bool = hasattr(self, "unit") and getattr(self, "unit") is not None
has_device_class: bool = hasattr(self, "device_class") and (
getattr(self, "device_class") is not None
)
if has_device_class and has_unit:
raise ValueError(
f"EntityMetadata cannot have both unit and device_class: {self}"
)
if has_device_class and self.translation_key is not None:
raise ValueError(
f"EntityMetadata cannot have both a translation_key and a device_class: {self}"
)


@attrs.define(frozen=True, kw_only=True, repr=True)
class ZCLEnumMetadata(EntityMetadata):
Expand Down Expand Up @@ -605,6 +587,7 @@ def enum(
initially_disabled: bool = False,
attribute_initialized_from_cache: bool = True,
translation_key: str | None = None,
fallback_name: str | None = None,
) -> QuirkBuilder:
"""Add an EntityMetadata containing ZCLEnumMetadata and return self.
Expand All @@ -620,6 +603,7 @@ def enum(
initially_disabled=initially_disabled,
attribute_initialized_from_cache=attribute_initialized_from_cache,
translation_key=translation_key,
fallback_name=fallback_name,
enum=enum_class,
attribute_name=attribute_name,
)
Expand All @@ -641,6 +625,7 @@ def sensor(
initially_disabled: bool = False,
attribute_initialized_from_cache: bool = True,
translation_key: str | None = None,
fallback_name: str | None = None,
) -> QuirkBuilder:
"""Add an EntityMetadata containing ZCLSensorMetadata and return self.
Expand All @@ -656,6 +641,7 @@ def sensor(
initially_disabled=initially_disabled,
attribute_initialized_from_cache=attribute_initialized_from_cache,
translation_key=translation_key,
fallback_name=fallback_name,
attribute_name=attribute_name,
divisor=divisor,
multiplier=multiplier,
Expand All @@ -680,6 +666,7 @@ def switch(
initially_disabled: bool = False,
attribute_initialized_from_cache: bool = True,
translation_key: str | None = None,
fallback_name: str | None = None,
) -> QuirkBuilder:
"""Add an EntityMetadata containing SwitchMetadata and return self.
Expand All @@ -695,6 +682,7 @@ def switch(
initially_disabled=initially_disabled,
attribute_initialized_from_cache=attribute_initialized_from_cache,
translation_key=translation_key,
fallback_name=fallback_name,
attribute_name=attribute_name,
force_inverted=force_inverted,
invert_attribute_name=invert_attribute_name,
Expand All @@ -720,6 +708,7 @@ def number(
initially_disabled: bool = False,
attribute_initialized_from_cache: bool = True,
translation_key: str | None = None,
fallback_name: str | None = None,
) -> QuirkBuilder:
"""Add an EntityMetadata containing NumberMetadata and return self.
Expand All @@ -735,6 +724,7 @@ def number(
initially_disabled=initially_disabled,
attribute_initialized_from_cache=attribute_initialized_from_cache,
translation_key=translation_key,
fallback_name=fallback_name,
attribute_name=attribute_name,
min=min_value,
max=max_value,
Expand All @@ -757,6 +747,7 @@ def binary_sensor(
initially_disabled: bool = False,
attribute_initialized_from_cache: bool = True,
translation_key: str | None = None,
fallback_name: str | None = None,
) -> QuirkBuilder:
"""Add an EntityMetadata containing BinarySensorMetadata and return self.
Expand All @@ -772,6 +763,7 @@ def binary_sensor(
initially_disabled=initially_disabled,
attribute_initialized_from_cache=attribute_initialized_from_cache,
translation_key=translation_key,
fallback_name=fallback_name,
attribute_name=attribute_name,
device_class=device_class,
)
Expand All @@ -789,6 +781,7 @@ def write_attr_button(
initially_disabled: bool = False,
attribute_initialized_from_cache: bool = True,
translation_key: str | None = None,
fallback_name: str | None = None,
) -> QuirkBuilder:
"""Add an EntityMetadata containing WriteAttributeButtonMetadata and return self.
Expand All @@ -805,6 +798,7 @@ def write_attr_button(
initially_disabled=initially_disabled,
attribute_initialized_from_cache=attribute_initialized_from_cache,
translation_key=translation_key,
fallback_name=fallback_name,
attribute_name=attribute_name,
attribute_value=attribute_value,
)
Expand All @@ -822,6 +816,7 @@ def command_button(
entity_type: EntityType = EntityType.CONFIG,
initially_disabled: bool = False,
translation_key: str | None = None,
fallback_name: str | None = None,
) -> QuirkBuilder:
"""Add an EntityMetadata containing ZCLCommandButtonMetadata and return self.
Expand All @@ -837,6 +832,7 @@ def command_button(
entity_type=entity_type,
initially_disabled=initially_disabled,
translation_key=translation_key,
fallback_name=fallback_name,
command_name=command_name,
args=command_args if command_args is not None else (),
kwargs=command_kwargs if command_kwargs is not None else frozendict(),
Expand Down

0 comments on commit 563dcfe

Please # to comment.