1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00
ha-core/homeassistant/components/rest/__init__.py

185 lines
5.9 KiB
Python
Raw Normal View History

Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 07:07:39 +01:00
"""The rest component."""
import asyncio
import logging
import httpx
import voluptuous as vol
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import (
CONF_AUTHENTICATION,
CONF_HEADERS,
CONF_METHOD,
CONF_PARAMS,
CONF_PASSWORD,
CONF_PAYLOAD,
CONF_RESOURCE,
CONF_RESOURCE_TEMPLATE,
CONF_SCAN_INTERVAL,
CONF_TIMEOUT,
CONF_USERNAME,
CONF_VERIFY_SSL,
HTTP_DIGEST_AUTHENTICATION,
SERVICE_RELOAD,
Platform,
)
from homeassistant.core import HomeAssistant, ServiceCall, callback
2021-12-08 23:35:50 +01:00
from homeassistant.helpers import discovery, template
2022-09-14 20:16:23 +02:00
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import (
DEFAULT_SCAN_INTERVAL,
EntityComponent,
)
from homeassistant.helpers.reload import async_reload_integration_platforms
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import COORDINATOR, DOMAIN, PLATFORM_IDX, REST, REST_DATA, REST_IDX
from .data import RestData
from .schema import CONFIG_SCHEMA # noqa: F401
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.NOTIFY,
Platform.SENSOR,
Platform.SWITCH,
]
COORDINATOR_AWARE_PLATFORMS = [SENSOR_DOMAIN, BINARY_SENSOR_DOMAIN]
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the rest platforms."""
2022-09-14 20:16:23 +02:00
component = EntityComponent[Entity](_LOGGER, DOMAIN, hass)
_async_setup_shared_data(hass)
async def reload_service_handler(service: ServiceCall) -> None:
"""Remove all user-defined groups and load new ones from config."""
2021-10-30 16:29:07 +02:00
if (conf := await component.async_prepare_reload()) is None:
return
await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)
_async_setup_shared_data(hass)
await _async_process_config(hass, conf)
hass.services.async_register(
DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=vol.Schema({})
)
return await _async_process_config(hass, config)
@callback
def _async_setup_shared_data(hass: HomeAssistant):
"""Create shared data for platform config and rest coordinators."""
hass.data[DOMAIN] = {key: [] for key in (REST_DATA, *COORDINATOR_AWARE_PLATFORMS)}
async def _async_process_config(hass, config) -> bool:
"""Process rest configuration."""
if DOMAIN not in config:
return True
refresh_tasks = []
load_tasks = []
for rest_idx, conf in enumerate(config[DOMAIN]):
scan_interval = conf.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
resource_template = conf.get(CONF_RESOURCE_TEMPLATE)
rest = create_rest_data_from_config(hass, conf)
coordinator = _rest_coordinator(hass, rest, resource_template, scan_interval)
refresh_tasks.append(coordinator.async_refresh())
hass.data[DOMAIN][REST_DATA].append({REST: rest, COORDINATOR: coordinator})
for platform_domain in COORDINATOR_AWARE_PLATFORMS:
if platform_domain not in conf:
continue
for platform_conf in conf[platform_domain]:
hass.data[DOMAIN][platform_domain].append(platform_conf)
platform_idx = len(hass.data[DOMAIN][platform_domain]) - 1
load = discovery.async_load_platform(
hass,
platform_domain,
DOMAIN,
{REST_IDX: rest_idx, PLATFORM_IDX: platform_idx},
config,
)
load_tasks.append(load)
if refresh_tasks:
await asyncio.gather(*refresh_tasks)
if load_tasks:
await asyncio.gather(*load_tasks)
return True
async def async_get_config_and_coordinator(hass, platform_domain, discovery_info):
"""Get the config and coordinator for the platform from discovery."""
shared_data = hass.data[DOMAIN][REST_DATA][discovery_info[REST_IDX]]
conf = hass.data[DOMAIN][platform_domain][discovery_info[PLATFORM_IDX]]
coordinator = shared_data[COORDINATOR]
rest = shared_data[REST]
if rest.data is None:
await coordinator.async_request_refresh()
return conf, coordinator, rest
def _rest_coordinator(hass, rest, resource_template, update_interval):
"""Wrap a DataUpdateCoordinator around the rest object."""
if resource_template:
async def _async_refresh_with_resource_template():
rest.set_url(resource_template.async_render(parse_result=False))
await rest.async_update()
update_method = _async_refresh_with_resource_template
else:
update_method = rest.async_update
return DataUpdateCoordinator(
hass,
_LOGGER,
name="rest data",
update_method=update_method,
update_interval=update_interval,
)
def create_rest_data_from_config(hass, config):
"""Create RestData from config."""
resource = config.get(CONF_RESOURCE)
resource_template = config.get(CONF_RESOURCE_TEMPLATE)
method = config.get(CONF_METHOD)
payload = config.get(CONF_PAYLOAD)
verify_ssl = config.get(CONF_VERIFY_SSL)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
headers = config.get(CONF_HEADERS)
params = config.get(CONF_PARAMS)
timeout = config.get(CONF_TIMEOUT)
if resource_template is not None:
resource_template.hass = hass
resource = resource_template.async_render(parse_result=False)
2021-12-08 23:35:50 +01:00
template.attach(hass, headers)
template.attach(hass, params)
if username and password:
if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
auth = httpx.DigestAuth(username, password)
else:
auth = (username, password)
else:
auth = None
return RestData(
hass, method, resource, auth, headers, params, payload, verify_ssl, timeout
)