Add code-reuse to ensure unique entity ids
This commit is contained in:
parent
84578c9894
commit
82357b421f
4 changed files with 31 additions and 20 deletions
|
@ -17,7 +17,6 @@ SERVICE_YOUTUBE_VIDEO = 'play_youtube_video'
|
||||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||||
STATE_NO_APP = 'no_app'
|
STATE_NO_APP = 'no_app'
|
||||||
|
|
||||||
ATTR_FRIENDLY_NAME = 'friendly_name'
|
|
||||||
ATTR_HOST = 'host'
|
ATTR_HOST = 'host'
|
||||||
ATTR_STATE = 'state'
|
ATTR_STATE = 'state'
|
||||||
ATTR_OPTIONS = 'options'
|
ATTR_OPTIONS = 'options'
|
||||||
|
@ -100,13 +99,14 @@ def setup(bus, statemachine):
|
||||||
|
|
||||||
casts = {}
|
casts = {}
|
||||||
|
|
||||||
eid_form = ENTITY_ID_FORMAT.format
|
|
||||||
|
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
try:
|
try:
|
||||||
cast = pychromecast.PyChromecast(host)
|
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
|
casts[entity_id] = cast
|
||||||
|
|
||||||
|
@ -124,7 +124,8 @@ def setup(bus, statemachine):
|
||||||
status = chromecast.app
|
status = chromecast.app
|
||||||
|
|
||||||
state_attr = {ATTR_HOST: chromecast.host,
|
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']:
|
if status and status.app_id != pychromecast.APP_ID['HOME']:
|
||||||
state = status.app_id
|
state = status.app_id
|
||||||
|
|
|
@ -14,7 +14,7 @@ import homeassistant as ha
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
from homeassistant.components import (group, STATE_ON, STATE_OFF,
|
from homeassistant.components import (group, STATE_ON, STATE_OFF,
|
||||||
SERVICE_TURN_ON, SERVICE_TURN_OFF,
|
SERVICE_TURN_ON, SERVICE_TURN_OFF,
|
||||||
ATTR_ENTITY_ID)
|
ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME)
|
||||||
|
|
||||||
|
|
||||||
DOMAIN = "light"
|
DOMAIN = "light"
|
||||||
|
@ -93,30 +93,24 @@ def setup(bus, statemachine, light_control):
|
||||||
|
|
||||||
def _update_light_state(light_id, light_state):
|
def _update_light_state(light_id, light_state):
|
||||||
""" Update statemachine based on the LightState passed in. """
|
""" Update statemachine based on the LightState passed in. """
|
||||||
|
name = light_control.get_name(light_id) or "Unknown Light"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
entity_id = light_to_ent[light_id]
|
entity_id = light_to_ent[light_id]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# We have not seen this light before, set it up
|
# We have not seen this light before, set it up
|
||||||
|
|
||||||
# Get name and create entity id
|
# Create entity id
|
||||||
name = light_control.get_name(light_id) or "Unknown Light"
|
|
||||||
|
|
||||||
logger.info(u"Found new light {}".format(name))
|
logger.info(u"Found new light {}".format(name))
|
||||||
|
|
||||||
entity_id = ENTITY_ID_FORMAT.format(util.slugify(name))
|
entity_id = util.ensure_unique_string(
|
||||||
|
ENTITY_ID_FORMAT.format(util.slugify(name)),
|
||||||
# Ensure unique entity id
|
ent_to_light.keys())
|
||||||
tries = 1
|
|
||||||
while entity_id in ent_to_light:
|
|
||||||
tries += 1
|
|
||||||
|
|
||||||
entity_id = ENTITY_ID_FORMAT.format(
|
|
||||||
util.slugify("{} {}".format(name, tries)))
|
|
||||||
|
|
||||||
ent_to_light[entity_id] = light_id
|
ent_to_light[entity_id] = light_id
|
||||||
light_to_ent[light_id] = entity_id
|
light_to_ent[light_id] = entity_id
|
||||||
|
|
||||||
state_attr = {}
|
state_attr = {ATTR_FRIENDLY_NAME: name}
|
||||||
|
|
||||||
if light_state.on:
|
if light_state.on:
|
||||||
state = STATE_ON
|
state = STATE_ON
|
||||||
|
|
|
@ -74,7 +74,9 @@ def setup(bus, statemachine):
|
||||||
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# New device, set it up
|
# 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
|
sno_to_ent[device.serialnumber] = entity_id
|
||||||
ent_to_dev[entity_id] = device
|
ent_to_dev[entity_id] = device
|
||||||
|
|
|
@ -119,6 +119,20 @@ def convert(value, to_type, default=None):
|
||||||
return default
|
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
|
# Reason why I decided to roll my own ThreadPool instead of using
|
||||||
# multiprocessing.dummy.pool or even better, use multiprocessing.pool and
|
# multiprocessing.dummy.pool or even better, use multiprocessing.pool and
|
||||||
# not be hurt by the GIL in the cpython interpreter:
|
# not be hurt by the GIL in the cpython interpreter:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue