Add code-reuse to ensure unique entity ids

This commit is contained in:
Paulus Schoutsen 2014-03-23 12:31:24 -07:00
parent 84578c9894
commit 82357b421f
4 changed files with 31 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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: