Skip to content

Commit 8434f19

Browse files
committed
Remove attribute_converter support for switch
1 parent a2ca056 commit 8434f19

File tree

2 files changed

+4
-60
lines changed

2 files changed

+4
-60
lines changed

tests/test_switch.py

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from zigpy.quirks.v2 import CustomDeviceV2, QuirkBuilder
1919
import zigpy.types as t
2020
from zigpy.zcl.clusters import closures, general
21-
from zigpy.zcl.clusters.general import OnOff
2221
from zigpy.zcl.clusters.manufacturer_specific import ManufacturerSpecificCluster
2322
import zigpy.zcl.foundation as zcl_f
2423

@@ -851,52 +850,3 @@ async def test_cover_inversion_switch_not_created(zha_gateway: Gateway) -> None:
851850
# entity should not be created when mode or config status aren't present
852851
with pytest.raises(KeyError):
853852
get_entity(zha_device, platform=Platform.SWITCH)
854-
855-
856-
async def test_quirks_switch_attr_converter(zha_gateway: Gateway) -> None:
857-
"""Test ZHA quirks v2 switch with attribute_converter."""
858-
859-
registry = DeviceRegistry()
860-
zigpy_dev = create_mock_zigpy_device(
861-
zha_gateway,
862-
{
863-
1: {
864-
SIG_EP_INPUT: [
865-
general.Basic.cluster_id,
866-
general.OnOff.cluster_id,
867-
],
868-
SIG_EP_OUTPUT: [],
869-
SIG_EP_TYPE: zha.DeviceType.SIMPLE_SENSOR,
870-
}
871-
},
872-
manufacturer="manufacturer",
873-
model="model",
874-
)
875-
876-
(
877-
QuirkBuilder(zigpy_dev.manufacturer, zigpy_dev.model, registry=registry)
878-
.switch(
879-
OnOff.AttributeDefs.on_off.name,
880-
OnOff.cluster_id,
881-
translation_key="on_off_quirks",
882-
fallback_name="On/off",
883-
attribute_converter=lambda x: not bool(x), # invert value with lambda
884-
)
885-
.add_to_registry()
886-
)
887-
888-
zigpy_device_ = registry.get_device(zigpy_dev)
889-
890-
assert isinstance(zigpy_device_, CustomDeviceV2)
891-
cluster = zigpy_device_.endpoints[1].on_off
892-
893-
zha_device = await join_zigpy_device(zha_gateway, zigpy_device_)
894-
entity = get_entity(zha_device, platform=Platform.SWITCH, qualifier="on_off")
895-
assert entity.translation_key == "on_off_quirks"
896-
897-
# send updated value, check if the value is inverted
898-
await send_attributes_report(zha_gateway, cluster, {"on_off": 1})
899-
assert entity.state["state"] is False
900-
901-
await send_attributes_report(zha_gateway, cluster, {"on_off": 0})
902-
assert entity.state["state"] is True

zha/application/platforms/switch.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from dataclasses import dataclass
77
import functools
88
import logging
9-
import typing
109
from typing import TYPE_CHECKING, Any, Self, cast
1110

1211
from zhaquirks.quirk_ids import DANFOSS_ALLY_THERMOSTAT, TUYA_PLUG_ONOFF
@@ -191,7 +190,6 @@ class ConfigurableAttributeSwitch(PlatformEntity):
191190

192191
_attr_entity_category = EntityCategory.CONFIG
193192
_attribute_name: str
194-
_attribute_converter: typing.Callable[[typing.Any], typing.Any] | None = None
195193
_inverter_attribute_name: str | None = None
196194
_force_inverted: bool = False
197195
_off_value: int = 0
@@ -245,8 +243,6 @@ def _init_from_quirks_metadata(self, entity_metadata: SwitchMetadata) -> None:
245243
"""Init this entity from the quirks metadata."""
246244
super()._init_from_quirks_metadata(entity_metadata)
247245
self._attribute_name = entity_metadata.attribute_name
248-
if entity_metadata.attribute_converter is not None:
249-
self._attribute_converter = entity_metadata.attribute_converter
250246
if entity_metadata.invert_attribute_name:
251247
self._inverter_attribute_name = entity_metadata.invert_attribute_name
252248
if entity_metadata.force_inverted:
@@ -286,13 +282,11 @@ def inverted(self) -> bool:
286282
@property
287283
def is_on(self) -> bool:
288284
"""Return if the switch is on based on the statemachine."""
289-
raw_state = self._cluster_handler.cluster.get(self._attribute_name)
290-
if self._attribute_converter:
291-
val = self._attribute_converter(raw_state)
292-
elif self._on_value != 1:
293-
val = raw_state == self._on_value
285+
if self._on_value != 1:
286+
val = self._cluster_handler.cluster.get(self._attribute_name)
287+
val = val == self._on_value
294288
else:
295-
val = bool(raw_state)
289+
val = bool(self._cluster_handler.cluster.get(self._attribute_name))
296290
return (not val) if self.inverted else val
297291

298292
def handle_cluster_handler_attribute_updated(

0 commit comments

Comments
 (0)