adaptive-lighting/custom_components/adaptive_lighting/__init__.py

92 lines
2.7 KiB
Python
Raw Permalink Normal View History

2020-10-17 16:54:15 +02:00
"""Adaptive Lighting integration in Home-Assistant."""
2020-09-19 11:35:28 +02:00
import logging
2022-04-17 19:51:44 -07:00
from typing import Any
2020-09-23 13:11:50 +02:00
2020-09-20 18:38:01 +02:00
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
2020-10-05 23:41:57 +02:00
from homeassistant.const import CONF_SOURCE
from homeassistant.core import HomeAssistant
2020-09-26 17:39:17 +02:00
import homeassistant.helpers.config_validation as cv
Feature requests: Switch now optional for service calls | New default icons | Reload `.yaml` w/o restart | `adapt_delay` ms (#459) * added #274 * attempt getSwitchFromLightId() * Update switch.py * changed from register_entity_service to hass.services.async_register * Update switch.py * Update switch.py * Update switch.py * Update switch.py * Update switch.py * Update switch.py * Update services.yaml * Update switch.py * test * test builds ready * target selector may not be possible with current syntax priority is backwards-compatibility as I know most people will smash that update button * expanded light groups * test builds ready * add feature #104 * Update custom_components/adaptive_lighting/switch.py Co-authored-by: Chris <firstof9@gmail.com> * Might as well type both. No reason not to. Co-authored-by: Chris <firstof9@gmail.com> * Might as well type both. No reason not to. Co-authored-by: Chris <firstof9@gmail.com> * Reformatted debug messages. Reimported ServiceCall as suggested * Multiple switches allowed again in services. Apparently this was possible before. With this change, the `lights` argument must not be passed with multiple switches, or the integration has no way of knowing what the user wants to do. Integration did not make this check in prior versions. Also reformatted the debug messages. Removed `automerge.yaml` (my apologies) * Reload config without restart. You can now reload any changes to the yaml file without restarting your home assistant. Should show a 'reload' button in your integrations page or you can call the homeassistant reload integration service call. See https://community.home-assistant.io/t/how-to-allow-custom-compontent-for-yaml-configuration-reloading/391190/4 * Use snake_case for function name 'parseServiceArgs' * Slight rephrase * Small style changes * Rephrased debug messages. Removed `integration_entities` as we rewrote the code from that function already in parse_service_args. Reference function now above parse_service_args. * removed: `these_switches = data = None` * Factor out _find_switch_with_lights * Rename function and add log statement * Remove pylint marker * Handle multiple switches found * Small changes * Rename _parse_service_args to _get_switches_from_service_call * Rephrase log messages * Add type hint --------- Co-authored-by: Chris <firstof9@gmail.com> Co-authored-by: Bas Nijholt <bas@nijho.lt>
2023-03-25 22:22:05 -05:00
from homeassistant.helpers.reload import async_setup_reload_service
2022-08-28 15:08:03 -07:00
import voluptuous as vol
2020-09-19 11:10:54 +02:00
2020-09-30 00:42:27 +02:00
from .const import (
_DOMAIN_SCHEMA,
ATTR_TURN_ON_OFF_LISTENER,
CONF_NAME,
DOMAIN,
UNDO_UPDATE_LISTENER,
)
2020-09-19 11:10:54 +02:00
2020-09-20 18:38:01 +02:00
_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["switch"]
2020-09-19 11:10:54 +02:00
2020-09-23 13:11:50 +02:00
2020-09-26 13:13:44 +02:00
def _all_unique_names(value):
2020-10-05 23:41:57 +02:00
"""Validate that all entities have a unique profile name."""
2020-09-23 18:20:25 +02:00
hosts = [device[CONF_NAME] for device in value]
schema = vol.Schema(vol.Unique())
schema(hosts)
return value
2020-09-24 20:41:24 +02:00
CONFIG_SCHEMA = vol.Schema(
2020-09-26 13:13:44 +02:00
{DOMAIN: vol.All(cv.ensure_list, [_DOMAIN_SCHEMA], _all_unique_names)},
2020-09-24 20:41:24 +02:00
extra=vol.ALLOW_EXTRA,
)
2020-09-23 18:20:25 +02:00
2022-04-17 19:50:55 -07:00
async def async_setup(hass: HomeAssistant, config: dict[str, Any]):
2020-09-19 11:10:54 +02:00
"""Import integration from config."""
Feature requests: Switch now optional for service calls | New default icons | Reload `.yaml` w/o restart | `adapt_delay` ms (#459) * added #274 * attempt getSwitchFromLightId() * Update switch.py * changed from register_entity_service to hass.services.async_register * Update switch.py * Update switch.py * Update switch.py * Update switch.py * Update switch.py * Update switch.py * Update services.yaml * Update switch.py * test * test builds ready * target selector may not be possible with current syntax priority is backwards-compatibility as I know most people will smash that update button * expanded light groups * test builds ready * add feature #104 * Update custom_components/adaptive_lighting/switch.py Co-authored-by: Chris <firstof9@gmail.com> * Might as well type both. No reason not to. Co-authored-by: Chris <firstof9@gmail.com> * Might as well type both. No reason not to. Co-authored-by: Chris <firstof9@gmail.com> * Reformatted debug messages. Reimported ServiceCall as suggested * Multiple switches allowed again in services. Apparently this was possible before. With this change, the `lights` argument must not be passed with multiple switches, or the integration has no way of knowing what the user wants to do. Integration did not make this check in prior versions. Also reformatted the debug messages. Removed `automerge.yaml` (my apologies) * Reload config without restart. You can now reload any changes to the yaml file without restarting your home assistant. Should show a 'reload' button in your integrations page or you can call the homeassistant reload integration service call. See https://community.home-assistant.io/t/how-to-allow-custom-compontent-for-yaml-configuration-reloading/391190/4 * Use snake_case for function name 'parseServiceArgs' * Slight rephrase * Small style changes * Rephrased debug messages. Removed `integration_entities` as we rewrote the code from that function already in parse_service_args. Reference function now above parse_service_args. * removed: `these_switches = data = None` * Factor out _find_switch_with_lights * Rename function and add log statement * Remove pylint marker * Handle multiple switches found * Small changes * Rename _parse_service_args to _get_switches_from_service_call * Rephrase log messages * Add type hint --------- Co-authored-by: Chris <firstof9@gmail.com> Co-authored-by: Bas Nijholt <bas@nijho.lt>
2023-03-25 22:22:05 -05:00
# This will reload any changes the user made to any YAML configurations.
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
2020-09-19 11:10:54 +02:00
if DOMAIN in config:
for entry in config[DOMAIN]:
hass.async_create_task(
hass.config_entries.flow.async_init(
2020-10-05 23:41:57 +02:00
DOMAIN, context={CONF_SOURCE: SOURCE_IMPORT}, data=entry
)
2020-09-19 11:10:54 +02:00
)
return True
2020-10-05 23:41:57 +02:00
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
2020-09-19 11:10:54 +02:00
"""Set up the component."""
2020-09-28 23:39:55 +02:00
data = hass.data.setdefault(DOMAIN, {})
2020-09-19 11:10:54 +02:00
undo_listener = config_entry.add_update_listener(async_update_options)
2020-09-28 23:39:55 +02:00
data[config_entry.entry_id] = {UNDO_UPDATE_LISTENER: undo_listener}
for platform in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
)
2020-09-19 11:10:54 +02:00
return True
async def async_update_options(hass, config_entry: ConfigEntry):
"""Update options."""
await hass.config_entries.async_reload(config_entry.entry_id)
async def async_unload_entry(hass, config_entry: ConfigEntry) -> bool:
"""Unload a config entry."""
2020-09-29 13:13:08 +02:00
unload_ok = await hass.config_entries.async_forward_entry_unload(
config_entry, "switch"
)
2020-09-28 23:39:55 +02:00
data = hass.data[DOMAIN]
data[config_entry.entry_id][UNDO_UPDATE_LISTENER]()
2020-10-17 15:43:24 +02:00
if unload_ok:
data.pop(config_entry.entry_id)
if len(data) == 1 and ATTR_TURN_ON_OFF_LISTENER in data:
# no more config_entries
2020-10-05 23:41:57 +02:00
turn_on_off_listener = data.pop(ATTR_TURN_ON_OFF_LISTENER)
turn_on_off_listener.remove_listener()
turn_on_off_listener.remove_listener2()
2020-10-17 15:43:24 +02:00
if not data:
hass.data.pop(DOMAIN)
2020-09-20 18:38:01 +02:00
return unload_ok