mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 01:07:10 +00:00
refactor legacy; move imports
This commit is contained in:
parent
5fba67f6c3
commit
32003daf3f
@ -14,13 +14,6 @@ from homeassistant.components.light import (Light, ATTR_BRIGHTNESS,
|
|||||||
ATTR_FLASH, FLASH_LONG,
|
ATTR_FLASH, FLASH_LONG,
|
||||||
EFFECT_COLORLOOP, EFFECT_WHITE)
|
EFFECT_COLORLOOP, EFFECT_WHITE)
|
||||||
|
|
||||||
from limitlessled import Color
|
|
||||||
from limitlessled.bridge import Bridge
|
|
||||||
from limitlessled.group.rgbw import RgbwGroup
|
|
||||||
from limitlessled.group.white import WhiteGroup
|
|
||||||
from limitlessled.pipeline import Pipeline
|
|
||||||
from limitlessled.presets import COLORLOOP
|
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
REQUIREMENTS = ['limitlessled==1.0.0']
|
REQUIREMENTS = ['limitlessled==1.0.0']
|
||||||
@ -32,38 +25,43 @@ DEFAULT_LED_TYPE = 'rgbw'
|
|||||||
WHITE = [255, 255, 255]
|
WHITE = [255, 255, 255]
|
||||||
|
|
||||||
|
|
||||||
def legacy_setup(config, add_devices_callback):
|
def rewrite_legacy(config):
|
||||||
""" Perform setup using legacy format. """
|
""" Rewrite legacy configuration to new format. """
|
||||||
bridges = config.get('bridges', [config])
|
bridges = config.get('bridges', [config])
|
||||||
lights = []
|
new_bridges = []
|
||||||
for bridge_conf in bridges:
|
for bridge_conf in bridges:
|
||||||
bridge = Bridge(bridge_conf.get('host'))
|
groups = []
|
||||||
for i in range(1, 5):
|
if 'groups' in bridge_conf:
|
||||||
name_key = 'group_%d_name' % i
|
groups = bridge_conf['groups']
|
||||||
if name_key in bridge_conf:
|
else:
|
||||||
group_type = bridge_conf.get('group_%d_type' % i,
|
_LOGGER.warn("Legacy configuration format detected")
|
||||||
DEFAULT_LED_TYPE)
|
for i in range(1, 5):
|
||||||
group = bridge.add_group(i,
|
name_key = 'group_%d_name' % i
|
||||||
bridge_conf.get(name_key),
|
if name_key in bridge_conf:
|
||||||
group_type)
|
groups.append({
|
||||||
lights.append(LimitlessLEDGroup.factory(group))
|
'number': i,
|
||||||
add_devices_callback(lights)
|
'type': bridge_conf.get('group_%d_type' % i,
|
||||||
|
DEFAULT_LED_TYPE),
|
||||||
|
'name': bridge_conf.get(name_key)
|
||||||
|
})
|
||||||
|
new_bridges.append({
|
||||||
|
'host': bridge_conf.get('host'),
|
||||||
|
'groups': groups
|
||||||
|
})
|
||||||
|
return {'bridges': new_bridges}
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
""" Gets the LimitlessLED lights. """
|
""" Gets the LimitlessLED lights. """
|
||||||
|
from limitlessled.bridge import Bridge
|
||||||
|
|
||||||
# Two legacy configuration formats are supported to
|
# Two legacy configuration formats are supported to
|
||||||
# maintain backwards compatibility.
|
# maintain backwards compatibility.
|
||||||
legacy_setup(config, add_devices_callback)
|
config = rewrite_legacy(config)
|
||||||
|
|
||||||
# Use the expanded configuration format.
|
# Use the expanded configuration format.
|
||||||
if 'bridges' not in config:
|
|
||||||
return
|
|
||||||
lights = []
|
lights = []
|
||||||
for bridge_conf in config.get('bridges'):
|
for bridge_conf in config.get('bridges'):
|
||||||
if 'groups' not in bridge_conf:
|
|
||||||
continue
|
|
||||||
bridge = Bridge(bridge_conf.get('host'),
|
bridge = Bridge(bridge_conf.get('host'),
|
||||||
port=bridge_conf.get('port', DEFAULT_PORT),
|
port=bridge_conf.get('port', DEFAULT_PORT),
|
||||||
version=bridge_conf.get('version', DEFAULT_VERSION))
|
version=bridge_conf.get('version', DEFAULT_VERSION))
|
||||||
@ -82,9 +80,10 @@ def state(new_state):
|
|||||||
"""
|
"""
|
||||||
def decorator(function):
|
def decorator(function):
|
||||||
""" Decorator function. """
|
""" Decorator function. """
|
||||||
# pylint: disable=no-member
|
# pylint: disable=no-member,protected-access
|
||||||
def wrapper(self, **kwargs):
|
def wrapper(self, **kwargs):
|
||||||
""" Wrap a group state change. """
|
""" Wrap a group state change. """
|
||||||
|
from limitlessled.pipeline import Pipeline
|
||||||
pipeline = Pipeline()
|
pipeline = Pipeline()
|
||||||
transition_time = DEFAULT_TRANSITION
|
transition_time = DEFAULT_TRANSITION
|
||||||
# Stop any repeating pipeline.
|
# Stop any repeating pipeline.
|
||||||
@ -100,7 +99,7 @@ def state(new_state):
|
|||||||
# Do group type-specific work.
|
# Do group type-specific work.
|
||||||
function(self, transition_time, pipeline, **kwargs)
|
function(self, transition_time, pipeline, **kwargs)
|
||||||
# Update state.
|
# Update state.
|
||||||
self.on_state = new_state
|
self._is_on = new_state
|
||||||
self.group.enqueue(pipeline)
|
self.group.enqueue(pipeline)
|
||||||
self.update_ha_state()
|
self.update_ha_state()
|
||||||
return wrapper
|
return wrapper
|
||||||
@ -113,12 +112,14 @@ class LimitlessLEDGroup(Light):
|
|||||||
""" Initialize a group. """
|
""" Initialize a group. """
|
||||||
self.group = group
|
self.group = group
|
||||||
self.repeating = False
|
self.repeating = False
|
||||||
self.on_state = False
|
self._is_on = False
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def factory(group):
|
def factory(group):
|
||||||
""" Produce LimitlessLEDGroup objects. """
|
""" Produce LimitlessLEDGroup objects. """
|
||||||
|
from limitlessled.group.rgbw import RgbwGroup
|
||||||
|
from limitlessled.group.white import WhiteGroup
|
||||||
if isinstance(group, WhiteGroup):
|
if isinstance(group, WhiteGroup):
|
||||||
return LimitlessLEDWhiteGroup(group)
|
return LimitlessLEDWhiteGroup(group)
|
||||||
elif isinstance(group, RgbwGroup):
|
elif isinstance(group, RgbwGroup):
|
||||||
@ -140,7 +141,7 @@ class LimitlessLEDGroup(Light):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
""" True if device is on. """
|
""" True if device is on. """
|
||||||
return self.on_state
|
return self._is_on
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def brightness(self):
|
def brightness(self):
|
||||||
@ -208,6 +209,7 @@ class LimitlessLEDRGBWGroup(LimitlessLEDGroup):
|
|||||||
@state(True)
|
@state(True)
|
||||||
def turn_on(self, transition_time, pipeline, **kwargs):
|
def turn_on(self, transition_time, pipeline, **kwargs):
|
||||||
""" Turn on (or adjust property of) a group. """
|
""" Turn on (or adjust property of) a group. """
|
||||||
|
from limitlessled.presets import COLORLOOP
|
||||||
# Check arguments.
|
# Check arguments.
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
@ -270,6 +272,7 @@ def _from_hass_color(color):
|
|||||||
""" Convert Home Assistant RGB list
|
""" Convert Home Assistant RGB list
|
||||||
to Color tuple.
|
to Color tuple.
|
||||||
"""
|
"""
|
||||||
|
from limitlessled import Color
|
||||||
return Color(*tuple(color))
|
return Color(*tuple(color))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user