diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py index 2a950c749ac..660e7233b33 100644 --- a/homeassistant/components/script/__init__.py +++ b/homeassistant/components/script/__init__.py @@ -319,6 +319,10 @@ class ScriptEntity(ToggleEntity, RestoreEntity): self.description = cfg[CONF_DESCRIPTION] self.fields = cfg[CONF_FIELDS] + # The object ID of scripts need / are unique already + # they cannot be changed from the UI after creating + self._attr_unique_id = object_id + self.entity_id = ENTITY_ID_FORMAT.format(object_id) self.script = Script( hass, diff --git a/tests/components/script/test_init.py b/tests/components/script/test_init.py index 3bd179286a9..680293b57d7 100644 --- a/tests/components/script/test_init.py +++ b/tests/components/script/test_init.py @@ -25,7 +25,7 @@ from homeassistant.core import ( split_entity_id, ) from homeassistant.exceptions import ServiceNotFound -from homeassistant.helpers import template +from homeassistant.helpers import entity_registry as er, template from homeassistant.helpers.event import async_track_state_change from homeassistant.helpers.script import ( SCRIPT_MODE_CHOICES, @@ -205,7 +205,8 @@ async def test_reload_service(hass, running): await hass.async_block_till_done() if running != "same": - assert hass.states.get(ENTITY_ID) is None + state = hass.states.get(ENTITY_ID) + assert state.attributes["restored"] is True assert not hass.services.has_service(script.DOMAIN, "test") assert hass.states.get("script.test2") is not None @@ -482,6 +483,11 @@ async def test_config_basic(hass): assert test_script.name == "Script Name" assert test_script.attributes["icon"] == "mdi:party" + registry = er.async_get(hass) + entry = registry.async_get("script.test_script") + assert entry + assert entry.unique_id == "test_script" + async def test_config_multiple_domains(hass): """Test splitting configuration over multiple domains."""