diff --git a/homeassistant/components/chromecast.py b/homeassistant/components/chromecast.py index 3bfc79d58e8..7ce36384994 100644 --- a/homeassistant/components/chromecast.py +++ b/homeassistant/components/chromecast.py @@ -17,7 +17,6 @@ SERVICE_YOUTUBE_VIDEO = 'play_youtube_video' ENTITY_ID_FORMAT = DOMAIN + '.{}' STATE_NO_APP = 'no_app' -ATTR_FRIENDLY_NAME = 'friendly_name' ATTR_HOST = 'host' ATTR_STATE = 'state' ATTR_OPTIONS = 'options' @@ -100,13 +99,14 @@ def setup(bus, statemachine): casts = {} - eid_form = ENTITY_ID_FORMAT.format - for host in hosts: try: cast = pychromecast.PyChromecast(host) - entity_id = eid_form(util.slugify(cast.device.friendly_name)) + entity_id = util.ensure_unique_string( + ENTITY_ID_FORMAT.format( + util.slugify(cast.device.friendly_name)), + casts.keys()) casts[entity_id] = cast @@ -124,7 +124,8 @@ def setup(bus, statemachine): status = chromecast.app state_attr = {ATTR_HOST: chromecast.host, - ATTR_FRIENDLY_NAME: chromecast.device.friendly_name} + components.ATTR_FRIENDLY_NAME: + chromecast.device.friendly_name} if status and status.app_id != pychromecast.APP_ID['HOME']: state = status.app_id diff --git a/homeassistant/components/light.py b/homeassistant/components/light.py index e1fcb9d9585..5df19e7dbc5 100644 --- a/homeassistant/components/light.py +++ b/homeassistant/components/light.py @@ -14,7 +14,7 @@ import homeassistant as ha import homeassistant.util as util from homeassistant.components import (group, STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, - ATTR_ENTITY_ID) + ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME) DOMAIN = "light" @@ -93,30 +93,24 @@ def setup(bus, statemachine, light_control): def _update_light_state(light_id, light_state): """ Update statemachine based on the LightState passed in. """ + name = light_control.get_name(light_id) or "Unknown Light" + try: entity_id = light_to_ent[light_id] except KeyError: # We have not seen this light before, set it up - # Get name and create entity id - name = light_control.get_name(light_id) or "Unknown Light" - + # Create entity id logger.info(u"Found new light {}".format(name)) - entity_id = ENTITY_ID_FORMAT.format(util.slugify(name)) - - # Ensure unique entity id - tries = 1 - while entity_id in ent_to_light: - tries += 1 - - entity_id = ENTITY_ID_FORMAT.format( - util.slugify("{} {}".format(name, tries))) + entity_id = util.ensure_unique_string( + ENTITY_ID_FORMAT.format(util.slugify(name)), + ent_to_light.keys()) ent_to_light[entity_id] = light_id light_to_ent[light_id] = entity_id - state_attr = {} + state_attr = {ATTR_FRIENDLY_NAME: name} if light_state.on: state = STATE_ON diff --git a/homeassistant/components/wemo.py b/homeassistant/components/wemo.py index 3517943274d..c90f205ab01 100644 --- a/homeassistant/components/wemo.py +++ b/homeassistant/components/wemo.py @@ -74,7 +74,9 @@ def setup(bus, statemachine): except KeyError: # New device, set it up - entity_id = ENTITY_ID_FORMAT.format(util.slugify(device.name)) + entity_id = util.ensure_unique_string( + ENTITY_ID_FORMAT.format(util.slugify(device.name)), + ent_to_dev.keys()) sno_to_ent[device.serialnumber] = entity_id ent_to_dev[entity_id] = device diff --git a/homeassistant/util.py b/homeassistant/util.py index 0d1007ac10c..c29284541e2 100644 --- a/homeassistant/util.py +++ b/homeassistant/util.py @@ -119,6 +119,20 @@ def convert(value, to_type, default=None): return default +def ensure_unique_string(preferred_string, current_strings): + """ Returns a string that is not present in current_strings. + If preferred string exists will append _2, _3, .. """ + string = preferred_string + + tries = 1 + + while preferred_string in current_strings: + tries += 1 + string = "{}_{}".format(preferred_string, tries) + + return string + + # Reason why I decided to roll my own ThreadPool instead of using # multiprocessing.dummy.pool or even better, use multiprocessing.pool and # not be hurt by the GIL in the cpython interpreter: