From 95370ac84baa7fb1a0494649c1d51daeaa4690ae Mon Sep 17 00:00:00 2001 From: David Keijser Date: Tue, 23 Mar 2021 10:28:19 +0100 Subject: [PATCH] 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 --- homeassistant/components/nanoleaf/light.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/nanoleaf/light.py b/homeassistant/components/nanoleaf/light.py index 02d8f4751ef..a6f453ce2aa 100644 --- a/homeassistant/components/nanoleaf/light.py +++ b/homeassistant/components/nanoleaf/light.py @@ -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)