diff --git a/tests/common.py b/tests/common.py index a9ba4ae86b4fe7a90ff6cce7cb196a8ef7e59e47..19f0aaec44b9ed582d69714d3e569c1dfe1b7df0 100644 --- a/tests/common.py +++ b/tests/common.py @@ -943,49 +943,54 @@ class MockEntity(entity.Entity): self.entity_id = values["entity_id"] @property - def name(self): - """Return the name of the entity.""" - return self._handle("name") + def available(self): + """Return True if entity is available.""" + return self._handle("available") @property - def should_poll(self): - """Return the ste of the polling.""" - return self._handle("should_poll") + def capability_attributes(self): + """Info about capabilities.""" + return self._handle("capability_attributes") @property - def unique_id(self): - """Return the unique ID of the entity.""" - return self._handle("unique_id") + def device_class(self): + """Info how device should be classified.""" + return self._handle("device_class") @property - def state(self): - """Return the state of the entity.""" - return self._handle("state") + def device_info(self): + """Info how it links to a device.""" + return self._handle("device_info") @property - def available(self): - """Return True if entity is available.""" - return self._handle("available") + def entity_category(self): + """Return the entity category.""" + return self._handle("entity_category") @property - def device_info(self): - """Info how it links to a device.""" - return self._handle("device_info") + def entity_registry_enabled_default(self): + """Return if the entity should be enabled when first added to the entity registry.""" + return self._handle("entity_registry_enabled_default") @property - def device_class(self): - """Info how device should be classified.""" - return self._handle("device_class") + def icon(self): + """Return the suggested icon.""" + return self._handle("icon") @property - def unit_of_measurement(self): - """Info on the units the entity state is in.""" - return self._handle("unit_of_measurement") + def name(self): + """Return the name of the entity.""" + return self._handle("name") @property - def capability_attributes(self): - """Info about capabilities.""" - return self._handle("capability_attributes") + def should_poll(self): + """Return the ste of the polling.""" + return self._handle("should_poll") + + @property + def state(self): + """Return the state of the entity.""" + return self._handle("state") @property def supported_features(self): @@ -993,9 +998,14 @@ class MockEntity(entity.Entity): return self._handle("supported_features") @property - def entity_registry_enabled_default(self): - """Return if the entity should be enabled when first added to the entity registry.""" - return self._handle("entity_registry_enabled_default") + def unique_id(self): + """Return the unique ID of the entity.""" + return self._handle("unique_id") + + @property + def unit_of_measurement(self): + """Info on the units the entity state is in.""" + return self._handle("unit_of_measurement") def _handle(self, attr): """Return attribute value.""" diff --git a/tests/components/config/test_entity_registry.py b/tests/components/config/test_entity_registry.py index 3faff1222d4c979ec842e3da5f29ff17015b63e0..6257037e57ef2b9df67f586e61b3bb4d089ebbd4 100644 --- a/tests/components/config/test_entity_registry.py +++ b/tests/components/config/test_entity_registry.py @@ -100,19 +100,19 @@ async def test_get_entity(hass, client): msg = await client.receive_json() assert msg["result"] == { + "area_id": None, + "capabilities": None, "config_entry_id": None, "device_id": None, - "area_id": None, "disabled_by": None, - "platform": "test_platform", + "entity_category": None, "entity_id": "test_domain.name", - "name": "Hello World", "icon": None, - "original_name": None, + "name": "Hello World", "original_icon": None, - "capabilities": None, + "original_name": None, + "platform": "test_platform", "unique_id": "1234", - "entity_category": None, } await client.send_json( @@ -125,19 +125,19 @@ async def test_get_entity(hass, client): msg = await client.receive_json() assert msg["result"] == { + "area_id": None, + "capabilities": None, "config_entry_id": None, "device_id": None, - "area_id": None, "disabled_by": None, - "platform": "test_platform", + "entity_category": None, "entity_id": "test_domain.no_name", - "name": None, "icon": None, - "original_name": None, + "name": None, "original_icon": None, - "capabilities": None, + "original_name": None, + "platform": "test_platform", "unique_id": "6789", - "entity_category": None, } @@ -181,19 +181,19 @@ async def test_update_entity(hass, client): assert msg["result"] == { "entity_entry": { + "area_id": "mock-area-id", + "capabilities": None, "config_entry_id": None, "device_id": None, - "area_id": "mock-area-id", "disabled_by": None, - "platform": "test_platform", + "entity_category": None, "entity_id": "test_domain.world", - "name": "after update", "icon": "icon:after update", - "original_name": None, + "name": "after update", "original_icon": None, - "capabilities": None, + "original_name": None, + "platform": "test_platform", "unique_id": "1234", - "entity_category": None, } } @@ -230,19 +230,19 @@ async def test_update_entity(hass, client): assert msg["result"] == { "entity_entry": { + "area_id": "mock-area-id", + "capabilities": None, "config_entry_id": None, "device_id": None, - "area_id": "mock-area-id", "disabled_by": None, - "platform": "test_platform", + "entity_category": None, "entity_id": "test_domain.world", - "name": "after update", "icon": "icon:after update", - "original_name": None, + "name": "after update", "original_icon": None, - "capabilities": None, + "original_name": None, + "platform": "test_platform", "unique_id": "1234", - "entity_category": None, }, "reload_delay": 30, } @@ -285,19 +285,19 @@ async def test_update_entity_require_restart(hass, client): assert msg["result"] == { "entity_entry": { + "area_id": None, + "capabilities": None, "config_entry_id": config_entry.entry_id, "device_id": None, - "area_id": None, "disabled_by": None, - "platform": "test_platform", + "entity_category": None, "entity_id": "test_domain.world", - "name": None, "icon": None, - "original_name": None, + "name": None, "original_icon": None, - "capabilities": None, + "original_name": None, + "platform": "test_platform", "unique_id": "1234", - "entity_category": None, }, "require_restart": True, } @@ -387,19 +387,19 @@ async def test_update_entity_no_changes(hass, client): assert msg["result"] == { "entity_entry": { + "area_id": None, + "capabilities": None, "config_entry_id": None, "device_id": None, - "area_id": None, "disabled_by": None, - "platform": "test_platform", + "entity_category": None, "entity_id": "test_domain.world", - "name": "name of entity", "icon": None, - "original_name": None, + "name": "name of entity", "original_icon": None, - "capabilities": None, + "original_name": None, + "platform": "test_platform", "unique_id": "1234", - "entity_category": None, } } @@ -468,19 +468,19 @@ async def test_update_entity_id(hass, client): assert msg["result"] == { "entity_entry": { + "area_id": None, + "capabilities": None, "config_entry_id": None, "device_id": None, - "area_id": None, "disabled_by": None, - "platform": "test_platform", + "entity_category": None, "entity_id": "test_domain.planet", - "name": None, "icon": None, - "original_name": None, + "name": None, "original_icon": None, - "capabilities": None, + "original_name": None, + "platform": "test_platform", "unique_id": "1234", - "entity_category": None, } } diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index a151a3b7ef3839af5c14282434d0f651e5ac5fae..4e40bcd9882b3d20dd720e63c318ff1b01cf4991 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -1085,10 +1085,13 @@ async def test_entity_info_added_to_entity_registry(hass): component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20)) entity_default = MockEntity( - unique_id="default", capability_attributes={"max": 100}, - supported_features=5, device_class="mock-device-class", + entity_category="config", + icon="nice:icon", + name="best name", + supported_features=5, + unique_id="default", unit_of_measurement=PERCENTAGE, ) @@ -1097,10 +1100,20 @@ async def test_entity_info_added_to_entity_registry(hass): registry = er.async_get(hass) entry_default = registry.async_get_or_create(DOMAIN, DOMAIN, "default") - assert entry_default.capabilities == {"max": 100} - assert entry_default.supported_features == 5 - assert entry_default.device_class == "mock-device-class" - assert entry_default.unit_of_measurement == PERCENTAGE + assert entry_default == er.RegistryEntry( + "test_domain.best_name", + "default", + "test_domain", + capabilities={"max": 100}, + device_class="mock-device-class", + entity_category="config", + icon=None, + name=None, + original_icon="nice:icon", + original_name="best name", + supported_features=5, + unit_of_measurement=PERCENTAGE, + ) async def test_override_restored_entities(hass): diff --git a/tests/helpers/test_entity_registry.py b/tests/helpers/test_entity_registry.py index b07c413179573ee7dc289b19cbe86bf836455782..543cf6c0045af1a53e5e050d4cf320e89aaa46eb 100644 --- a/tests/helpers/test_entity_registry.py +++ b/tests/helpers/test_entity_registry.py @@ -71,54 +71,75 @@ def test_get_or_create_updates_data(registry): "light", "hue", "5678", + area_id="mock-area-id", + capabilities={"max": 100}, config_entry=orig_config_entry, + device_class="mock-device-class", device_id="mock-dev-id", - capabilities={"max": 100}, + disabled_by=er.DISABLED_HASS, + entity_category="config", + original_icon="initial-original_icon", + original_name="initial-original_name", supported_features=5, + unit_of_measurement="initial-unit_of_measurement", + ) + + assert orig_entry == er.RegistryEntry( + "light.hue_5678", + "5678", + "hue", + area_id="mock-area-id", + capabilities={"max": 100}, + config_entry_id=orig_config_entry.entry_id, device_class="mock-device-class", + device_id="mock-dev-id", disabled_by=er.DISABLED_HASS, - unit_of_measurement="initial-unit_of_measurement", - original_name="initial-original_name", + entity_category="config", + icon=None, + name=None, original_icon="initial-original_icon", + original_name="initial-original_name", + supported_features=5, + unit_of_measurement="initial-unit_of_measurement", ) - assert orig_entry.config_entry_id == orig_config_entry.entry_id - assert orig_entry.device_id == "mock-dev-id" - assert orig_entry.capabilities == {"max": 100} - assert orig_entry.supported_features == 5 - assert orig_entry.device_class == "mock-device-class" - assert orig_entry.disabled_by == er.DISABLED_HASS - assert orig_entry.unit_of_measurement == "initial-unit_of_measurement" - assert orig_entry.original_name == "initial-original_name" - assert orig_entry.original_icon == "initial-original_icon" - new_config_entry = MockConfigEntry(domain="light") new_entry = registry.async_get_or_create( "light", "hue", "5678", - config_entry=new_config_entry, - device_id="new-mock-dev-id", + area_id="new-mock-area-id", capabilities={"new-max": 100}, - supported_features=10, + config_entry=new_config_entry, device_class="new-mock-device-class", + device_id="new-mock-dev-id", disabled_by=er.DISABLED_USER, - unit_of_measurement="updated-unit_of_measurement", - original_name="updated-original_name", + entity_category=None, original_icon="updated-original_icon", + original_name="updated-original_name", + supported_features=10, + unit_of_measurement="updated-unit_of_measurement", ) - assert new_entry.config_entry_id == new_config_entry.entry_id - assert new_entry.device_id == "new-mock-dev-id" - assert new_entry.capabilities == {"new-max": 100} - assert new_entry.supported_features == 10 - assert new_entry.device_class == "new-mock-device-class" - assert new_entry.unit_of_measurement == "updated-unit_of_measurement" - assert new_entry.original_name == "updated-original_name" - assert new_entry.original_icon == "updated-original_icon" - # Should not be updated - assert new_entry.disabled_by == er.DISABLED_HASS + assert new_entry == er.RegistryEntry( + "light.hue_5678", + "5678", + "hue", + area_id="new-mock-area-id", + capabilities={"new-max": 100}, + config_entry_id=new_config_entry.entry_id, + device_class="new-mock-device-class", + device_id="new-mock-dev-id", + disabled_by=er.DISABLED_HASS, # Should not be updated + entity_category="config", + icon=None, + name=None, + original_icon="updated-original_icon", + original_name="updated-original_name", + supported_features=10, + unit_of_measurement="updated-unit_of_measurement", + ) def test_get_or_create_suggested_object_id_conflict_register(registry): @@ -158,15 +179,17 @@ async def test_loading_saving_data(hass, registry): "light", "hue", "5678", - device_id="mock-dev-id", area_id="mock-area-id", - config_entry=mock_config, capabilities={"max": 100}, - supported_features=5, + config_entry=mock_config, device_class="mock-device-class", + device_id="mock-dev-id", disabled_by=er.DISABLED_HASS, - original_name="Original Name", + entity_category="config", original_icon="hass:original-icon", + original_name="Original Name", + supported_features=5, + unit_of_measurement="initial-unit_of_measurement", ) orig_entry2 = registry.async_update_entity( orig_entry2.entity_id, name="User Name", icon="hass:user-icon" @@ -187,16 +210,19 @@ async def test_loading_saving_data(hass, registry): assert orig_entry1 == new_entry1 assert orig_entry2 == new_entry2 - assert new_entry2.device_id == "mock-dev-id" assert new_entry2.area_id == "mock-area-id" - assert new_entry2.disabled_by == er.DISABLED_HASS assert new_entry2.capabilities == {"max": 100} - assert new_entry2.supported_features == 5 + assert new_entry2.config_entry_id == mock_config.entry_id assert new_entry2.device_class == "mock-device-class" - assert new_entry2.name == "User Name" + assert new_entry2.device_id == "mock-dev-id" + assert new_entry2.disabled_by == er.DISABLED_HASS + assert new_entry2.entity_category == "config" assert new_entry2.icon == "hass:user-icon" - assert new_entry2.original_name == "Original Name" + assert new_entry2.name == "User Name" assert new_entry2.original_icon == "hass:original-icon" + assert new_entry2.original_name == "Original Name" + assert new_entry2.supported_features == 5 + assert new_entry2.unit_of_measurement == "initial-unit_of_measurement" def test_generate_entity_considers_registered_entities(registry): @@ -354,8 +380,8 @@ async def test_removing_area_id(registry): @pytest.mark.parametrize("load_registries", [False]) -async def test_migration(hass): - """Test migration from old data to new.""" +async def test_migration_yaml_to_json(hass): + """Test migration from old (yaml) data to new.""" mock_config = MockConfigEntry(domain="test-platform", entry_id="test-config-id") old_conf = { @@ -385,8 +411,9 @@ async def test_migration(hass): assert entry.config_entry_id == "test-config-id" +@pytest.mark.parametrize("load_registries", [False]) async def test_loading_invalid_entity_id(hass, hass_storage): - """Test we autofix invalid entity IDs.""" + """Test we skip entities with invalid entity IDs.""" hass_storage[er.STORAGE_KEY] = { "version": er.STORAGE_VERSION_MAJOR, "minor_version": er.STORAGE_VERSION_MINOR, @@ -396,41 +423,49 @@ async def test_loading_invalid_entity_id(hass, hass_storage): "entity_id": "test.invalid__middle", "platform": "super_platform", "unique_id": "id-invalid-middle", - "name": "registry override", + "name": "registry override 1", }, { "entity_id": "test.invalid_end_", "platform": "super_platform", "unique_id": "id-invalid-end", + "name": "registry override 2", }, { "entity_id": "test._invalid_start", "platform": "super_platform", "unique_id": "id-invalid-start", + "name": "registry override 3", }, ] }, } + await er.async_load(hass) registry = er.async_get(hass) + assert len(registry.entities) == 0 entity_invalid_middle = registry.async_get_or_create( "test", "super_platform", "id-invalid-middle" ) assert valid_entity_id(entity_invalid_middle.entity_id) + # Check name to make sure we created a new entity + assert entity_invalid_middle.name is None entity_invalid_end = registry.async_get_or_create( "test", "super_platform", "id-invalid-end" ) assert valid_entity_id(entity_invalid_end.entity_id) + assert entity_invalid_end.name is None entity_invalid_start = registry.async_get_or_create( "test", "super_platform", "id-invalid-start" ) assert valid_entity_id(entity_invalid_start.entity_id) + assert entity_invalid_start.name is None async def test_update_entity_unique_id(registry):