Skip to content

Commit

Permalink
Fix issue with custom device_class after Hass v2024.1 #1333
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Feb 13, 2024
1 parent 7a9aea1 commit 63eb385
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 12 additions & 3 deletions custom_components/sonoff/core/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
}


def unwrap_cached_properties(attrs: dict):
"""Fix metaclass CachedProperties problem in latest Hass."""
for k, v in list(attrs.items()):
if k.startswith("_attr_") and f"_{k}" in attrs and isinstance(v, property):
attrs[k] = attrs.pop(f"_{k}")
return attrs


def spec(cls, base: str = None, enabled: bool = None, **kwargs) -> type:
"""Make duplicate for cls class with changes in kwargs params.
Expand All @@ -84,9 +92,10 @@ def spec(cls, base: str = None, enabled: bool = None, **kwargs) -> type:
if enabled is not None:
kwargs["_attr_entity_registry_enabled_default"] = enabled
if base:
bases = cls.__mro__[-len(XSwitch.__mro__) :: -1]
bases = {k: v for b in bases for k, v in b.__dict__.items()}
return type(cls.__name__, DEVICE_CLASS[base], {**bases, **kwargs})
attrs = cls.__mro__[-len(XSwitch.__mro__) :: -1]
attrs = {k: v for b in attrs for k, v in b.__dict__.items()}
attrs = unwrap_cached_properties({**attrs, **kwargs})
return type(cls.__name__, DEVICE_CLASS[base], attrs)
return type(cls.__name__, (cls,), kwargs)


Expand Down
6 changes: 6 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio

from custom_components.sonoff.core.devices import spec
from custom_components.sonoff.core.ewelink import XDevice, XRegistry, XRegistryLocal
from custom_components.sonoff.light import XLightL1
from . import save_to


Expand Down Expand Up @@ -47,3 +49,7 @@ def test_issue_1160():
"9b0810bc-557a-406c-8266-614767890531",
)
assert payload == {"switches": [{"outlet": 0, "switch": "off"}]}


def test_issue_1333():
assert spec(XLightL1, base="light")

0 comments on commit 63eb385

Please sign in to comment.