diff --git a/homeassistant/components/script.py b/homeassistant/components/script.py index 3f892fdfa80..6d5f0ea37a1 100644 --- a/homeassistant/components/script.py +++ b/homeassistant/components/script.py @@ -16,7 +16,7 @@ import threading from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.event import track_point_in_utc_time -from homeassistant.util import split_entity_id +from homeassistant.util import slugify, split_entity_id from homeassistant.const import ( ATTR_ENTITY_ID, EVENT_TIME_CHANGED, STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF) @@ -70,14 +70,17 @@ def setup(hass, config): if script: script.turn_on() - for name, cfg in config[DOMAIN].items(): - if not cfg.get(CONF_SEQUENCE): - _LOGGER.warn("Missing key 'sequence' for script %s", name) + for object_id, cfg in config[DOMAIN].items(): + if object_id != slugify(object_id): + _LOGGER.warn("Found invalid key for script: %s. Use %s instead.", + object_id, slugify(object_id)) continue - alias = cfg.get(CONF_ALIAS, name) - script = Script(hass, alias, cfg[CONF_SEQUENCE]) + if not cfg.get(CONF_SEQUENCE): + _LOGGER.warn("Missing key 'sequence' for script %s", object_id) + continue + alias = cfg.get(CONF_ALIAS, object_id) + script = Script(hass, object_id, alias, cfg[CONF_SEQUENCE]) component.add_entities((script,)) - _, object_id = split_entity_id(script.entity_id) hass.services.register(DOMAIN, object_id, service_handler) def turn_on_service(service): @@ -100,8 +103,10 @@ def setup(hass, config): class Script(ToggleEntity): """ Represents a script. """ - def __init__(self, hass, name, sequence): + # pylint: disable=too-many-instance-attributes + def __init__(self, hass, object_id, name, sequence): self.hass = hass + self.entity_id = ENTITY_ID_FORMAT.format(object_id) self._name = name self.sequence = sequence self._lock = threading.Lock() diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 708bf6e93a9..d3c0514dcad 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -65,8 +65,10 @@ class EntityComponent(object): if entity is not None and entity not in self.entities.values(): entity.hass = self.hass - entity.entity_id = generate_entity_id( - self.entity_id_format, entity.name, self.entities.keys()) + if getattr(entity, 'entity_id', None) is None: + entity.entity_id = generate_entity_id( + self.entity_id_format, entity.name, + self.entities.keys()) self.entities[entity.entity_id] = entity diff --git a/tests/components/test_script.py b/tests/components/test_script.py index d3974661bc6..e4abed18ec9 100644 --- a/tests/components/test_script.py +++ b/tests/components/test_script.py @@ -47,7 +47,18 @@ class TestScript(unittest.TestCase): } })) - self.assertIsNone(self.hass.states.get(ENTITY_ID)) + self.assertEqual(0, len(self.hass.states.entity_ids('script'))) + + def test_setup_with_invalid_object_id(self): + self.assertTrue(script.setup(self.hass, { + 'script': { + 'test hello world': { + 'sequence': [] + } + } + })) + + self.assertEqual(0, len(self.hass.states.entity_ids('script'))) def test_firing_event(self): event = 'test_event' @@ -61,6 +72,7 @@ class TestScript(unittest.TestCase): self.assertTrue(script.setup(self.hass, { 'script': { 'test': { + 'alias': 'Test Script', 'sequence': [{ 'event': event, 'event_data': {