mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Add auto discovery for nanoleaf aurora lights (#14301)
* auto discovery added for nanoleaf aurora lights * changes requested by review * visual indentation * line too long * hide autocreated config
This commit is contained in:
parent
c050eb4100
commit
c8ad9c4daa
@ -83,6 +83,7 @@ SERVICE_HANDLERS = {
|
|||||||
'songpal': ('media_player', 'songpal'),
|
'songpal': ('media_player', 'songpal'),
|
||||||
'kodi': ('media_player', 'kodi'),
|
'kodi': ('media_player', 'kodi'),
|
||||||
'volumio': ('media_player', 'volumio'),
|
'volumio': ('media_player', 'volumio'),
|
||||||
|
'nanoleaf_aurora': ('light', 'nanoleaf_aurora'),
|
||||||
}
|
}
|
||||||
|
|
||||||
OPTIONAL_SERVICE_HANDLERS = {
|
OPTIONAL_SERVICE_HANDLERS = {
|
||||||
|
@ -17,6 +17,7 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.util import color as color_util
|
from homeassistant.util import color as color_util
|
||||||
from homeassistant.util.color import \
|
from homeassistant.util.color import \
|
||||||
color_temperature_mired_to_kelvin as mired_to_kelvin
|
color_temperature_mired_to_kelvin as mired_to_kelvin
|
||||||
|
from homeassistant.util.json import load_json, save_json
|
||||||
|
|
||||||
REQUIREMENTS = ['nanoleaf==0.4.1']
|
REQUIREMENTS = ['nanoleaf==0.4.1']
|
||||||
|
|
||||||
@ -24,6 +25,10 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
DEFAULT_NAME = 'Aurora'
|
DEFAULT_NAME = 'Aurora'
|
||||||
|
|
||||||
|
DATA_NANOLEAF_AURORA = 'nanoleaf_aurora'
|
||||||
|
|
||||||
|
CONFIG_FILE = '.nanoleaf_aurora.conf'
|
||||||
|
|
||||||
ICON = 'mdi:triangle-outline'
|
ICON = 'mdi:triangle-outline'
|
||||||
|
|
||||||
SUPPORT_AURORA = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_EFFECT |
|
SUPPORT_AURORA = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_EFFECT |
|
||||||
@ -39,31 +44,59 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the Nanoleaf Aurora device."""
|
"""Set up the Nanoleaf Aurora device."""
|
||||||
import nanoleaf
|
import nanoleaf
|
||||||
host = config.get(CONF_HOST)
|
import nanoleaf.setup
|
||||||
name = config.get(CONF_NAME)
|
if DATA_NANOLEAF_AURORA not in hass.data:
|
||||||
token = config.get(CONF_TOKEN)
|
hass.data[DATA_NANOLEAF_AURORA] = dict()
|
||||||
|
|
||||||
|
token = ''
|
||||||
|
if discovery_info is not None:
|
||||||
|
host = discovery_info['host']
|
||||||
|
name = discovery_info['hostname']
|
||||||
|
# if device already exists via config, skip discovery setup
|
||||||
|
if host in hass.data[DATA_NANOLEAF_AURORA]:
|
||||||
|
return
|
||||||
|
_LOGGER.info("Discovered a new Aurora: %s", discovery_info)
|
||||||
|
conf = load_json(hass.config.path(CONFIG_FILE))
|
||||||
|
if conf.get(host, {}).get('token'):
|
||||||
|
token = conf[host]['token']
|
||||||
|
else:
|
||||||
|
host = config[CONF_HOST]
|
||||||
|
name = config[CONF_NAME]
|
||||||
|
token = config[CONF_TOKEN]
|
||||||
|
|
||||||
|
if not token:
|
||||||
|
token = nanoleaf.setup.generate_auth_token(host)
|
||||||
|
if not token:
|
||||||
|
_LOGGER.error("Could not generate the auth token, did you press "
|
||||||
|
"and hold the power button on %s"
|
||||||
|
"for 5-7 seconds?", name)
|
||||||
|
return
|
||||||
|
conf = load_json(hass.config.path(CONFIG_FILE))
|
||||||
|
conf[host] = {'token': token}
|
||||||
|
save_json(hass.config.path(CONFIG_FILE), conf)
|
||||||
|
|
||||||
aurora_light = nanoleaf.Aurora(host, token)
|
aurora_light = nanoleaf.Aurora(host, token)
|
||||||
aurora_light.hass_name = name
|
|
||||||
|
|
||||||
if aurora_light.on is None:
|
if aurora_light.on is None:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Could not connect to Nanoleaf Aurora: %s on %s", name, host)
|
"Could not connect to Nanoleaf Aurora: %s on %s", name, host)
|
||||||
return
|
return
|
||||||
|
|
||||||
add_devices([AuroraLight(aurora_light)], True)
|
hass.data[DATA_NANOLEAF_AURORA][host] = aurora_light
|
||||||
|
add_devices([AuroraLight(aurora_light, name)], True)
|
||||||
|
|
||||||
|
|
||||||
class AuroraLight(Light):
|
class AuroraLight(Light):
|
||||||
"""Representation of a Nanoleaf Aurora."""
|
"""Representation of a Nanoleaf Aurora."""
|
||||||
|
|
||||||
def __init__(self, light):
|
def __init__(self, light, name):
|
||||||
"""Initialize an Aurora light."""
|
"""Initialize an Aurora light."""
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
self._color_temp = None
|
self._color_temp = None
|
||||||
self._effect = None
|
self._effect = None
|
||||||
self._effects_list = None
|
self._effects_list = None
|
||||||
self._light = light
|
self._light = light
|
||||||
self._name = light.hass_name
|
self._name = name
|
||||||
self._hs_color = None
|
self._hs_color = None
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user