Adjust BaseEditConfigView.__init__ (#126729)
This commit is contained in:
parent
46812777e2
commit
c5b4892596
5 changed files with 53 additions and 13 deletions
|
@ -6,10 +6,7 @@ from typing import Any
|
|||
import uuid
|
||||
|
||||
from homeassistant.components.automation import DOMAIN as AUTOMATION_DOMAIN
|
||||
from homeassistant.components.automation.config import (
|
||||
PLATFORM_SCHEMA,
|
||||
async_validate_config_item,
|
||||
)
|
||||
from homeassistant.components.automation.config import async_validate_config_item
|
||||
from homeassistant.config import AUTOMATION_CONFIG_PATH
|
||||
from homeassistant.const import CONF_ID, SERVICE_RELOAD
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
@ -48,7 +45,6 @@ def async_setup(hass: HomeAssistant) -> bool:
|
|||
"config",
|
||||
AUTOMATION_CONFIG_PATH,
|
||||
cv.string,
|
||||
PLATFORM_SCHEMA,
|
||||
post_write_hook=hook,
|
||||
data_validator=async_validate_config_item,
|
||||
)
|
||||
|
|
|
@ -47,7 +47,7 @@ def async_setup(hass: HomeAssistant) -> bool:
|
|||
"config",
|
||||
SCENE_CONFIG_PATH,
|
||||
cv.string,
|
||||
PLATFORM_SCHEMA,
|
||||
data_schema=PLATFORM_SCHEMA,
|
||||
post_write_hook=hook,
|
||||
)
|
||||
)
|
||||
|
|
|
@ -5,10 +5,7 @@ from __future__ import annotations
|
|||
from typing import Any
|
||||
|
||||
from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN
|
||||
from homeassistant.components.script.config import (
|
||||
SCRIPT_ENTITY_SCHEMA,
|
||||
async_validate_config_item,
|
||||
)
|
||||
from homeassistant.components.script.config import async_validate_config_item
|
||||
from homeassistant.config import SCRIPT_CONFIG_PATH
|
||||
from homeassistant.const import SERVICE_RELOAD
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
@ -45,7 +42,6 @@ def async_setup(hass: HomeAssistant) -> bool:
|
|||
"config",
|
||||
SCRIPT_CONFIG_PATH,
|
||||
cv.slug,
|
||||
SCRIPT_ENTITY_SCHEMA,
|
||||
post_write_hook=hook,
|
||||
data_validator=async_validate_config_item,
|
||||
)
|
||||
|
|
|
@ -33,9 +33,9 @@ class BaseEditConfigView[_DataT: (dict[str, dict[str, Any]], list[dict[str, Any]
|
|||
config_type: str,
|
||||
path: str,
|
||||
key_schema: Callable[[Any], str],
|
||||
data_schema: Callable[[dict[str, Any]], Any],
|
||||
*,
|
||||
post_write_hook: Callable[[str, str], Coroutine[Any, Any, None]] | None = None,
|
||||
data_schema: Callable[[dict[str, Any]], Any] | None = None,
|
||||
data_validator: Callable[
|
||||
[HomeAssistant, str, dict[str, Any]],
|
||||
Coroutine[Any, Any, dict[str, Any] | None],
|
||||
|
@ -51,6 +51,12 @@ class BaseEditConfigView[_DataT: (dict[str, dict[str, Any]], list[dict[str, Any]
|
|||
self.post_write_hook = post_write_hook
|
||||
self.data_validator = data_validator
|
||||
self.mutation_lock = asyncio.Lock()
|
||||
if (self.data_schema is None and self.data_validator is None) or (
|
||||
self.data_schema is not None and self.data_validator is not None
|
||||
):
|
||||
raise ValueError(
|
||||
"Must specify exactly one of data_schema or data_validator"
|
||||
)
|
||||
|
||||
def _empty_config(self) -> _DataT:
|
||||
"""Empty config if file not found."""
|
||||
|
@ -112,7 +118,8 @@ class BaseEditConfigView[_DataT: (dict[str, dict[str, Any]], list[dict[str, Any]
|
|||
if self.data_validator:
|
||||
await self.data_validator(hass, config_key, data)
|
||||
else:
|
||||
self.data_schema(data)
|
||||
# We either have a data_schema or a data_validator, ignore mypy
|
||||
self.data_schema(data) # type: ignore[misc]
|
||||
except (vol.Invalid, HomeAssistantError) as err:
|
||||
return self.json_message(
|
||||
f"Message malformed: {err}", HTTPStatus.BAD_REQUEST
|
||||
|
|
41
tests/components/config/test_view.py
Normal file
41
tests/components/config/test_view.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
"""Test config HTTP views."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from contextlib import AbstractContextManager, nullcontext as does_not_raise
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.config import view
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
async def _mock_validator(hass: HomeAssistant, key: str, data: dict) -> dict:
|
||||
"""Mock data validator."""
|
||||
return data
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("data_schema", "data_validator", "expected_result"),
|
||||
[
|
||||
(None, None, pytest.raises(ValueError)),
|
||||
(None, _mock_validator, does_not_raise()),
|
||||
(lambda x: x, None, does_not_raise()),
|
||||
(lambda x: x, _mock_validator, pytest.raises(ValueError)),
|
||||
],
|
||||
)
|
||||
async def test_view_requires_data_schema_or_validator(
|
||||
hass: HomeAssistant,
|
||||
data_schema: Callable | None,
|
||||
data_validator: Callable | None,
|
||||
expected_result: AbstractContextManager,
|
||||
) -> None:
|
||||
"""Test the view base class requires a schema or validator."""
|
||||
with expected_result:
|
||||
view.BaseEditConfigView(
|
||||
"test",
|
||||
"test",
|
||||
"test",
|
||||
lambda x: "",
|
||||
data_schema=data_schema,
|
||||
data_validator=data_validator,
|
||||
)
|
Loading…
Add table
Reference in a new issue