From d9cd74bef3023bfc6349320e0fd97689da708f7d Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Sat, 8 Apr 2023 05:15:17 -0500 Subject: [PATCH 1/7] initial commit, passes most tests. 'function too complex' --- custom_components/adaptive_lighting/const.py | 28 +++++- custom_components/adaptive_lighting/switch.py | 90 +++++++++++++++++++ tests/test_switch.py | 79 ++++++++++++++++ 3 files changed, 196 insertions(+), 1 deletion(-) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 64620927..c0cb2eaf 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -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 @@ '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, diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 5b816b67..b28a2abc 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -56,6 +56,7 @@ EVENT_CALL_SERVICE, EVENT_HOMEASSISTANT_STARTED, EVENT_STATE_CHANGED, + SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_OFF, @@ -129,6 +130,7 @@ CONF_TRANSITION, CONF_TURN_ON_LIGHTS, CONF_USE_DEFAULTS, + CONF_WHICH_SWITCH, DOMAIN, EXTRA_VALIDATION, ICON_BRIGHTNESS, @@ -138,6 +140,7 @@ 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 handle_set_manual_control(service_call: ServiceCall): ), # 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, diff --git a/tests/test_switch.py b/tests/test_switch.py index f6d671f4..3b8f615b 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -25,6 +25,7 @@ CONF_TRANSITION, CONF_TURN_ON_LIGHTS, CONF_USE_DEFAULTS, + CONF_WHICH_SWITCH, DEFAULT_MAX_BRIGHTNESS, DEFAULT_NAME, DEFAULT_SLEEP_BRIGHTNESS, @@ -64,6 +65,8 @@ CONF_NAME, CONF_PLATFORM, EVENT_STATE_CHANGED, + SERVICE_TOGGLE, + SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_OFF, STATE_ON, @@ -1246,6 +1249,82 @@ async def test_area(hass): assert light.entity_id not in switch.turn_on_off_listener.last_service_data +@pytest.mark.dependency(depends=GLOBAL_TEST_DEPENDENCIES) +async def test_switch_turn_on_off_toggle(hass): + """Test adaptive_lighting.change_switch_settings service.""" + switch, (_, _, light) = await setup_lights_and_switch(hass) + entity_id = switch.entity_id + assert entity_id not in switch._lights + + async def turn_on(which: str, **kwargs): + await hass.services.async_call( + DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: entity_id, + CONF_WHICH_SWITCH: which, + **kwargs, + }, + blocking=True, + ) + await hass.async_block_till_done() + + async def turn_off(which: str, **kwargs): + await hass.services.async_call( + DOMAIN, + SERVICE_TURN_OFF, + { + ATTR_ENTITY_ID: entity_id, + CONF_WHICH_SWITCH: which, + **kwargs, + }, + blocking=True, + ) + await hass.async_block_till_done() + + async def toggle(which: str, **kwargs): + await hass.services.async_call( + DOMAIN, + SERVICE_TOGGLE, + { + ATTR_ENTITY_ID: entity_id, + CONF_WHICH_SWITCH: which, + **kwargs, + }, + blocking=True, + ) + await hass.async_block_till_done() + + # Test sleep + await turn_on("sleep") + assert switch.sleep_mode_switch.is_on + await turn_off("sleep") + assert not switch.sleep_mode_switch.is_on + await toggle("sleep") + assert switch.sleep_mode_switch.is_on + # Test brightness + await turn_on("brightness") + assert switch.adapt_brightness_switch.is_on + await turn_off("brightness") + assert not switch.adapt_brightness_switch.is_on + await toggle("brightness") + assert switch.adapt_brightness_switch.is_on + # Test color + await turn_on("color") + assert switch.adapt_color_switch.is_on + await turn_off("color") + assert not switch.adapt_color_switch.is_on + await toggle("color") + assert switch.adapt_color_switch.is_on + # Test main + await turn_on("main") + assert switch.is_on + await turn_off("main") + assert not switch.is_on + await toggle("main") + assert switch.is_on + + @pytest.mark.dependency(depends=GLOBAL_TEST_DEPENDENCIES) async def test_change_switch_settings_service(hass): """Test adaptive_lighting.change_switch_settings service.""" From 251babce9d7d911cf2f31f5eb967c98ee85b126e Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Sat, 8 Apr 2023 05:35:16 -0500 Subject: [PATCH 2/7] fixes and now passes tests --- custom_components/adaptive_lighting/switch.py | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index b28a2abc..ec04d431 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -484,15 +484,15 @@ async def handle_turn_on(service_call: ServiceCall): 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] + 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] + 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: + switches = [s.adapt_color_switch for s in switches] + + _LOGGER.debug("Turning on switches [%s]", switches) + for switch in switches: await switch.async_turn_on() @callback @@ -504,15 +504,14 @@ async def handle_turn_off(service_call: ServiceCall): 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] + 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] + 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: + switches = [s.adapt_color_switch for s in switches] + _LOGGER.debug("Turning off switches [%s]", switches) + for switch in switches: await switch.async_turn_off() @callback @@ -524,15 +523,14 @@ async def handle_toggle(service_call: ServiceCall): 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] + 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] + 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: + switches = [s.adapt_color_switch for s in switches] + _LOGGER.debug("Toggling switches [%s]", switches) + for switch in switches: if switch.is_on: await switch.async_turn_off() else: From 258964d97bace1cc306c2690b4b9cf5f945eae7b Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Mon, 10 Apr 2023 12:07:18 -0500 Subject: [PATCH 3/7] pass flake8 move stuff out of `async_setup_entry` --- custom_components/adaptive_lighting/switch.py | 257 +++++++++--------- 1 file changed, 134 insertions(+), 123 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 32b51540..0ced7a2f 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -70,6 +70,7 @@ HomeAssistant, ServiceCall, State, + async_get_hass, callback, ) from homeassistant.helpers import entity_platform, entity_registry @@ -415,6 +416,139 @@ async def handle_change_switch_settings( ) +@callback +async def handle_turn_on(service_call: ServiceCall): + """Toggles the specified switch.""" + hass = async_get_hass() + data = service_call.data + _LOGGER.debug( + "Called 'adaptive_lighting.turn_on' service with '%s'", + data, + ) + switches = _get_switches_from_service_call(hass, service_call) + if data[CONF_WHICH_SWITCH] == "sleep": + switches = [s.sleep_mode_switch for s in switches] + elif data[CONF_WHICH_SWITCH] == "brightness": + switches = [s.adapt_brightness_switch for s in switches] + elif data[CONF_WHICH_SWITCH] == "color": + switches = [s.adapt_color_switch for s in switches] + + _LOGGER.debug("Turning on switches [%s]", switches) + for switch in switches: + await switch.async_turn_on() + + +@callback +async def handle_turn_off(service_call: ServiceCall): + """Toggles the specified switch.""" + hass = async_get_hass() + data = service_call.data + _LOGGER.debug( + "Called 'adaptive_lighting.turn_off' service with '%s'", + data, + ) + switches = _get_switches_from_service_call(hass, service_call) + if data[CONF_WHICH_SWITCH] == "sleep": + switches = [s.sleep_mode_switch for s in switches] + elif data[CONF_WHICH_SWITCH] == "brightness": + switches = [s.adapt_brightness_switch for s in switches] + elif data[CONF_WHICH_SWITCH] == "color": + switches = [s.adapt_color_switch for s in switches] + _LOGGER.debug("Turning off switches [%s]", switches) + for switch in switches: + await switch.async_turn_off() + + +@callback +async def handle_toggle(service_call: ServiceCall): + """Toggles the specified switch.""" + hass = async_get_hass() + data = service_call.data + _LOGGER.debug( + "Called 'adaptive_lighting.toggle' service with '%s'", + data, + ) + switches = _get_switches_from_service_call(hass, service_call) + if data[CONF_WHICH_SWITCH] == "sleep": + switches = [s.sleep_mode_switch for s in switches] + elif data[CONF_WHICH_SWITCH] == "brightness": + switches = [s.adapt_brightness_switch for s in switches] + elif data[CONF_WHICH_SWITCH] == "color": + switches = [s.adapt_color_switch for s in switches] + _LOGGER.debug("Toggling switches [%s]", switches) + for switch in 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.""" + hass = async_get_hass() + data = service_call.data + _LOGGER.debug( + "Called 'adaptive_lighting.apply' service with '%s'", + data, + ) + switches = _get_switches_from_service_call(hass, service_call) + lights = data[CONF_LIGHTS] + for switch in switches: + if not lights: + all_lights = switch._lights # pylint: disable=protected-access + else: + all_lights = _expand_light_groups(switch.hass, lights) + switch.turn_on_off_listener.lights.update(all_lights) + for light in all_lights: + if data[CONF_TURN_ON_LIGHTS] or is_on(hass, light): + await switch._adapt_light( # pylint: disable=protected-access + light, + data[CONF_TRANSITION], + data[ATTR_ADAPT_BRIGHTNESS], + data[ATTR_ADAPT_COLOR], + data[CONF_PREFER_RGB_COLOR], + force=True, + context=switch.create_context( + "service", parent=service_call.context + ), + ) + + +@callback +async def handle_set_manual_control(service_call: ServiceCall): + """Set or unset lights as 'manually controlled'.""" + hass = async_get_hass() + data = service_call.data + _LOGGER.debug( + "Called 'adaptive_lighting.set_manual_control' service with '%s'", + data, + ) + switches = _get_switches_from_service_call(hass, service_call) + lights = data[CONF_LIGHTS] + for switch in switches: + if not lights: + all_lights = switch._lights # pylint: disable=protected-access + else: + all_lights = _expand_light_groups(switch.hass, lights) + if service_call.data[CONF_MANUAL_CONTROL]: + for light in all_lights: + switch.turn_on_off_listener.mark_as_manual_control(light) + _fire_manual_control_event(switch, light, service_call.context) + else: + switch.turn_on_off_listener.reset(*all_lights) + if switch.is_on: + # pylint: disable=protected-access + await switch._update_attrs_and_maybe_adapt_lights( + all_lights, + transition=switch._initial_transition, + force=True, + context=switch.create_context( + "service", parent=service_call.context + ), + ) + + @callback def _fire_manual_control_event( switch: AdaptiveSwitch, light: str, context: Context, is_async=True @@ -475,129 +609,6 @@ 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) - if data[CONF_WHICH_SWITCH] == "sleep": - switches = [s.sleep_mode_switch for s in switches] - elif data[CONF_WHICH_SWITCH] == "brightness": - switches = [s.adapt_brightness_switch for s in switches] - elif data[CONF_WHICH_SWITCH] == "color": - switches = [s.adapt_color_switch for s in switches] - - _LOGGER.debug("Turning on switches [%s]", switches) - for switch in 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) - if data[CONF_WHICH_SWITCH] == "sleep": - switches = [s.sleep_mode_switch for s in switches] - elif data[CONF_WHICH_SWITCH] == "brightness": - switches = [s.adapt_brightness_switch for s in switches] - elif data[CONF_WHICH_SWITCH] == "color": - switches = [s.adapt_color_switch for s in switches] - _LOGGER.debug("Turning off switches [%s]", switches) - for switch in 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) - if data[CONF_WHICH_SWITCH] == "sleep": - switches = [s.sleep_mode_switch for s in switches] - elif data[CONF_WHICH_SWITCH] == "brightness": - switches = [s.adapt_brightness_switch for s in switches] - elif data[CONF_WHICH_SWITCH] == "color": - switches = [s.adapt_color_switch for s in switches] - _LOGGER.debug("Toggling switches [%s]", switches) - for switch in 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.""" - data = service_call.data - _LOGGER.debug( - "Called 'adaptive_lighting.apply' service with '%s'", - data, - ) - switches = _get_switches_from_service_call(hass, service_call) - lights = data[CONF_LIGHTS] - for switch in switches: - if not lights: - all_lights = switch._lights # pylint: disable=protected-access - else: - all_lights = _expand_light_groups(switch.hass, lights) - switch.turn_on_off_listener.lights.update(all_lights) - for light in all_lights: - if data[CONF_TURN_ON_LIGHTS] or is_on(hass, light): - await switch._adapt_light( # pylint: disable=protected-access - light, - data[CONF_TRANSITION], - data[ATTR_ADAPT_BRIGHTNESS], - data[ATTR_ADAPT_COLOR], - data[CONF_PREFER_RGB_COLOR], - force=True, - context=switch.create_context( - "service", parent=service_call.context - ), - ) - - @callback - async def handle_set_manual_control(service_call: ServiceCall): - """Set or unset lights as 'manually controlled'.""" - data = service_call.data - _LOGGER.debug( - "Called 'adaptive_lighting.set_manual_control' service with '%s'", - data, - ) - switches = _get_switches_from_service_call(hass, service_call) - lights = data[CONF_LIGHTS] - for switch in switches: - if not lights: - all_lights = switch._lights # pylint: disable=protected-access - else: - all_lights = _expand_light_groups(switch.hass, lights) - if service_call.data[CONF_MANUAL_CONTROL]: - for light in all_lights: - switch.turn_on_off_listener.mark_as_manual_control(light) - _fire_manual_control_event(switch, light, service_call.context) - else: - switch.turn_on_off_listener.reset(*all_lights) - if switch.is_on: - # pylint: disable=protected-access - await switch._update_attrs_and_maybe_adapt_lights( - all_lights, - transition=switch._initial_transition, - force=True, - context=switch.create_context( - "service", parent=service_call.context - ), - ) - # Register `apply` service hass.services.async_register( domain=DOMAIN, From 3b3f56f2d245ce815a3e62ccccff41b221168960 Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Mon, 10 Apr 2023 12:39:26 -0500 Subject: [PATCH 4/7] Update test_switch.py --- tests/test_switch.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_switch.py b/tests/test_switch.py index f38239dc..c569f16d 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -53,7 +53,6 @@ ATTR_XY_COLOR, ) from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN -from homeassistant.components.light import SERVICE_TURN_OFF from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN import homeassistant.config as config_util from homeassistant.config_entries import ConfigEntryState From cfe565a8b276049feb3b00cbcd839078fbb9fcdd Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 31 May 2023 14:50:56 -0700 Subject: [PATCH 5/7] Add docs --- README.md | 20 +++++++++++++++++++ .../adaptive_lighting/_docs_helpers.py | 5 +++++ custom_components/adaptive_lighting/const.py | 12 +++++------ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1a0830b0..74adea61 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ The `adaptive_lighting.manual_control` event is fired when a light is marked as - [:memo: Options](#memo-options) - [:hammer_and_wrench: Services](#hammer_and_wrench-services) - [`adaptive_lighting.apply`](#adaptive_lightingapply) + - [`adaptive_lighting.turn_on`, `adaptive_lighting.turn_off`, `adaptive_lighting.toggle`](#adaptive_lightingturn_on-adaptive_lightingturn_off-adaptive_lightingtoggle) - [`adaptive_lighting.set_manual_control`](#adaptive_lightingset_manual_control) - [`adaptive_lighting.change_switch_settings`](#adaptive_lightingchange_switch_settings) - [:robot: Automation examples](#robot-automation-examples) @@ -176,6 +177,25 @@ adaptive_lighting: | `turn_on_lights` | Whether to turn on lights that are currently off. 🔆 | ❌ | bool | + + +#### `adaptive_lighting.turn_on`, `adaptive_lighting.turn_off`, `adaptive_lighting.toggle` + +`adaptive_lighting.turn_on`, `adaptive_lighting.turn_off`, `adaptive_lighting.toggle` turn Adaptive Lighting on or off. + +TODO: ... + + + + + + + + + + + + #### `adaptive_lighting.set_manual_control` `adaptive_lighting.set_manual_control` can mark (or unmark) whether a light is "manually controlled", meaning that when a light has `manual_control`, the light is not adapted. diff --git a/custom_components/adaptive_lighting/_docs_helpers.py b/custom_components/adaptive_lighting/_docs_helpers.py index 40afc235..2f8371cb 100644 --- a/custom_components/adaptive_lighting/_docs_helpers.py +++ b/custom_components/adaptive_lighting/_docs_helpers.py @@ -9,6 +9,7 @@ DOCS, DOCS_APPLY, DOCS_MANUAL_CONTROL, + SERVICE_TOGGLE_SCHEMA, SET_MANUAL_CONTROL_SCHEMA, VALIDATION_TUPLES, apply_service_schema, @@ -110,6 +111,10 @@ def generate_apply_markdown_table(): return _generate_service_markdown_table(apply_service_schema(), DOCS_APPLY) +def generate_turn_on_markdown_table(): + return _generate_service_markdown_table(SERVICE_TOGGLE_SCHEMA, DOCS_APPLY) + + def generate_set_manual_control_markdown_table(): return _generate_service_markdown_table( SET_MANUAL_CONTROL_SCHEMA, DOCS_MANUAL_CONTROL diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index babea0a5..df159b4b 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -211,12 +211,12 @@ "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_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 From 295c0fa2c1cfdf0a07a86ad675dfaeb9995e848d Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 31 May 2023 14:55:59 -0700 Subject: [PATCH 6/7] check for cv.string --- custom_components/adaptive_lighting/_docs_helpers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/custom_components/adaptive_lighting/_docs_helpers.py b/custom_components/adaptive_lighting/_docs_helpers.py index 2f8371cb..00c83fe1 100644 --- a/custom_components/adaptive_lighting/_docs_helpers.py +++ b/custom_components/adaptive_lighting/_docs_helpers.py @@ -42,6 +42,8 @@ def _type_to_str(type_: Any) -> str: """Convert a (voluptuous) type to a string.""" if type_ == cv.entity_ids: return "list of `entity_id`s" + elif type_ == cv.string: + return "str" elif type_ in (bool, int, float, str): return f"`{type_.__name__}`" elif type_ == cv.boolean: From 5fdc911d30847b6ceb27094d2ebda6ccebe97e48 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 31 May 2023 15:05:02 -0700 Subject: [PATCH 7/7] Update README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 74adea61..7f1e10ee 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,11 @@ TODO: ... +| Service data attribute | Description | Required | Type | +|:-------------------------|:--------------------------------------------------------------------------------------------------------------------------------|:-----------|:---------------------| +| `entity_id` | The `entity_id` of the switch with the settings to apply. 📝 | ✅ | list of `entity_id`s | +| `lights` | A light (or list of lights) to apply the settings to. 💡 | ❌ | list of `entity_id`s | +| `switch_type` | Which switch to target in this service call. Options: "main" (default, targets the main switch), "sleep", "brightness", "color" | ✅ | str |