mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
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:
parent
8900b38c7f
commit
95370ac84b
@ -62,14 +62,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
token = ""
|
token = ""
|
||||||
if discovery_info is not None:
|
if discovery_info is not None:
|
||||||
host = discovery_info["host"]
|
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 device already exists via config, skip discovery setup
|
||||||
if host in hass.data[DATA_NANOLEAF]:
|
if host in hass.data[DATA_NANOLEAF]:
|
||||||
return
|
return
|
||||||
_LOGGER.info("Discovered a new Nanoleaf: %s", discovery_info)
|
_LOGGER.info("Discovered a new Nanoleaf: %s", discovery_info)
|
||||||
conf = load_json(hass.config.path(CONFIG_FILE))
|
conf = load_json(hass.config.path(CONFIG_FILE))
|
||||||
if conf.get(host, {}).get("token"):
|
if host in conf and device_id not in conf:
|
||||||
token = conf[host]["token"]
|
conf[device_id] = conf.pop(host)
|
||||||
|
save_json(hass.config.path(CONFIG_FILE), conf)
|
||||||
|
token = conf.get(device_id, {}).get("token", "")
|
||||||
else:
|
else:
|
||||||
host = config[CONF_HOST]
|
host = config[CONF_HOST]
|
||||||
name = config[CONF_NAME]
|
name = config[CONF_NAME]
|
||||||
@ -94,11 +98,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
nanoleaf_light.token = token
|
nanoleaf_light.token = token
|
||||||
|
|
||||||
try:
|
try:
|
||||||
nanoleaf_light.available
|
info = nanoleaf_light.info
|
||||||
except Unavailable:
|
except Unavailable:
|
||||||
_LOGGER.error("Could not connect to Nanoleaf Light: %s on %s", name, host)
|
_LOGGER.error("Could not connect to Nanoleaf Light: %s on %s", name, host)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if name is None:
|
||||||
|
name = info.name
|
||||||
|
|
||||||
hass.data[DATA_NANOLEAF][host] = nanoleaf_light
|
hass.data[DATA_NANOLEAF][host] = nanoleaf_light
|
||||||
add_entities([NanoleafLight(nanoleaf_light, name)], True)
|
add_entities([NanoleafLight(nanoleaf_light, name)], True)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user