diff --git a/homeassistant/util/frozen_dataclass_compat.py b/homeassistant/util/frozen_dataclass_compat.py index 58faedeea6f..456fc4f1570 100644 --- a/homeassistant/util/frozen_dataclass_compat.py +++ b/homeassistant/util/frozen_dataclass_compat.py @@ -117,4 +117,5 @@ class FrozenOrThawed(type): return object.__new__(cls) return cls._dataclass(*_args, **kwargs) + cls.__init__ = cls._dataclass.__init__ # type: ignore[misc] cls.__new__ = __new__ # type: ignore[method-assign] diff --git a/tests/helpers/snapshots/test_entity.ambr b/tests/helpers/snapshots/test_entity.ambr index 7f146fa0494..1031134d2ad 100644 --- a/tests/helpers/snapshots/test_entity.ambr +++ b/tests/helpers/snapshots/test_entity.ambr @@ -36,6 +36,24 @@ # name: test_extending_entity_description.1 "test_extending_entity_description..FrozenEntityDescription(key='blah', device_class=None, entity_category=None, entity_registry_enabled_default=True, entity_registry_visible_default=True, force_update=False, icon=None, has_entity_name=False, name='name', translation_key=None, unit_of_measurement=None, extra='foo')" # --- +# name: test_extending_entity_description.10 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.11 + "test_extending_entity_description..CustomInitEntityDescription(key='blah', device_class=None, entity_category=None, entity_registry_enabled_default=True, entity_registry_visible_default=True, force_update=False, icon=None, has_entity_name=False, name='name', translation_key=None, unit_of_measurement=None)" +# --- # name: test_extending_entity_description.2 dict({ 'device_class': None, diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index 5a706b73b49..76577daf8a6 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -1749,3 +1749,15 @@ def test_extending_entity_description(snapshot: SnapshotAssertion): key="blah", extra="foo", mixin="mixin", name="name" ) assert repr(obj) == snapshot + + # Try inheriting with custom init + @dataclasses.dataclass + class CustomInitEntityDescription(entity.EntityDescription): + def __init__(self, extra, *args, **kwargs) -> None: + super().__init__(*args, **kwargs) + self.extra: str = extra + + obj = CustomInitEntityDescription(key="blah", extra="foo", name="name") + assert obj == snapshot + assert obj == CustomInitEntityDescription(key="blah", extra="foo", name="name") + assert repr(obj) == snapshot