2019-04-03 15:40:03 +00:00
|
|
|
"""Provides functionality to interact with lights."""
|
2016-11-30 21:33:38 +00:00
|
|
|
import asyncio
|
2018-01-21 06:35:38 +00:00
|
|
|
import csv
|
2017-01-05 23:16:12 +00:00
|
|
|
from datetime import timedelta
|
2013-12-11 08:07:30 +00:00
|
|
|
import logging
|
2014-03-26 07:08:50 +00:00
|
|
|
import os
|
2019-08-12 03:38:18 +00:00
|
|
|
from typing import Dict, Optional, Tuple
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-31 22:24:06 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-12-14 09:18:36 +00:00
|
|
|
from homeassistant.auth.permissions.const import POLICY_CONTROL
|
2015-09-27 06:17:04 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
SERVICE_TOGGLE,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2019-12-08 17:16:23 +00:00
|
|
|
from homeassistant.exceptions import Unauthorized, UnknownUser
|
2018-01-21 06:35:38 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-11-16 09:22:07 +00:00
|
|
|
from homeassistant.helpers.config_validation import ( # noqa: F401
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA_BASE,
|
2019-12-03 00:23:12 +00:00
|
|
|
make_entity_service_schema,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-06-13 23:42:09 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2015-09-27 06:17:04 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2015-07-07 07:01:17 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "light"
|
2017-01-05 23:16:12 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
# Bitfield of features supported by the light entity
|
|
|
|
SUPPORT_BRIGHTNESS = 1
|
|
|
|
SUPPORT_COLOR_TEMP = 2
|
|
|
|
SUPPORT_EFFECT = 4
|
|
|
|
SUPPORT_FLASH = 8
|
2018-03-18 22:00:29 +00:00
|
|
|
SUPPORT_COLOR = 16
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_TRANSITION = 32
|
2016-09-21 04:26:40 +00:00
|
|
|
SUPPORT_WHITE_VALUE = 128
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# Integer that represents transition time in seconds to make change.
|
2014-03-16 22:00:59 +00:00
|
|
|
ATTR_TRANSITION = "transition"
|
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# Lists holding color values
|
2014-03-16 22:00:59 +00:00
|
|
|
ATTR_RGB_COLOR = "rgb_color"
|
|
|
|
ATTR_XY_COLOR = "xy_color"
|
2018-03-18 22:00:29 +00:00
|
|
|
ATTR_HS_COLOR = "hs_color"
|
2015-10-28 23:12:16 +00:00
|
|
|
ATTR_COLOR_TEMP = "color_temp"
|
2017-05-17 06:00:46 +00:00
|
|
|
ATTR_KELVIN = "kelvin"
|
2017-04-29 22:04:20 +00:00
|
|
|
ATTR_MIN_MIREDS = "min_mireds"
|
|
|
|
ATTR_MAX_MIREDS = "max_mireds"
|
2016-05-17 07:06:55 +00:00
|
|
|
ATTR_COLOR_NAME = "color_name"
|
2016-09-21 04:26:40 +00:00
|
|
|
ATTR_WHITE_VALUE = "white_value"
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
# Brightness of the light, 0..255 or percentage
|
2014-03-16 22:00:59 +00:00
|
|
|
ATTR_BRIGHTNESS = "brightness"
|
2017-05-17 06:00:46 +00:00
|
|
|
ATTR_BRIGHTNESS_PCT = "brightness_pct"
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# String representing a profile (built-in ones or external defined).
|
2014-03-26 07:08:50 +00:00
|
|
|
ATTR_PROFILE = "profile"
|
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# If the light should flash, can be FLASH_SHORT or FLASH_LONG.
|
2014-12-09 07:02:38 +00:00
|
|
|
ATTR_FLASH = "flash"
|
|
|
|
FLASH_SHORT = "short"
|
|
|
|
FLASH_LONG = "long"
|
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
# List of possible effects
|
|
|
|
ATTR_EFFECT_LIST = "effect_list"
|
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# Apply an effect to the light, can be EFFECT_COLORLOOP.
|
2015-07-08 18:26:37 +00:00
|
|
|
ATTR_EFFECT = "effect"
|
|
|
|
EFFECT_COLORLOOP = "colorloop"
|
2015-12-25 03:10:27 +00:00
|
|
|
EFFECT_RANDOM = "random"
|
2015-11-01 15:04:23 +00:00
|
|
|
EFFECT_WHITE = "white"
|
2015-07-08 18:26:37 +00:00
|
|
|
|
2017-06-02 06:05:05 +00:00
|
|
|
COLOR_GROUP = "Color descriptors"
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
LIGHT_PROFILES_FILE = "light_profiles.csv"
|
|
|
|
|
2016-03-31 22:24:06 +00:00
|
|
|
# Service call validation schemas
|
2017-03-09 21:50:30 +00:00
|
|
|
VALID_TRANSITION = vol.All(vol.Coerce(float), vol.Clamp(min=0, max=6553))
|
2016-07-11 19:39:46 +00:00
|
|
|
VALID_BRIGHTNESS = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=255))
|
2017-05-17 06:00:46 +00:00
|
|
|
VALID_BRIGHTNESS_PCT = vol.All(vol.Coerce(float), vol.Range(min=0, max=100))
|
2016-03-31 22:24:06 +00:00
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
LIGHT_TURN_ON_SCHEMA = {
|
|
|
|
vol.Exclusive(ATTR_PROFILE, COLOR_GROUP): cv.string,
|
2019-09-03 07:50:24 +00:00
|
|
|
ATTR_TRANSITION: VALID_TRANSITION,
|
2019-12-03 00:23:12 +00:00
|
|
|
ATTR_BRIGHTNESS: VALID_BRIGHTNESS,
|
|
|
|
ATTR_BRIGHTNESS_PCT: VALID_BRIGHTNESS_PCT,
|
|
|
|
vol.Exclusive(ATTR_COLOR_NAME, COLOR_GROUP): cv.string,
|
|
|
|
vol.Exclusive(ATTR_RGB_COLOR, COLOR_GROUP): vol.All(
|
|
|
|
vol.ExactSequence((cv.byte, cv.byte, cv.byte)), vol.Coerce(tuple)
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_XY_COLOR, COLOR_GROUP): vol.All(
|
|
|
|
vol.ExactSequence((cv.small_float, cv.small_float)), vol.Coerce(tuple)
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_HS_COLOR, COLOR_GROUP): vol.All(
|
|
|
|
vol.ExactSequence(
|
|
|
|
(
|
|
|
|
vol.All(vol.Coerce(float), vol.Range(min=0, max=360)),
|
|
|
|
vol.All(vol.Coerce(float), vol.Range(min=0, max=100)),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
vol.Coerce(tuple),
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_COLOR_TEMP, COLOR_GROUP): vol.All(
|
|
|
|
vol.Coerce(int), vol.Range(min=1)
|
|
|
|
),
|
|
|
|
vol.Exclusive(ATTR_KELVIN, COLOR_GROUP): vol.All(vol.Coerce(int), vol.Range(min=0)),
|
|
|
|
ATTR_WHITE_VALUE: vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
|
2019-09-03 07:50:24 +00:00
|
|
|
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
|
2019-12-03 00:23:12 +00:00
|
|
|
ATTR_EFFECT: cv.string,
|
2019-09-03 07:50:24 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 22:24:06 +00:00
|
|
|
|
2016-04-01 03:19:59 +00:00
|
|
|
PROFILE_SCHEMA = vol.Schema(
|
|
|
|
vol.ExactSequence((str, cv.small_float, cv.small_float, cv.byte))
|
|
|
|
)
|
|
|
|
|
2014-11-09 23:12:23 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2020-01-07 16:30:53 +00:00
|
|
|
def is_on(hass, entity_id):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return if the lights are on based on the statemachine."""
|
2014-04-24 07:40:45 +00:00
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
def preprocess_turn_on_alternatives(params):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Process extra data for turn light on request."""
|
2017-05-17 06:00:46 +00:00
|
|
|
profile = Profiles.get(params.pop(ATTR_PROFILE, None))
|
|
|
|
if profile is not None:
|
|
|
|
params.setdefault(ATTR_XY_COLOR, profile[:2])
|
|
|
|
params.setdefault(ATTR_BRIGHTNESS, profile[2])
|
|
|
|
|
|
|
|
color_name = params.pop(ATTR_COLOR_NAME, None)
|
|
|
|
if color_name is not None:
|
2018-02-28 02:02:21 +00:00
|
|
|
try:
|
|
|
|
params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name)
|
|
|
|
except ValueError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Got unknown color %s, falling back to white", color_name)
|
2018-02-28 02:02:21 +00:00
|
|
|
params[ATTR_RGB_COLOR] = (255, 255, 255)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
|
|
|
kelvin = params.pop(ATTR_KELVIN, None)
|
|
|
|
if kelvin is not None:
|
|
|
|
mired = color_util.color_temperature_kelvin_to_mired(kelvin)
|
2017-05-18 02:20:59 +00:00
|
|
|
params[ATTR_COLOR_TEMP] = int(mired)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
|
|
|
brightness_pct = params.pop(ATTR_BRIGHTNESS_PCT, None)
|
|
|
|
if brightness_pct is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
params[ATTR_BRIGHTNESS] = int(255 * brightness_pct / 100)
|
2017-05-17 06:00:46 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
xy_color = params.pop(ATTR_XY_COLOR, None)
|
|
|
|
if xy_color is not None:
|
|
|
|
params[ATTR_HS_COLOR] = color_util.color_xy_to_hs(*xy_color)
|
|
|
|
|
|
|
|
rgb_color = params.pop(ATTR_RGB_COLOR, None)
|
|
|
|
if rgb_color is not None:
|
|
|
|
params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
|
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
|
2019-03-31 04:04:32 +00:00
|
|
|
def preprocess_turn_off(params):
|
|
|
|
"""Process data for turning light off if brightness is 0."""
|
|
|
|
if ATTR_BRIGHTNESS in params and params[ATTR_BRIGHTNESS] == 0:
|
|
|
|
# Zero brightness: Light will be turned off
|
2019-07-31 19:25:30 +00:00
|
|
|
params = {k: v for k, v in params.items() if k in [ATTR_TRANSITION, ATTR_FLASH]}
|
2019-03-31 04:04:32 +00:00
|
|
|
return (True, params) # Light should be turned off
|
|
|
|
|
|
|
|
return (False, None) # Light should be turned on
|
|
|
|
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_setup(hass, config):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Expose light control via state machine and services."""
|
2018-04-09 14:09:08 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(
|
2020-01-07 16:30:53 +00:00
|
|
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-02-24 18:24:33 +00:00
|
|
|
await component.async_setup(config)
|
2015-03-01 09:35:58 +00:00
|
|
|
|
2016-11-30 21:33:38 +00:00
|
|
|
# load profiles from files
|
2018-02-24 18:24:33 +00:00
|
|
|
profiles_valid = await Profiles.load_profiles(hass)
|
2017-05-17 06:00:46 +00:00
|
|
|
if not profiles_valid:
|
2016-11-30 21:33:38 +00:00
|
|
|
return False
|
2015-08-03 15:42:28 +00:00
|
|
|
|
2018-08-16 12:28:59 +00:00
|
|
|
async def async_handle_light_on_service(service):
|
|
|
|
"""Handle a turn light on service call."""
|
2016-03-31 22:24:06 +00:00
|
|
|
# Get the validated data
|
|
|
|
params = service.data.copy()
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-03-25 03:34:35 +00:00
|
|
|
# Convert the entity ids to valid light ids
|
2019-03-04 17:51:12 +00:00
|
|
|
target_lights = await component.async_extract_from_service(service)
|
2016-03-31 22:24:06 +00:00
|
|
|
params.pop(ATTR_ENTITY_ID, None)
|
2014-11-26 05:28:43 +00:00
|
|
|
|
2018-12-14 09:18:36 +00:00
|
|
|
if service.context.user_id:
|
|
|
|
user = await hass.auth.async_get_user(service.context.user_id)
|
|
|
|
if user is None:
|
|
|
|
raise UnknownUser(context=service.context)
|
|
|
|
|
|
|
|
entity_perms = user.permissions.check_entity
|
|
|
|
|
|
|
|
for light in target_lights:
|
|
|
|
if not entity_perms(light, POLICY_CONTROL):
|
|
|
|
raise Unauthorized(
|
|
|
|
context=service.context,
|
|
|
|
entity_id=light,
|
2019-07-31 19:25:30 +00:00
|
|
|
permission=POLICY_CONTROL,
|
2018-12-14 09:18:36 +00:00
|
|
|
)
|
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
preprocess_turn_on_alternatives(params)
|
2019-03-31 04:04:32 +00:00
|
|
|
turn_lights_off, off_params = preprocess_turn_off(params)
|
2016-05-17 07:06:55 +00:00
|
|
|
|
Switch on/off all lights, and wait for the result (#27078)
* Switch on/off all lights, and wait for the result
Reuses the parallel_updates semaphore.
This is a small crutch which serializes platforms which already do tis
for updates. Platforms which can parallelize everything, this makes it
go faster
* Fix broken unittest
With manual validation, with help from @frenck, we found out that the
assertions are wrong and the test should be failing.
The sequence requested is
OFF
ON
without cancelation, this code should result in:
off,off,off,on,on,on
testable, by adding a `await hass.async_block_till_done()` between the
off and on call.
with cancelation. there should be less off call's so
off,on,on,on
* Adding tests for async_request_call
* Process review feedback
* Switch gather with wait
* :shirt: running black
2019-10-06 15:23:12 +00:00
|
|
|
poll_lights = []
|
|
|
|
change_tasks = []
|
2015-08-03 15:42:28 +00:00
|
|
|
for light in target_lights:
|
2018-08-20 15:39:53 +00:00
|
|
|
light.async_set_context(service.context)
|
|
|
|
|
2018-08-16 12:28:59 +00:00
|
|
|
pars = params
|
2019-03-31 04:04:32 +00:00
|
|
|
off_pars = off_params
|
|
|
|
turn_light_off = turn_lights_off
|
2018-08-16 12:28:59 +00:00
|
|
|
if not pars:
|
|
|
|
pars = params.copy()
|
|
|
|
pars[ATTR_PROFILE] = Profiles.get_default(light.entity_id)
|
|
|
|
preprocess_turn_on_alternatives(pars)
|
2019-03-31 04:04:32 +00:00
|
|
|
turn_light_off, off_pars = preprocess_turn_off(pars)
|
|
|
|
if turn_light_off:
|
Switch on/off all lights, and wait for the result (#27078)
* Switch on/off all lights, and wait for the result
Reuses the parallel_updates semaphore.
This is a small crutch which serializes platforms which already do tis
for updates. Platforms which can parallelize everything, this makes it
go faster
* Fix broken unittest
With manual validation, with help from @frenck, we found out that the
assertions are wrong and the test should be failing.
The sequence requested is
OFF
ON
without cancelation, this code should result in:
off,off,off,on,on,on
testable, by adding a `await hass.async_block_till_done()` between the
off and on call.
with cancelation. there should be less off call's so
off,on,on,on
* Adding tests for async_request_call
* Process review feedback
* Switch gather with wait
* :shirt: running black
2019-10-06 15:23:12 +00:00
|
|
|
task = light.async_request_call(light.async_turn_off(**off_pars))
|
2019-03-31 04:04:32 +00:00
|
|
|
else:
|
Switch on/off all lights, and wait for the result (#27078)
* Switch on/off all lights, and wait for the result
Reuses the parallel_updates semaphore.
This is a small crutch which serializes platforms which already do tis
for updates. Platforms which can parallelize everything, this makes it
go faster
* Fix broken unittest
With manual validation, with help from @frenck, we found out that the
assertions are wrong and the test should be failing.
The sequence requested is
OFF
ON
without cancelation, this code should result in:
off,off,off,on,on,on
testable, by adding a `await hass.async_block_till_done()` between the
off and on call.
with cancelation. there should be less off call's so
off,on,on,on
* Adding tests for async_request_call
* Process review feedback
* Switch gather with wait
* :shirt: running black
2019-10-06 15:23:12 +00:00
|
|
|
task = light.async_request_call(light.async_turn_on(**pars))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
Switch on/off all lights, and wait for the result (#27078)
* Switch on/off all lights, and wait for the result
Reuses the parallel_updates semaphore.
This is a small crutch which serializes platforms which already do tis
for updates. Platforms which can parallelize everything, this makes it
go faster
* Fix broken unittest
With manual validation, with help from @frenck, we found out that the
assertions are wrong and the test should be failing.
The sequence requested is
OFF
ON
without cancelation, this code should result in:
off,off,off,on,on,on
testable, by adding a `await hass.async_block_till_done()` between the
off and on call.
with cancelation. there should be less off call's so
off,on,on,on
* Adding tests for async_request_call
* Process review feedback
* Switch gather with wait
* :shirt: running black
2019-10-06 15:23:12 +00:00
|
|
|
change_tasks.append(task)
|
2018-07-29 00:53:37 +00:00
|
|
|
|
Switch on/off all lights, and wait for the result (#27078)
* Switch on/off all lights, and wait for the result
Reuses the parallel_updates semaphore.
This is a small crutch which serializes platforms which already do tis
for updates. Platforms which can parallelize everything, this makes it
go faster
* Fix broken unittest
With manual validation, with help from @frenck, we found out that the
assertions are wrong and the test should be failing.
The sequence requested is
OFF
ON
without cancelation, this code should result in:
off,off,off,on,on,on
testable, by adding a `await hass.async_block_till_done()` between the
off and on call.
with cancelation. there should be less off call's so
off,on,on,on
* Adding tests for async_request_call
* Process review feedback
* Switch gather with wait
* :shirt: running black
2019-10-06 15:23:12 +00:00
|
|
|
if light.should_poll:
|
|
|
|
poll_lights.append(light)
|
2016-11-30 21:33:38 +00:00
|
|
|
|
Switch on/off all lights, and wait for the result (#27078)
* Switch on/off all lights, and wait for the result
Reuses the parallel_updates semaphore.
This is a small crutch which serializes platforms which already do tis
for updates. Platforms which can parallelize everything, this makes it
go faster
* Fix broken unittest
With manual validation, with help from @frenck, we found out that the
assertions are wrong and the test should be failing.
The sequence requested is
OFF
ON
without cancelation, this code should result in:
off,off,off,on,on,on
testable, by adding a `await hass.async_block_till_done()` between the
off and on call.
with cancelation. there should be less off call's so
off,on,on,on
* Adding tests for async_request_call
* Process review feedback
* Switch gather with wait
* :shirt: running black
2019-10-06 15:23:12 +00:00
|
|
|
if change_tasks:
|
|
|
|
await asyncio.wait(change_tasks)
|
|
|
|
|
|
|
|
if poll_lights:
|
|
|
|
await asyncio.wait(
|
|
|
|
[light.async_update_ha_state(True) for light in poll_lights]
|
|
|
|
)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-07 21:08:21 +00:00
|
|
|
# Listen for light on and light off service calls.
|
2016-11-30 21:33:38 +00:00
|
|
|
hass.services.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
async_handle_light_on_service,
|
2019-12-03 00:23:12 +00:00
|
|
|
schema=cv.make_entity_service_schema(LIGHT_TURN_ON_SCHEMA),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2018-08-16 12:28:59 +00:00
|
|
|
component.async_register_entity_service(
|
2019-12-03 00:23:12 +00:00
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
{
|
|
|
|
ATTR_TRANSITION: VALID_TRANSITION,
|
|
|
|
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
|
|
|
|
},
|
|
|
|
"async_turn_off",
|
2018-08-16 12:28:59 +00:00
|
|
|
)
|
2016-11-30 21:33:38 +00:00
|
|
|
|
2018-08-16 12:28:59 +00:00
|
|
|
component.async_register_entity_service(
|
2019-12-03 00:23:12 +00:00
|
|
|
SERVICE_TOGGLE, LIGHT_TURN_ON_SCHEMA, "async_toggle"
|
2018-08-16 12:28:59 +00:00
|
|
|
)
|
2016-01-16 15:45:05 +00:00
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
return True
|
2015-06-13 23:42:09 +00:00
|
|
|
|
|
|
|
|
2018-04-09 14:09:08 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up a config entry."""
|
2018-04-09 14:09:08 +00:00
|
|
|
return await hass.data[DOMAIN].async_setup_entry(entry)
|
|
|
|
|
|
|
|
|
2018-04-12 12:28:54 +00:00
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.data[DOMAIN].async_unload_entry(entry)
|
|
|
|
|
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
class Profiles:
|
|
|
|
"""Representation of available color profiles."""
|
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
_all: Optional[Dict[str, Tuple[float, float, int]]] = None
|
2017-05-17 06:00:46 +00:00
|
|
|
|
|
|
|
@classmethod
|
2018-02-24 18:24:33 +00:00
|
|
|
async def load_profiles(cls, hass):
|
2017-05-17 06:00:46 +00:00
|
|
|
"""Load and cache profiles."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-05-17 06:00:46 +00:00
|
|
|
def load_profile_data(hass):
|
|
|
|
"""Load built-in profiles and custom profiles."""
|
2019-07-31 19:25:30 +00:00
|
|
|
profile_paths = [
|
|
|
|
os.path.join(os.path.dirname(__file__), LIGHT_PROFILES_FILE),
|
|
|
|
hass.config.path(LIGHT_PROFILES_FILE),
|
|
|
|
]
|
2017-05-17 06:00:46 +00:00
|
|
|
profiles = {}
|
|
|
|
|
|
|
|
for profile_path in profile_paths:
|
|
|
|
if not os.path.isfile(profile_path):
|
|
|
|
continue
|
|
|
|
with open(profile_path) as inp:
|
|
|
|
reader = csv.reader(inp)
|
|
|
|
|
|
|
|
# Skip the header
|
|
|
|
next(reader, None)
|
|
|
|
|
|
|
|
try:
|
|
|
|
for rec in reader:
|
2019-07-31 19:25:30 +00:00
|
|
|
profile, color_x, color_y, brightness = PROFILE_SCHEMA(rec)
|
2017-05-17 06:00:46 +00:00
|
|
|
profiles[profile] = (color_x, color_y, brightness)
|
|
|
|
except vol.MultipleInvalid as ex:
|
|
|
|
_LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Error parsing light profile from %s: %s", profile_path, ex
|
|
|
|
)
|
2017-05-17 06:00:46 +00:00
|
|
|
return None
|
|
|
|
return profiles
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
cls._all = await hass.async_add_job(load_profile_data, hass)
|
2017-05-17 06:00:46 +00:00
|
|
|
return cls._all is not None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls, name):
|
|
|
|
"""Return a named profile."""
|
|
|
|
return cls._all.get(name)
|
2016-11-30 21:33:38 +00:00
|
|
|
|
2018-07-24 18:29:59 +00:00
|
|
|
@classmethod
|
|
|
|
def get_default(cls, entity_id):
|
|
|
|
"""Return the default turn-on profile for the given light."""
|
2018-07-26 06:55:42 +00:00
|
|
|
# pylint: disable=unsupported-membership-test
|
2018-07-24 18:29:59 +00:00
|
|
|
name = entity_id + ".default"
|
|
|
|
if name in cls._all:
|
|
|
|
return name
|
2020-01-07 16:30:53 +00:00
|
|
|
name = "group.all_lights.default"
|
2018-07-24 18:29:59 +00:00
|
|
|
if name in cls._all:
|
|
|
|
return name
|
|
|
|
return None
|
|
|
|
|
2016-11-30 21:33:38 +00:00
|
|
|
|
2015-06-13 23:42:09 +00:00
|
|
|
class Light(ToggleEntity):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Representation of a light."""
|
2015-06-13 23:42:09 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2015-06-13 23:42:09 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hue and saturation color value [float, float]."""
|
2015-11-07 09:25:33 +00:00
|
|
|
return None
|
|
|
|
|
2015-10-27 22:34:49 +00:00
|
|
|
@property
|
2015-10-28 23:12:16 +00:00
|
|
|
def color_temp(self):
|
2016-03-20 02:44:20 +00:00
|
|
|
"""Return the CT color value in mireds."""
|
2015-10-27 22:34:49 +00:00
|
|
|
return None
|
|
|
|
|
2017-04-29 22:04:20 +00:00
|
|
|
@property
|
|
|
|
def min_mireds(self):
|
|
|
|
"""Return the coldest color_temp that this light supports."""
|
|
|
|
# Default to the Philips Hue value that HA has always assumed
|
2018-04-02 07:45:38 +00:00
|
|
|
# https://developers.meethue.com/documentation/core-concepts
|
|
|
|
return 153
|
2017-04-29 22:04:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return the warmest color_temp that this light supports."""
|
|
|
|
# Default to the Philips Hue value that HA has always assumed
|
2018-04-02 07:45:38 +00:00
|
|
|
# https://developers.meethue.com/documentation/core-concepts
|
2017-04-29 22:04:20 +00:00
|
|
|
return 500
|
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
@property
|
|
|
|
def white_value(self):
|
|
|
|
"""Return the white value of this light between 0..255."""
|
|
|
|
return None
|
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect(self):
|
|
|
|
"""Return the current effect."""
|
|
|
|
return None
|
|
|
|
|
2015-06-13 23:42:09 +00:00
|
|
|
@property
|
2019-12-02 19:15:50 +00:00
|
|
|
def capability_attributes(self):
|
|
|
|
"""Return capability attributes."""
|
2015-06-13 23:42:09 +00:00
|
|
|
data = {}
|
2018-07-18 10:18:22 +00:00
|
|
|
supported_features = self.supported_features
|
2015-06-13 23:42:09 +00:00
|
|
|
|
2018-07-18 10:18:22 +00:00
|
|
|
if supported_features & SUPPORT_COLOR_TEMP:
|
2018-03-08 22:39:10 +00:00
|
|
|
data[ATTR_MIN_MIREDS] = self.min_mireds
|
|
|
|
data[ATTR_MAX_MIREDS] = self.max_mireds
|
|
|
|
|
2019-02-19 05:01:26 +00:00
|
|
|
if supported_features & SUPPORT_EFFECT:
|
|
|
|
data[ATTR_EFFECT_LIST] = self.effect_list
|
|
|
|
|
2019-12-02 19:15:50 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
"""Return state attributes."""
|
|
|
|
if not self.is_on:
|
|
|
|
return None
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
supported_features = self.supported_features
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2019-12-02 19:15:50 +00:00
|
|
|
if supported_features & SUPPORT_BRIGHTNESS:
|
|
|
|
data[ATTR_BRIGHTNESS] = self.brightness
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2019-12-02 19:15:50 +00:00
|
|
|
if supported_features & SUPPORT_COLOR_TEMP:
|
|
|
|
data[ATTR_COLOR_TEMP] = self.color_temp
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2019-12-02 19:15:50 +00:00
|
|
|
if supported_features & SUPPORT_COLOR and self.hs_color:
|
|
|
|
# pylint: disable=unsubscriptable-object,not-an-iterable
|
|
|
|
hs_color = self.hs_color
|
|
|
|
data[ATTR_HS_COLOR] = (round(hs_color[0], 3), round(hs_color[1], 3))
|
|
|
|
data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
|
|
|
|
data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)
|
2018-07-18 10:18:22 +00:00
|
|
|
|
2019-12-02 19:15:50 +00:00
|
|
|
if supported_features & SUPPORT_WHITE_VALUE:
|
|
|
|
data[ATTR_WHITE_VALUE] = self.white_value
|
|
|
|
|
|
|
|
if supported_features & SUPPORT_EFFECT:
|
|
|
|
data[ATTR_EFFECT] = self.effect
|
2015-11-07 09:25:33 +00:00
|
|
|
|
2018-07-18 10:18:22 +00:00
|
|
|
return {key: val for key, val in data.items() if val is not None}
|
2016-08-16 06:07:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return 0
|