1
mirror of https://github.com/home-assistant/core synced 2024-07-27 18:58:57 +02:00
ha-core/homeassistant/components/rest/__init__.py

202 lines
7.0 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."""
from __future__ import annotations
import asyncio
from collections.abc import Coroutine
import contextlib
from datetime import timedelta
import logging
from typing import Any
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
from homeassistant.exceptions import HomeAssistantError
2021-12-08 23:35:50 +01:00
from homeassistant.helpers import discovery, template
from homeassistant.helpers.entity_component import DEFAULT_SCAN_INTERVAL
from homeassistant.helpers.reload import (
async_integration_yaml_config,
async_reload_integration_platforms,
)
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
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, RESOURCE_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."""
_async_setup_shared_data(hass)
async def reload_service_handler(service: ServiceCall) -> None:
"""Remove all user-defined groups and load new ones from config."""
conf = None
with contextlib.suppress(HomeAssistantError):
conf = await async_integration_yaml_config(hass, DOMAIN)
if conf 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) -> None:
"""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: HomeAssistant, config: ConfigType) -> bool:
"""Process rest configuration."""
if DOMAIN not in config:
return True
refresh_coroutines: list[Coroutine[Any, Any, None]] = []
load_coroutines: list[Coroutine[Any, Any, None]] = []
rest_config: list[ConfigType] = config[DOMAIN]
for rest_idx, conf in enumerate(rest_config):
scan_interval: timedelta = conf.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
resource_template: template.Template | None = conf.get(CONF_RESOURCE_TEMPLATE)
rest = create_rest_data_from_config(hass, conf)
coordinator = _rest_coordinator(hass, rest, resource_template, scan_interval)
refresh_coroutines.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_coroutine = discovery.async_load_platform(
hass,
platform_domain,
DOMAIN,
{REST_IDX: rest_idx, PLATFORM_IDX: platform_idx},
config,
)
load_coroutines.append(load_coroutine)
if refresh_coroutines:
await asyncio.gather(*refresh_coroutines)
if load_coroutines:
await asyncio.gather(*load_coroutines)
return True
async def async_get_config_and_coordinator(
hass: HomeAssistant, platform_domain: str, discovery_info: DiscoveryInfoType
) -> tuple[ConfigType, DataUpdateCoordinator[None], RestData]:
"""Get the config and coordinator for the platform from discovery."""
shared_data = hass.data[DOMAIN][REST_DATA][discovery_info[REST_IDX]]
conf: ConfigType = hass.data[DOMAIN][platform_domain][discovery_info[PLATFORM_IDX]]
coordinator: DataUpdateCoordinator[None] = shared_data[COORDINATOR]
rest: RestData = shared_data[REST]
if rest.data is None:
await coordinator.async_request_refresh()
return conf, coordinator, rest
def _rest_coordinator(
hass: HomeAssistant,
rest: RestData,
resource_template: template.Template | None,
update_interval: timedelta,
) -> DataUpdateCoordinator[None]:
"""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: HomeAssistant, config: ConfigType) -> RestData:
"""Create RestData from config."""
resource: str | None = config.get(CONF_RESOURCE)
resource_template: template.Template | None = config.get(CONF_RESOURCE_TEMPLATE)
method: str = config[CONF_METHOD]
payload: str | None = config.get(CONF_PAYLOAD)
verify_ssl: bool = config[CONF_VERIFY_SSL]
username: str | None = config.get(CONF_USERNAME)
password: str | None = config.get(CONF_PASSWORD)
headers: dict[str, str] | None = config.get(CONF_HEADERS)
params: dict[str, str] | None = config.get(CONF_PARAMS)
timeout: int = config[CONF_TIMEOUT]
if resource_template is not None:
resource_template.hass = hass
resource = resource_template.async_render(parse_result=False)
if not resource:
raise HomeAssistantError("Resource not set for RestData")
2021-12-08 23:35:50 +01:00
template.attach(hass, headers)
template.attach(hass, params)
auth: httpx.DigestAuth | tuple[str, str] | None = None
if username and password:
if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
auth = httpx.DigestAuth(username, password)
else:
auth = (username, password)
return RestData(
hass, method, resource, auth, headers, params, payload, verify_ssl, timeout
)