Skip to content

Commit

Permalink
Drop custom config validator
Browse files Browse the repository at this point in the history
In preparation of changes in the config processing and error handling
for home-assistant 2023.11 this drops the custom config validator and
exposes the schema through CONFIG_SCHEMA so it is validated from core.

Properly raise exception to notify the user of failure.

see https://developers.home-assistant.io/blog/2023/11/27/config-validation-and-error-handling

fixes #322, fixes #325
  • Loading branch information
rautesamtr committed Dec 1, 2023
1 parent b14ced2 commit 649ab58
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 89 deletions.
64 changes: 46 additions & 18 deletions custom_components/thermal_comfort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@

import logging

from homeassistant.config import async_hass_config_yaml, async_process_component_config
import voluptuous as vol

from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, SERVICE_RELOAD
from homeassistant.core import Event, HomeAssistant, ServiceCall
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import discovery
from homeassistant.exceptions import ConfigValidationError, ServiceValidationError
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
from homeassistant.helpers.reload import async_reload_integration_platforms
from homeassistant.helpers.reload import (
async_integration_yaml_config,
async_reload_integration_platforms,
)
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import async_get_integration

from .config import OPTIONS_SCHEMA
from .config_flow import get_value
from .const import DOMAIN, PLATFORMS, UPDATE_LISTENER
from .sensor import (
Expand All @@ -28,6 +31,8 @@
CONF_POLL,
CONF_SCAN_INTERVAL,
CONF_TEMPERATURE_SENSOR,
SENSOR_OPTIONS_SCHEMA,
SENSOR_SCHEMA,
LegacySensorType,
SensorType,
)
Expand Down Expand Up @@ -104,6 +109,29 @@ def update_unique_id(entry: RegistryEntry):
return True


OPTIONS_SCHEMA = vol.Schema({}).extend(
SENSOR_OPTIONS_SCHEMA.schema,
extra=vol.REMOVE_EXTRA,
)

COMBINED_SCHEMA = vol.Schema(
{
vol.Optional(SENSOR_DOMAIN): vol.All(
cv.ensure_list, [SENSOR_SCHEMA]
),
}
).extend(OPTIONS_SCHEMA.schema)

CONFIG_SCHEMA = vol.Schema(
{
vol.Optional(DOMAIN): vol.All(
cv.ensure_list,
[COMBINED_SCHEMA],
)
},
extra=vol.ALLOW_EXTRA,
)

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the thermal_comfort integration."""
if DOMAIN in config:
Expand All @@ -112,22 +140,22 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def _reload_config(call: Event | ServiceCall) -> None:
"""Reload top-level + platforms."""
try:
unprocessed_conf = await async_hass_config_yaml(hass)
except HomeAssistantError as err:
_LOGGER.error(err)
return

conf = await async_process_component_config(
hass, unprocessed_conf, await async_get_integration(hass, DOMAIN)
)

if conf is None:
config_yaml = await async_integration_yaml_config(hass, DOMAIN, raise_on_failure=True)
except ConfigValidationError as ex:
raise ServiceValidationError(
str(ex),
translation_domain=ex.translation_domain,
translation_key=ex.translation_key,
translation_placeholders=ex.translation_placeholders,
) from ex

if config_yaml is None:
return

await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)

if DOMAIN in conf:
await _process_config(hass, conf)
if DOMAIN in config_yaml:
await _process_config(hass, config_yaml)

hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context)

Expand Down
53 changes: 0 additions & 53 deletions custom_components/thermal_comfort/config.py

This file was deleted.

22 changes: 4 additions & 18 deletions custom_components/thermal_comfort/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from homeassistant import util
from homeassistant.components.sensor import (
DOMAIN as SENSOR_DOMAIN,
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
Expand All @@ -24,7 +23,6 @@
ATTR_TEMPERATURE,
ATTR_UNIT_OF_MEASUREMENT,
CONF_ENTITY_PICTURE_TEMPLATE,
CONF_FRIENDLY_NAME,
CONF_ICON_TEMPLATE,
CONF_NAME,
CONF_SENSORS,
Expand Down Expand Up @@ -314,7 +312,7 @@ class ThomsDiscomfortPerception(StrEnum):

DEFAULT_SENSOR_TYPES = list(SENSOR_TYPES.keys())

PLATFORM_OPTIONS_SCHEMA = vol.Schema(
SENSOR_OPTIONS_SCHEMA = vol.Schema(
{
vol.Optional(CONF_POLL): cv.boolean,
vol.Optional(CONF_SCAN_INTERVAL): cv.time_period,
Expand All @@ -324,28 +322,16 @@ class ThomsDiscomfortPerception(StrEnum):
extra=vol.REMOVE_EXTRA,
)

LEGACY_SENSOR_SCHEMA = vol.Schema(
SENSOR_SCHEMA = vol.Schema(
{
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_TEMPERATURE_SENSOR): cv.entity_id,
vol.Required(CONF_HUMIDITY_SENSOR): cv.entity_id,
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
vol.Required(CONF_UNIQUE_ID): cv.string,
}
)

SENSOR_SCHEMA = LEGACY_SENSOR_SCHEMA.extend(
{
vol.Optional(CONF_NAME): cv.string,
}
).extend(PLATFORM_OPTIONS_SCHEMA.schema)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_SENSORS): cv.schema_with_slug_keys(SENSOR_SCHEMA),
}
).extend(PLATFORM_OPTIONS_SCHEMA.schema)
).extend(SENSOR_OPTIONS_SCHEMA.schema)


def compute_once_lock(sensor_type):
Expand Down

0 comments on commit 649ab58

Please sign in to comment.