mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-29 13:24:18 +02:00
initial commit, passes most tests.
'function too complex'
This commit is contained in:
parent
39e9d0e74f
commit
d9cd74bef3
3 changed files with 196 additions and 1 deletions
|
|
@ -1,7 +1,12 @@
|
|||
"""Constants for the Adaptive Lighting integration."""
|
||||
|
||||
from homeassistant.components.light import VALID_TRANSITION
|
||||
from homeassistant.const import CONF_ENTITY_ID
|
||||
from homeassistant.const import (
|
||||
CONF_ENTITY_ID,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.helpers import selector
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import voluptuous as vol
|
||||
|
|
@ -201,6 +206,19 @@ DOCS[CONF_USE_DEFAULTS] = (
|
|||
'documented defaults), or "configuration" (reverts to switch config defaults). ⚙️'
|
||||
)
|
||||
|
||||
CONF_WHICH_SWITCH, DEFAULT_WHICH_SWITCH = "switch_type", "main"
|
||||
DOCS[CONF_WHICH_SWITCH] = (
|
||||
"Which switch to target in this service call. Options: "
|
||||
'"main" (default, targets the main switch), "sleep", "brightness", "color"'
|
||||
)
|
||||
DOCS[
|
||||
SERVICE_TURN_ON
|
||||
] = "Turn on an Adaptive Lighting main/sleep/brightness/color switch"
|
||||
DOCS[
|
||||
SERVICE_TURN_OFF
|
||||
] = "Turn off an Adaptive Lighting main/sleep/brightness/color switch"
|
||||
DOCS[SERVICE_TOGGLE] = "Toggle an Adaptive Lighting main/sleep/brightness/color switch"
|
||||
|
||||
TURNING_OFF_DELAY = 5
|
||||
|
||||
DOCS_MANUAL_CONTROL = {
|
||||
|
|
@ -339,6 +357,14 @@ def apply_service_schema(initial_transition: int = 1):
|
|||
)
|
||||
|
||||
|
||||
SERVICE_TOGGLE_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_ENTITY_ID): cv.entity_ids,
|
||||
vol.Optional(CONF_LIGHTS, default=[]): cv.entity_ids,
|
||||
vol.Optional(CONF_WHICH_SWITCH): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
SET_MANUAL_CONTROL_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_ENTITY_ID): cv.entity_ids,
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ from homeassistant.const import (
|
|||
EVENT_CALL_SERVICE,
|
||||
EVENT_HOMEASSISTANT_STARTED,
|
||||
EVENT_STATE_CHANGED,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
|
|
@ -129,6 +130,7 @@ from .const import (
|
|||
CONF_TRANSITION,
|
||||
CONF_TURN_ON_LIGHTS,
|
||||
CONF_USE_DEFAULTS,
|
||||
CONF_WHICH_SWITCH,
|
||||
DOMAIN,
|
||||
EXTRA_VALIDATION,
|
||||
ICON_BRIGHTNESS,
|
||||
|
|
@ -138,6 +140,7 @@ from .const import (
|
|||
SERVICE_APPLY,
|
||||
SERVICE_CHANGE_SWITCH_SETTINGS,
|
||||
SERVICE_SET_MANUAL_CONTROL,
|
||||
SERVICE_TOGGLE_SCHEMA,
|
||||
SET_MANUAL_CONTROL_SCHEMA,
|
||||
SLEEP_MODE_SWITCH,
|
||||
SUN_EVENT_MIDNIGHT,
|
||||
|
|
@ -472,6 +475,69 @@ async def async_setup_entry(
|
|||
update_before_add=True,
|
||||
)
|
||||
|
||||
@callback
|
||||
async def handle_turn_on(service_call: ServiceCall):
|
||||
"""Toggles the specified switch."""
|
||||
data = service_call.data
|
||||
_LOGGER.debug(
|
||||
"Called 'adaptive_lighting.turn_on' service with '%s'",
|
||||
data,
|
||||
)
|
||||
switches = _get_switches_from_service_call(hass, service_call)
|
||||
these_switches = [] # CONF_WHICH_SWITCH == "main" is default
|
||||
if data[CONF_WHICH_SWITCH] == "sleep":
|
||||
these_switches = [s.sleep_mode_switch for s in switches]
|
||||
elif data[CONF_WHICH_SWITCH] == "brightness":
|
||||
these_switches = [s.adapt_brightness_switch for s in switches]
|
||||
elif data[CONF_WHICH_SWITCH] == "color":
|
||||
these_switches = [s.adapt_color_switch for s in switches]
|
||||
_LOGGER.debug("Turning on switches [%s]", these_switches)
|
||||
for switch in these_switches:
|
||||
await switch.async_turn_on()
|
||||
|
||||
@callback
|
||||
async def handle_turn_off(service_call: ServiceCall):
|
||||
"""Toggles the specified switch."""
|
||||
data = service_call.data
|
||||
_LOGGER.debug(
|
||||
"Called 'adaptive_lighting.turn_off' service with '%s'",
|
||||
data,
|
||||
)
|
||||
switches = _get_switches_from_service_call(hass, service_call)
|
||||
these_switches = [] # CONF_WHICH_SWITCH == "main" is default
|
||||
if data[CONF_WHICH_SWITCH] == "sleep":
|
||||
these_switches = [s.sleep_mode_switch for s in switches]
|
||||
elif data[CONF_WHICH_SWITCH] == "brightness":
|
||||
these_switches = [s.adapt_brightness_switch for s in switches]
|
||||
elif data[CONF_WHICH_SWITCH] == "color":
|
||||
these_switches = [s.adapt_color_switch for s in switches]
|
||||
_LOGGER.debug("Turning off switches [%s]", these_switches)
|
||||
for switch in these_switches:
|
||||
await switch.async_turn_off()
|
||||
|
||||
@callback
|
||||
async def handle_toggle(service_call: ServiceCall):
|
||||
"""Toggles the specified switch."""
|
||||
data = service_call.data
|
||||
_LOGGER.debug(
|
||||
"Called 'adaptive_lighting.toggle' service with '%s'",
|
||||
data,
|
||||
)
|
||||
switches = _get_switches_from_service_call(hass, service_call)
|
||||
these_switches = [] # CONF_WHICH_SWITCH == "main" is default
|
||||
if data[CONF_WHICH_SWITCH] == "sleep":
|
||||
these_switches = [s.sleep_mode_switch for s in switches]
|
||||
elif data[CONF_WHICH_SWITCH] == "brightness":
|
||||
these_switches = [s.adapt_brightness_switch for s in switches]
|
||||
elif data[CONF_WHICH_SWITCH] == "color":
|
||||
these_switches = [s.adapt_color_switch for s in switches]
|
||||
_LOGGER.debug("Toggling switches [%s]", these_switches)
|
||||
for switch in these_switches:
|
||||
if switch.is_on:
|
||||
await switch.async_turn_off()
|
||||
else:
|
||||
await switch.async_turn_on()
|
||||
|
||||
@callback
|
||||
async def handle_apply(service_call: ServiceCall):
|
||||
"""Handle the entity service apply."""
|
||||
|
|
@ -544,6 +610,30 @@ async def async_setup_entry(
|
|||
), # pylint: disable=protected-access
|
||||
)
|
||||
|
||||
# Register `turn_on` service
|
||||
hass.services.async_register(
|
||||
domain=DOMAIN,
|
||||
service=SERVICE_TURN_ON,
|
||||
service_func=handle_turn_on,
|
||||
schema=SERVICE_TOGGLE_SCHEMA,
|
||||
)
|
||||
|
||||
# Register `turn_off` service
|
||||
hass.services.async_register(
|
||||
domain=DOMAIN,
|
||||
service=SERVICE_TURN_OFF,
|
||||
service_func=handle_turn_off,
|
||||
schema=SERVICE_TOGGLE_SCHEMA,
|
||||
)
|
||||
|
||||
# Register `toggle` service
|
||||
hass.services.async_register(
|
||||
domain=DOMAIN,
|
||||
service=SERVICE_TOGGLE,
|
||||
service_func=handle_toggle,
|
||||
schema=SERVICE_TOGGLE_SCHEMA,
|
||||
)
|
||||
|
||||
# Register `set_manual_control` service
|
||||
hass.services.async_register(
|
||||
domain=DOMAIN,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue