mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Refactor basic light structure
This commit is contained in:
parent
0f2a50c62f
commit
d3320963c3
@ -53,6 +53,7 @@ import os
|
|||||||
import csv
|
import csv
|
||||||
|
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
|
from homeassistant.helpers.entity import ToggleEntity
|
||||||
|
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -96,6 +97,11 @@ DISCOVERY_PLATFORMS = {
|
|||||||
discovery.services.PHILIPS_HUE: 'hue',
|
discovery.services.PHILIPS_HUE: 'hue',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PROP_TO_ATTR = {
|
||||||
|
'brightness': ATTR_BRIGHTNESS,
|
||||||
|
'color_xy': ATTR_XY_COLOR,
|
||||||
|
}
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -251,7 +257,8 @@ def setup(hass, config):
|
|||||||
light.turn_on(**params)
|
light.turn_on(**params)
|
||||||
|
|
||||||
for light in target_lights:
|
for light in target_lights:
|
||||||
light.update_ha_state(True)
|
if light.should_poll:
|
||||||
|
light.update_ha_state(True)
|
||||||
|
|
||||||
# Listen for light on and light off service calls
|
# Listen for light on and light off service calls
|
||||||
hass.services.register(DOMAIN, SERVICE_TURN_ON,
|
hass.services.register(DOMAIN, SERVICE_TURN_ON,
|
||||||
@ -261,3 +268,41 @@ def setup(hass, config):
|
|||||||
handle_light_service)
|
handle_light_service)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class Light(ToggleEntity):
|
||||||
|
""" Represents a light within Home Assistant. """
|
||||||
|
# pylint: disable=no-self-use
|
||||||
|
|
||||||
|
@property
|
||||||
|
def brightness(self):
|
||||||
|
""" Brightness of this light between 0..255. """
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def color_xy(self):
|
||||||
|
""" XY color value [float, float]. """
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_state_attributes(self):
|
||||||
|
""" Returns device specific state attributes. """
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state_attributes(self):
|
||||||
|
""" Returns optional state attributes. """
|
||||||
|
data = {}
|
||||||
|
|
||||||
|
if self.is_on:
|
||||||
|
for prop, attr in PROP_TO_ATTR.items():
|
||||||
|
value = getattr(self, prop)
|
||||||
|
if value:
|
||||||
|
data[attr] = value
|
||||||
|
|
||||||
|
device_attr = self.device_state_attributes
|
||||||
|
|
||||||
|
if device_attr is not None:
|
||||||
|
data.update(device_attr)
|
||||||
|
|
||||||
|
return data
|
||||||
|
@ -7,9 +7,8 @@ Demo platform that implements lights.
|
|||||||
"""
|
"""
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.components.light import (
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
|
Light, ATTR_BRIGHTNESS, ATTR_XY_COLOR)
|
||||||
from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_XY_COLOR
|
|
||||||
|
|
||||||
|
|
||||||
LIGHT_COLORS = [
|
LIGHT_COLORS = [
|
||||||
@ -22,16 +21,16 @@ LIGHT_COLORS = [
|
|||||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
""" Find and return demo lights. """
|
""" Find and return demo lights. """
|
||||||
add_devices_callback([
|
add_devices_callback([
|
||||||
DemoLight("Bed Light", STATE_OFF),
|
DemoLight("Bed Light", False),
|
||||||
DemoLight("Ceiling", STATE_ON),
|
DemoLight("Ceiling", True),
|
||||||
DemoLight("Kitchen", STATE_ON)
|
DemoLight("Kitchen", True)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
class DemoLight(ToggleEntity):
|
class DemoLight(Light):
|
||||||
""" Provides a demo switch. """
|
""" Provides a demo switch. """
|
||||||
def __init__(self, name, state, xy=None, brightness=180):
|
def __init__(self, name, state, xy=None, brightness=180):
|
||||||
self._name = name or DEVICE_DEFAULT_NAME
|
self._name = name
|
||||||
self._state = state
|
self._state = state
|
||||||
self._xy = xy or random.choice(LIGHT_COLORS)
|
self._xy = xy or random.choice(LIGHT_COLORS)
|
||||||
self._brightness = brightness
|
self._brightness = brightness
|
||||||
@ -47,27 +46,23 @@ class DemoLight(ToggleEntity):
|
|||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def brightness(self):
|
||||||
""" Returns the name of the device if any. """
|
""" Brightness of this light between 0..255. """
|
||||||
return self._state
|
return self._brightness
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def color_xy(self):
|
||||||
""" Returns optional state attributes. """
|
""" XY color value. """
|
||||||
if self.is_on:
|
return self._xy
|
||||||
return {
|
|
||||||
ATTR_BRIGHTNESS: self._brightness,
|
|
||||||
ATTR_XY_COLOR: self._xy,
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
""" True if device is on. """
|
""" True if device is on. """
|
||||||
return self._state == STATE_ON
|
return self._state
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the device on. """
|
""" Turn the device on. """
|
||||||
self._state = STATE_ON
|
self._state = True
|
||||||
|
|
||||||
if ATTR_XY_COLOR in kwargs:
|
if ATTR_XY_COLOR in kwargs:
|
||||||
self._xy = kwargs[ATTR_XY_COLOR]
|
self._xy = kwargs[ATTR_XY_COLOR]
|
||||||
@ -75,6 +70,9 @@ class DemoLight(ToggleEntity):
|
|||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
|
|
||||||
|
self.update_ha_state()
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turn the device off. """
|
""" Turn the device off. """
|
||||||
self._state = STATE_OFF
|
self._state = False
|
||||||
|
self.update_ha_state()
|
||||||
|
@ -6,10 +6,9 @@ from urllib.parse import urlparse
|
|||||||
|
|
||||||
from homeassistant.loader import get_component
|
from homeassistant.loader import get_component
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.const import CONF_HOST, DEVICE_DEFAULT_NAME
|
||||||
from homeassistant.const import CONF_HOST
|
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS, ATTR_XY_COLOR, ATTR_TRANSITION,
|
Light, ATTR_BRIGHTNESS, ATTR_XY_COLOR, ATTR_TRANSITION,
|
||||||
ATTR_FLASH, FLASH_LONG, FLASH_SHORT)
|
ATTR_FLASH, FLASH_LONG, FLASH_SHORT)
|
||||||
|
|
||||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||||
@ -131,7 +130,7 @@ def request_configuration(host, hass, add_devices_callback):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class HueLight(ToggleEntity):
|
class HueLight(Light):
|
||||||
""" Represents a Hue light """
|
""" Represents a Hue light """
|
||||||
|
|
||||||
def __init__(self, light_id, info, bridge, update_lights):
|
def __init__(self, light_id, info, bridge, update_lights):
|
||||||
@ -149,19 +148,17 @@ class HueLight(ToggleEntity):
|
|||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
""" Get the mame of the Hue light. """
|
""" Get the mame of the Hue light. """
|
||||||
return self.info.get('name', 'No name')
|
return self.info.get('name', DEVICE_DEFAULT_NAME)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def brightness(self):
|
||||||
""" Returns optional state attributes. """
|
""" Brightness of this light between 0..255. """
|
||||||
attr = {}
|
return self.info['state']['bri']
|
||||||
|
|
||||||
if self.is_on:
|
@property
|
||||||
attr[ATTR_BRIGHTNESS] = self.info['state']['bri']
|
def color_xy(self):
|
||||||
if 'xy' in self.info['state']:
|
""" XY color value. """
|
||||||
attr[ATTR_XY_COLOR] = self.info['state']['xy']
|
return self.info['state'].get('xy')
|
||||||
|
|
||||||
return attr
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
|
@ -23,9 +23,8 @@ light:
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.const import DEVICE_DEFAULT_NAME
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
|
from homeassistant.components.light import Light, ATTR_BRIGHTNESS
|
||||||
from homeassistant.components.light import ATTR_BRIGHTNESS
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -43,18 +42,12 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||||||
lights = []
|
lights = []
|
||||||
for i in range(1, 5):
|
for i in range(1, 5):
|
||||||
if 'group_%d_name' % (i) in config:
|
if 'group_%d_name' % (i) in config:
|
||||||
lights.append(
|
lights.append(LimitlessLED(led, i, config['group_%d_name' % (i)]))
|
||||||
LimitlessLED(
|
|
||||||
led,
|
|
||||||
i,
|
|
||||||
config['group_%d_name' % (i)]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
add_devices_callback(lights)
|
add_devices_callback(lights)
|
||||||
|
|
||||||
|
|
||||||
class LimitlessLED(ToggleEntity):
|
class LimitlessLED(Light):
|
||||||
""" Represents a LimitlessLED light """
|
""" Represents a LimitlessLED light """
|
||||||
|
|
||||||
def __init__(self, led, group, name):
|
def __init__(self, led, group, name):
|
||||||
@ -65,7 +58,7 @@ class LimitlessLED(ToggleEntity):
|
|||||||
self.led.off(self.group)
|
self.led.off(self.group)
|
||||||
|
|
||||||
self._name = name or DEVICE_DEFAULT_NAME
|
self._name = name or DEVICE_DEFAULT_NAME
|
||||||
self._state = STATE_OFF
|
self._state = False
|
||||||
self._brightness = 100
|
self._brightness = 100
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -79,33 +72,26 @@ class LimitlessLED(ToggleEntity):
|
|||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def brightness(self):
|
||||||
""" Returns the name of the device if any. """
|
return self._brightness
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state_attributes(self):
|
|
||||||
""" Returns optional state attributes. """
|
|
||||||
if self.is_on:
|
|
||||||
return {
|
|
||||||
ATTR_BRIGHTNESS: self._brightness,
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
""" True if device is on. """
|
""" True if device is on. """
|
||||||
return self._state == STATE_ON
|
return self._state
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the device on. """
|
""" Turn the device on. """
|
||||||
self._state = STATE_ON
|
self._state = True
|
||||||
|
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
|
|
||||||
self.led.set_brightness(self._brightness, self.group)
|
self.led.set_brightness(self._brightness, self.group)
|
||||||
|
self.update_ha_state()
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turn the device off. """
|
""" Turn the device off. """
|
||||||
self._state = STATE_OFF
|
self._state = False
|
||||||
self.led.off(self.group)
|
self.led.off(self.group)
|
||||||
|
self.update_ha_state()
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
""" Support for Tellstick lights. """
|
""" Support for Tellstick lights. """
|
||||||
import logging
|
import logging
|
||||||
# pylint: disable=no-name-in-module, import-error
|
# pylint: disable=no-name-in-module, import-error
|
||||||
from homeassistant.components.light import ATTR_BRIGHTNESS
|
from homeassistant.components.light import Light, ATTR_BRIGHTNESS
|
||||||
from homeassistant.const import ATTR_FRIENDLY_NAME
|
from homeassistant.const import ATTR_FRIENDLY_NAME
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
|
||||||
import tellcore.constants as tellcore_constants
|
import tellcore.constants as tellcore_constants
|
||||||
|
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||||||
add_devices_callback(lights)
|
add_devices_callback(lights)
|
||||||
|
|
||||||
|
|
||||||
class TellstickLight(ToggleEntity):
|
class TellstickLight(Light):
|
||||||
""" Represents a tellstick light """
|
""" Represents a tellstick light """
|
||||||
last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON |
|
last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON |
|
||||||
tellcore_constants.TELLSTICK_TURNOFF |
|
tellcore_constants.TELLSTICK_TURNOFF |
|
||||||
@ -38,7 +37,7 @@ class TellstickLight(ToggleEntity):
|
|||||||
def __init__(self, tellstick):
|
def __init__(self, tellstick):
|
||||||
self.tellstick = tellstick
|
self.tellstick = tellstick
|
||||||
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name}
|
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name}
|
||||||
self.brightness = 0
|
self._brightness = 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -48,34 +47,28 @@ class TellstickLight(ToggleEntity):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
""" True if switch is on. """
|
""" True if switch is on. """
|
||||||
return self.brightness > 0
|
return self._brightness > 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def brightness(self):
|
||||||
|
""" Brightness of this light between 0..255. """
|
||||||
|
return self._brightness
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
""" Turns the switch off. """
|
""" Turns the switch off. """
|
||||||
self.tellstick.turn_off()
|
self.tellstick.turn_off()
|
||||||
self.brightness = 0
|
self._brightness = 0
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
""" Turns the switch on. """
|
""" Turns the switch on. """
|
||||||
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
||||||
|
|
||||||
if brightness is None:
|
if brightness is None:
|
||||||
self.brightness = 255
|
self._brightness = 255
|
||||||
else:
|
else:
|
||||||
self.brightness = brightness
|
self._brightness = brightness
|
||||||
|
|
||||||
self.tellstick.dim(self.brightness)
|
self.tellstick.dim(self._brightness)
|
||||||
|
|
||||||
@property
|
|
||||||
def state_attributes(self):
|
|
||||||
""" Returns optional state attributes. """
|
|
||||||
attr = {
|
|
||||||
ATTR_FRIENDLY_NAME: self.name
|
|
||||||
}
|
|
||||||
|
|
||||||
attr[ATTR_BRIGHTNESS] = int(self.brightness)
|
|
||||||
|
|
||||||
return attr
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Update state of the light. """
|
""" Update state of the light. """
|
||||||
@ -83,12 +76,12 @@ class TellstickLight(ToggleEntity):
|
|||||||
self.last_sent_command_mask)
|
self.last_sent_command_mask)
|
||||||
|
|
||||||
if last_command == tellcore_constants.TELLSTICK_TURNON:
|
if last_command == tellcore_constants.TELLSTICK_TURNON:
|
||||||
self.brightness = 255
|
self._brightness = 255
|
||||||
elif last_command == tellcore_constants.TELLSTICK_TURNOFF:
|
elif last_command == tellcore_constants.TELLSTICK_TURNOFF:
|
||||||
self.brightness = 0
|
self._brightness = 0
|
||||||
elif (last_command == tellcore_constants.TELLSTICK_DIM or
|
elif (last_command == tellcore_constants.TELLSTICK_DIM or
|
||||||
last_command == tellcore_constants.TELLSTICK_UP or
|
last_command == tellcore_constants.TELLSTICK_UP or
|
||||||
last_command == tellcore_constants.TELLSTICK_DOWN):
|
last_command == tellcore_constants.TELLSTICK_DOWN):
|
||||||
last_sent_value = self.tellstick.last_sent_value()
|
last_sent_value = self.tellstick.last_sent_value()
|
||||||
if last_sent_value is not None:
|
if last_sent_value is not None:
|
||||||
self.brightness = last_sent_value
|
self._brightness = last_sent_value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user