diff --git a/tests/helpers/snapshots/test_entity.ambr b/tests/helpers/snapshots/test_entity.ambr index 1031134d2ada3..cec9d05c8e10a 100644 --- a/tests/helpers/snapshots/test_entity.ambr +++ b/tests/helpers/snapshots/test_entity.ambr @@ -42,17 +42,99 @@ 'entity_category': None, 'entity_registry_enabled_default': True, 'entity_registry_visible_default': True, + 'extra': 'foo', 'force_update': False, 'has_entity_name': False, 'icon': None, 'key': 'blah', + 'mixin': 'mixin', '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)" + "test_extending_entity_description..ComplexEntityDescription1C(mixin='mixin', 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.12 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.13 + "test_extending_entity_description..ComplexEntityDescription1D(mixin='mixin', 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.14 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.15 + "test_extending_entity_description..ComplexEntityDescription2A(mixin='mixin', 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.16 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.17 + "test_extending_entity_description..ComplexEntityDescription2B(mixin='mixin', 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.18 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.19 + "test_extending_entity_description..ComplexEntityDescription2C(mixin='mixin', 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.2 dict({ @@ -70,9 +152,127 @@ 'unit_of_measurement': None, }) # --- +# name: test_extending_entity_description.20 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.21 + "test_extending_entity_description..ComplexEntityDescription2D(mixin='mixin', 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.22 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.23 + "test_extending_entity_description..ComplexEntityDescription3A(mixin='mixin', 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.24 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.25 + "test_extending_entity_description..ComplexEntityDescription3B(mixin='mixin', 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.26 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.27 + "test_extending_entity_description..ComplexEntityDescription4A(mixin='mixin', 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.28 + dict({ + 'device_class': None, + 'entity_category': None, + 'entity_registry_enabled_default': True, + 'entity_registry_visible_default': True, + 'extra': 'foo', + 'force_update': False, + 'has_entity_name': False, + 'icon': None, + 'key': 'blah', + 'mixin': 'mixin', + 'name': 'name', + 'translation_key': None, + 'unit_of_measurement': None, + }) +# --- +# name: test_extending_entity_description.29 + "test_extending_entity_description..ComplexEntityDescription4B(mixin='mixin', 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.3 "test_extending_entity_description..ThawedEntityDescription(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.30 + 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.31 + "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.4 dict({ 'device_class': None, @@ -111,7 +311,7 @@ }) # --- # name: test_extending_entity_description.7 - "test_extending_entity_description..ComplexEntityDescription1(mixin='mixin', 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')" + "test_extending_entity_description..ComplexEntityDescription1A(mixin='mixin', 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.8 dict({ @@ -131,5 +331,5 @@ }) # --- # name: test_extending_entity_description.9 - "test_extending_entity_description..ComplexEntityDescription2(mixin='mixin', 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')" + "test_extending_entity_description..ComplexEntityDescription1B(mixin='mixin', 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')" # --- diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index 96bbf95a98673..b85c8794fed56 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -1716,31 +1716,201 @@ class MyExtendedEntityDescription(ExtendedEntityDescription): # Try multiple direct parents @dataclasses.dataclass(frozen=True) - class MyMixin: + class MyMixin1: + mixin: str + + @dataclasses.dataclass + class MyMixin2: + mixin: str + + @dataclasses.dataclass(frozen=True) + class MyMixin3: + mixin: str = None + + @dataclasses.dataclass + class MyMixin4: mixin: str = None @dataclasses.dataclass(frozen=True, kw_only=True) - class ComplexEntityDescription1(MyMixin, entity.EntityDescription): + class ComplexEntityDescription1A(MyMixin1, entity.EntityDescription): extra: str = None - obj = ComplexEntityDescription1(key="blah", extra="foo", mixin="mixin", name="name") + obj = ComplexEntityDescription1A( + key="blah", extra="foo", mixin="mixin", name="name" + ) assert obj == snapshot - assert obj == ComplexEntityDescription1( + assert obj == ComplexEntityDescription1A( key="blah", extra="foo", mixin="mixin", name="name" ) assert repr(obj) == snapshot @dataclasses.dataclass(frozen=True, kw_only=True) - class ComplexEntityDescription2(entity.EntityDescription, MyMixin): + class ComplexEntityDescription1B(entity.EntityDescription, MyMixin1): + extra: str = None + + obj = ComplexEntityDescription1B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription1B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass(frozen=True) + class ComplexEntityDescription1C(MyMixin1, entity.EntityDescription): + extra: str = None + + obj = ComplexEntityDescription1C( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription1C( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass(frozen=True) + class ComplexEntityDescription1D(entity.EntityDescription, MyMixin1): extra: str = None - obj = ComplexEntityDescription2(key="blah", extra="foo", mixin="mixin", name="name") + obj = ComplexEntityDescription1D( + key="blah", extra="foo", mixin="mixin", name="name" + ) assert obj == snapshot - assert obj == ComplexEntityDescription2( + assert obj == ComplexEntityDescription1D( key="blah", extra="foo", mixin="mixin", name="name" ) assert repr(obj) == snapshot + @dataclasses.dataclass(kw_only=True) + class ComplexEntityDescription2A(MyMixin2, entity.EntityDescription): + extra: str = None + + obj = ComplexEntityDescription2A( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription2A( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass(kw_only=True) + class ComplexEntityDescription2B(entity.EntityDescription, MyMixin2): + extra: str = None + + obj = ComplexEntityDescription2B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription2B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass + class ComplexEntityDescription2C(MyMixin2, entity.EntityDescription): + extra: str = None + + obj = ComplexEntityDescription2C( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription2C( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass + class ComplexEntityDescription2D(entity.EntityDescription, MyMixin2): + extra: str = None + + obj = ComplexEntityDescription2D( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription2D( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass(frozen=True, kw_only=True) + class ComplexEntityDescription3A(MyMixin3, entity.EntityDescription): + extra: str = None + + obj = ComplexEntityDescription3A( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription3A( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass(frozen=True, kw_only=True) + class ComplexEntityDescription3B(entity.EntityDescription, MyMixin3): + extra: str = None + + obj = ComplexEntityDescription3B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription3B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + with pytest.raises(TypeError): + + @dataclasses.dataclass(frozen=True) + class ComplexEntityDescription3C(MyMixin3, entity.EntityDescription): + extra: str = None + + with pytest.raises(TypeError): + + @dataclasses.dataclass(frozen=True) + class ComplexEntityDescription3D(entity.EntityDescription, MyMixin3): + extra: str = None + + @dataclasses.dataclass(kw_only=True) + class ComplexEntityDescription4A(MyMixin4, entity.EntityDescription): + extra: str = None + + obj = ComplexEntityDescription4A( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription4A( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + @dataclasses.dataclass(kw_only=True) + class ComplexEntityDescription4B(entity.EntityDescription, MyMixin4): + extra: str = None + + obj = ComplexEntityDescription4B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert obj == snapshot + assert obj == ComplexEntityDescription4B( + key="blah", extra="foo", mixin="mixin", name="name" + ) + assert repr(obj) == snapshot + + with pytest.raises(TypeError): + + @dataclasses.dataclass + class ComplexEntityDescription4C(MyMixin4, entity.EntityDescription): + extra: str = None + + with pytest.raises(TypeError): + + @dataclasses.dataclass + class ComplexEntityDescription4D(entity.EntityDescription, MyMixin4): + extra: str = None + # Try inheriting with custom init @dataclasses.dataclass class CustomInitEntityDescription(entity.EntityDescription):