Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -8,28 +8,34 @@ import os
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.auth.permissions.const import POLICY_CONTROL
|
||||
from homeassistant.components.group import \
|
||||
ENTITY_ID_FORMAT as GROUP_ENTITY_ID_FORMAT
|
||||
from homeassistant.components.group import ENTITY_ID_FORMAT as GROUP_ENTITY_ID_FORMAT
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON,
|
||||
STATE_ON)
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.exceptions import UnknownUser, Unauthorized
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.config_validation import ( # noqa
|
||||
PLATFORM_SCHEMA, PLATFORM_SCHEMA_BASE, ENTITY_SERVICE_SCHEMA)
|
||||
PLATFORM_SCHEMA,
|
||||
PLATFORM_SCHEMA_BASE,
|
||||
ENTITY_SERVICE_SCHEMA,
|
||||
)
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers import intent
|
||||
from homeassistant.loader import bind_hass
|
||||
import homeassistant.util.color as color_util
|
||||
|
||||
DOMAIN = 'light'
|
||||
DOMAIN = "light"
|
||||
SCAN_INTERVAL = timedelta(seconds=30)
|
||||
|
||||
GROUP_NAME_ALL_LIGHTS = 'all lights'
|
||||
ENTITY_ID_ALL_LIGHTS = GROUP_ENTITY_ID_FORMAT.format('all_lights')
|
||||
GROUP_NAME_ALL_LIGHTS = "all lights"
|
||||
ENTITY_ID_ALL_LIGHTS = GROUP_ENTITY_ID_FORMAT.format("all_lights")
|
||||
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
||||
|
||||
# Bitfield of features supported by the light entity
|
||||
SUPPORT_BRIGHTNESS = 1
|
||||
@@ -84,36 +90,43 @@ VALID_TRANSITION = vol.All(vol.Coerce(float), vol.Clamp(min=0, max=6553))
|
||||
VALID_BRIGHTNESS = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=255))
|
||||
VALID_BRIGHTNESS_PCT = vol.All(vol.Coerce(float), vol.Range(min=0, max=100))
|
||||
|
||||
LIGHT_TURN_ON_SCHEMA = ENTITY_SERVICE_SCHEMA.extend({
|
||||
vol.Exclusive(ATTR_PROFILE, COLOR_GROUP): cv.string,
|
||||
ATTR_TRANSITION: VALID_TRANSITION,
|
||||
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)),
|
||||
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
|
||||
ATTR_EFFECT: cv.string,
|
||||
})
|
||||
LIGHT_TURN_ON_SCHEMA = ENTITY_SERVICE_SCHEMA.extend(
|
||||
{
|
||||
vol.Exclusive(ATTR_PROFILE, COLOR_GROUP): cv.string,
|
||||
ATTR_TRANSITION: VALID_TRANSITION,
|
||||
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)),
|
||||
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
|
||||
ATTR_EFFECT: cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
LIGHT_TURN_OFF_SCHEMA = ENTITY_SERVICE_SCHEMA.extend({
|
||||
ATTR_TRANSITION: VALID_TRANSITION,
|
||||
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
|
||||
})
|
||||
LIGHT_TURN_OFF_SCHEMA = ENTITY_SERVICE_SCHEMA.extend(
|
||||
{ATTR_TRANSITION: VALID_TRANSITION, ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG])}
|
||||
)
|
||||
|
||||
LIGHT_TOGGLE_SCHEMA = LIGHT_TURN_ON_SCHEMA
|
||||
|
||||
@@ -121,7 +134,7 @@ PROFILE_SCHEMA = vol.Schema(
|
||||
vol.ExactSequence((str, cv.small_float, cv.small_float, cv.byte))
|
||||
)
|
||||
|
||||
INTENT_SET = 'HassLightSet'
|
||||
INTENT_SET = "HassLightSet"
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -145,8 +158,7 @@ def preprocess_turn_on_alternatives(params):
|
||||
try:
|
||||
params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name)
|
||||
except ValueError:
|
||||
_LOGGER.warning('Got unknown color %s, falling back to white',
|
||||
color_name)
|
||||
_LOGGER.warning("Got unknown color %s, falling back to white", color_name)
|
||||
params[ATTR_RGB_COLOR] = (255, 255, 255)
|
||||
|
||||
kelvin = params.pop(ATTR_KELVIN, None)
|
||||
@@ -156,7 +168,7 @@ def preprocess_turn_on_alternatives(params):
|
||||
|
||||
brightness_pct = params.pop(ATTR_BRIGHTNESS_PCT, None)
|
||||
if brightness_pct is not None:
|
||||
params[ATTR_BRIGHTNESS] = int(255 * brightness_pct/100)
|
||||
params[ATTR_BRIGHTNESS] = int(255 * brightness_pct / 100)
|
||||
|
||||
xy_color = params.pop(ATTR_XY_COLOR, None)
|
||||
if xy_color is not None:
|
||||
@@ -171,8 +183,7 @@ 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
|
||||
params = {k: v for k, v in params.items() if k in [ATTR_TRANSITION,
|
||||
ATTR_FLASH]}
|
||||
params = {k: v for k, v in params.items() if k in [ATTR_TRANSITION, ATTR_FLASH]}
|
||||
return (True, params) # Light should be turned off
|
||||
|
||||
return (False, None) # Light should be turned on
|
||||
@@ -183,9 +194,9 @@ class SetIntentHandler(intent.IntentHandler):
|
||||
|
||||
intent_type = INTENT_SET
|
||||
slot_schema = {
|
||||
vol.Required('name'): cv.string,
|
||||
vol.Optional('color'): color_util.color_name_to_rgb,
|
||||
vol.Optional('brightness'): vol.All(vol.Coerce(int), vol.Range(0, 100))
|
||||
vol.Required("name"): cv.string,
|
||||
vol.Optional("color"): color_util.color_name_to_rgb,
|
||||
vol.Optional("brightness"): vol.All(vol.Coerce(int), vol.Range(0, 100)),
|
||||
}
|
||||
|
||||
async def async_handle(self, intent_obj):
|
||||
@@ -193,47 +204,43 @@ class SetIntentHandler(intent.IntentHandler):
|
||||
hass = intent_obj.hass
|
||||
slots = self.async_validate_slots(intent_obj.slots)
|
||||
state = hass.helpers.intent.async_match_state(
|
||||
slots['name']['value'],
|
||||
[state for state in hass.states.async_all()
|
||||
if state.domain == DOMAIN])
|
||||
slots["name"]["value"],
|
||||
[state for state in hass.states.async_all() if state.domain == DOMAIN],
|
||||
)
|
||||
|
||||
service_data = {
|
||||
ATTR_ENTITY_ID: state.entity_id,
|
||||
}
|
||||
service_data = {ATTR_ENTITY_ID: state.entity_id}
|
||||
speech_parts = []
|
||||
|
||||
if 'color' in slots:
|
||||
intent.async_test_feature(
|
||||
state, SUPPORT_COLOR, 'changing colors')
|
||||
service_data[ATTR_RGB_COLOR] = slots['color']['value']
|
||||
if "color" in slots:
|
||||
intent.async_test_feature(state, SUPPORT_COLOR, "changing colors")
|
||||
service_data[ATTR_RGB_COLOR] = slots["color"]["value"]
|
||||
# Use original passed in value of the color because we don't have
|
||||
# human readable names for that internally.
|
||||
speech_parts.append('the color {}'.format(
|
||||
intent_obj.slots['color']['value']))
|
||||
speech_parts.append(
|
||||
"the color {}".format(intent_obj.slots["color"]["value"])
|
||||
)
|
||||
|
||||
if 'brightness' in slots:
|
||||
intent.async_test_feature(
|
||||
state, SUPPORT_BRIGHTNESS, 'changing brightness')
|
||||
service_data[ATTR_BRIGHTNESS_PCT] = slots['brightness']['value']
|
||||
speech_parts.append('{}% brightness'.format(
|
||||
slots['brightness']['value']))
|
||||
if "brightness" in slots:
|
||||
intent.async_test_feature(state, SUPPORT_BRIGHTNESS, "changing brightness")
|
||||
service_data[ATTR_BRIGHTNESS_PCT] = slots["brightness"]["value"]
|
||||
speech_parts.append("{}% brightness".format(slots["brightness"]["value"]))
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, service_data)
|
||||
|
||||
response = intent_obj.create_response()
|
||||
|
||||
if not speech_parts: # No attributes changed
|
||||
speech = 'Turned on {}'.format(state.name)
|
||||
speech = "Turned on {}".format(state.name)
|
||||
else:
|
||||
parts = ['Changed {} to'.format(state.name)]
|
||||
parts = ["Changed {} to".format(state.name)]
|
||||
for index, part in enumerate(speech_parts):
|
||||
if index == 0:
|
||||
parts.append(' {}'.format(part))
|
||||
parts.append(" {}".format(part))
|
||||
elif index != len(speech_parts) - 1:
|
||||
parts.append(', {}'.format(part))
|
||||
parts.append(", {}".format(part))
|
||||
else:
|
||||
parts.append(' and {}'.format(part))
|
||||
speech = ''.join(parts)
|
||||
parts.append(" and {}".format(part))
|
||||
speech = "".join(parts)
|
||||
|
||||
response.async_set_speech(speech)
|
||||
return response
|
||||
@@ -242,7 +249,8 @@ class SetIntentHandler(intent.IntentHandler):
|
||||
async def async_setup(hass, config):
|
||||
"""Expose light control via state machine and services."""
|
||||
component = hass.data[DOMAIN] = EntityComponent(
|
||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LIGHTS)
|
||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LIGHTS
|
||||
)
|
||||
await component.async_setup(config)
|
||||
|
||||
# load profiles from files
|
||||
@@ -271,7 +279,7 @@ async def async_setup(hass, config):
|
||||
raise Unauthorized(
|
||||
context=service.context,
|
||||
entity_id=light,
|
||||
permission=POLICY_CONTROL
|
||||
permission=POLICY_CONTROL,
|
||||
)
|
||||
|
||||
preprocess_turn_on_alternatives(params)
|
||||
@@ -297,25 +305,25 @@ async def async_setup(hass, config):
|
||||
if not light.should_poll:
|
||||
continue
|
||||
|
||||
update_tasks.append(
|
||||
light.async_update_ha_state(True))
|
||||
update_tasks.append(light.async_update_ha_state(True))
|
||||
|
||||
if update_tasks:
|
||||
await asyncio.wait(update_tasks)
|
||||
|
||||
# Listen for light on and light off service calls.
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_TURN_ON, async_handle_light_on_service,
|
||||
schema=LIGHT_TURN_ON_SCHEMA)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TURN_OFF, LIGHT_TURN_OFF_SCHEMA,
|
||||
'async_turn_off'
|
||||
DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
async_handle_light_on_service,
|
||||
schema=LIGHT_TURN_ON_SCHEMA,
|
||||
)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TOGGLE, LIGHT_TOGGLE_SCHEMA,
|
||||
'async_toggle'
|
||||
SERVICE_TURN_OFF, LIGHT_TURN_OFF_SCHEMA, "async_turn_off"
|
||||
)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_TOGGLE, LIGHT_TOGGLE_SCHEMA, "async_toggle"
|
||||
)
|
||||
|
||||
hass.helpers.intent.async_register(SetIntentHandler())
|
||||
@@ -341,11 +349,13 @@ class Profiles:
|
||||
@classmethod
|
||||
async def load_profiles(cls, hass):
|
||||
"""Load and cache profiles."""
|
||||
|
||||
def load_profile_data(hass):
|
||||
"""Load built-in profiles and custom profiles."""
|
||||
profile_paths = [os.path.join(os.path.dirname(__file__),
|
||||
LIGHT_PROFILES_FILE),
|
||||
hass.config.path(LIGHT_PROFILES_FILE)]
|
||||
profile_paths = [
|
||||
os.path.join(os.path.dirname(__file__), LIGHT_PROFILES_FILE),
|
||||
hass.config.path(LIGHT_PROFILES_FILE),
|
||||
]
|
||||
profiles = {}
|
||||
|
||||
for profile_path in profile_paths:
|
||||
@@ -359,13 +369,12 @@ class Profiles:
|
||||
|
||||
try:
|
||||
for rec in reader:
|
||||
profile, color_x, color_y, brightness = \
|
||||
PROFILE_SCHEMA(rec)
|
||||
profile, color_x, color_y, brightness = PROFILE_SCHEMA(rec)
|
||||
profiles[profile] = (color_x, color_y, brightness)
|
||||
except vol.MultipleInvalid as ex:
|
||||
_LOGGER.error(
|
||||
"Error parsing light profile from %s: %s",
|
||||
profile_path, ex)
|
||||
"Error parsing light profile from %s: %s", profile_path, ex
|
||||
)
|
||||
return None
|
||||
return profiles
|
||||
|
||||
@@ -460,10 +469,7 @@ class Light(ToggleEntity):
|
||||
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_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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user