Change nanoleaf name to configured name instead of hostname (#46407)

* nanoleaf: Key config by device id

Rather than host which is not stable

* nanoleaf: Use pretty name instead of hostname
This commit is contained in:
David Keijser 2021-03-23 10:28:19 +01:00 committed by GitHub
parent 8900b38c7f
commit 95370ac84b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 4 deletions

View File

@ -62,14 +62,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
token = ""
if discovery_info is not None:
host = discovery_info["host"]
name = discovery_info["hostname"]
name = None
device_id = discovery_info["properties"]["id"]
# if device already exists via config, skip discovery setup
if host in hass.data[DATA_NANOLEAF]:
return
_LOGGER.info("Discovered a new Nanoleaf: %s", discovery_info)
conf = load_json(hass.config.path(CONFIG_FILE))
if conf.get(host, {}).get("token"):
token = conf[host]["token"]
if host in conf and device_id not in conf:
conf[device_id] = conf.pop(host)
save_json(hass.config.path(CONFIG_FILE), conf)
token = conf.get(device_id, {}).get("token", "")
else:
host = config[CONF_HOST]
name = config[CONF_NAME]
@ -94,11 +98,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
nanoleaf_light.token = token
try:
nanoleaf_light.available
info = nanoleaf_light.info
except Unavailable:
_LOGGER.error("Could not connect to Nanoleaf Light: %s on %s", name, host)
return
if name is None:
name = info.name
hass.data[DATA_NANOLEAF][host] = nanoleaf_light
add_entities([NanoleafLight(nanoleaf_light, name)], True)