mirror of
https://github.com/esphome/esphome.git
synced 2025-09-11 15:52:20 +01:00
Update testing
This commit is contained in:
@@ -125,22 +125,12 @@ def validate_ids_and_references(config: ConfigType) -> ConfigType:
|
|||||||
|
|
||||||
# Validate device hash collisions and area references
|
# Validate device hash collisions and area references
|
||||||
device_hashes: dict[int, str] = {}
|
device_hashes: dict[int, str] = {}
|
||||||
for i, device in enumerate(config[CONF_DEVICES]):
|
for device in config[CONF_DEVICES]:
|
||||||
device_id: core.ID = device[CONF_ID]
|
device_id: core.ID = device[CONF_ID]
|
||||||
check_hash_collision(
|
check_hash_collision(
|
||||||
device_id, device_hashes, "Device", [CONF_DEVICES, device_id.id]
|
device_id, device_hashes, "Device", [CONF_DEVICES, device_id.id]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Validate area_id reference if present
|
|
||||||
if CONF_AREA_ID in device:
|
|
||||||
area_ref_id: core.ID = device[CONF_AREA_ID]
|
|
||||||
if area_ref_id.id not in area_ids:
|
|
||||||
raise cv.Invalid(
|
|
||||||
f"Device '{device[CONF_NAME]}' has an area_id '{area_ref_id.id}'"
|
|
||||||
" that does not exist.",
|
|
||||||
path=[CONF_DEVICES, i, CONF_AREA_ID],
|
|
||||||
)
|
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
@@ -200,12 +190,16 @@ DEVICE_SCHEMA = cv.Schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_area_config(config: dict | str) -> dict[str, str | core.ID]:
|
||||||
|
return cv.maybe_simple_value(AREA_SCHEMA, key=CONF_NAME)(config)
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_NAME): cv.valid_name,
|
cv.Required(CONF_NAME): cv.valid_name,
|
||||||
cv.Optional(CONF_FRIENDLY_NAME, ""): cv.string,
|
cv.Optional(CONF_FRIENDLY_NAME, ""): cv.string,
|
||||||
cv.Optional(CONF_AREA): cv.maybe_simple_value(AREA_SCHEMA, key=CONF_NAME),
|
cv.Optional(CONF_AREA): validate_area_config,
|
||||||
cv.Optional(CONF_COMMENT): cv.string,
|
cv.Optional(CONF_COMMENT): cv.string,
|
||||||
cv.Required(CONF_BUILD_PATH): cv.string,
|
cv.Required(CONF_BUILD_PATH): cv.string,
|
||||||
cv.Optional(CONF_PLATFORMIO_OPTIONS, default={}): cv.Schema(
|
cv.Optional(CONF_PLATFORMIO_OPTIONS, default={}): cv.Schema(
|
||||||
@@ -260,9 +254,12 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
validate_hostname,
|
validate_hostname,
|
||||||
validate_ids_and_references,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
FINAL_VALIDATE_SCHEMA = cv.All(validate_ids_and_references)
|
||||||
|
|
||||||
|
|
||||||
PRELOAD_CONFIG_SCHEMA = cv.Schema(
|
PRELOAD_CONFIG_SCHEMA = cv.Schema(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_NAME): cv.valid_name,
|
cv.Required(CONF_NAME): cv.valid_name,
|
||||||
|
@@ -7,7 +7,7 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from esphome import config, config_validation as cv, yaml_util
|
from esphome import config, config_validation as cv, core, yaml_util
|
||||||
from esphome.config import Config
|
from esphome.config import Config
|
||||||
from esphome.const import CONF_AREA, CONF_AREAS, CONF_DEVICES
|
from esphome.const import CONF_AREA, CONF_AREAS, CONF_DEVICES
|
||||||
from esphome.core import CORE
|
from esphome.core import CORE
|
||||||
@@ -67,8 +67,9 @@ def test_validate_area_config_with_string() -> None:
|
|||||||
assert "id" in result
|
assert "id" in result
|
||||||
assert "name" in result
|
assert "name" in result
|
||||||
assert result["name"] == "Living Room"
|
assert result["name"] == "Living Room"
|
||||||
# ID should be based on slugified name
|
assert isinstance(result["id"], core.ID)
|
||||||
assert result["id"].id == "living_room"
|
assert result["id"].is_declaration
|
||||||
|
assert not result["id"].is_manual
|
||||||
|
|
||||||
|
|
||||||
def test_validate_area_config_with_dict() -> None:
|
def test_validate_area_config_with_dict() -> None:
|
||||||
@@ -157,10 +158,9 @@ def test_legacy_string_area(
|
|||||||
area = esphome_config[CONF_AREA]
|
area = esphome_config[CONF_AREA]
|
||||||
assert isinstance(area, dict)
|
assert isinstance(area, dict)
|
||||||
assert area["name"] == "Living Room"
|
assert area["name"] == "Living Room"
|
||||||
assert area["id"].id == "living_room"
|
assert isinstance(area["id"], core.ID)
|
||||||
|
assert area["id"].is_declaration
|
||||||
# Check for deprecation warning
|
assert not area["id"].is_manual
|
||||||
assert "Using 'area' as a string is deprecated" in caplog.text
|
|
||||||
|
|
||||||
|
|
||||||
def test_area_id_collision(
|
def test_area_id_collision(
|
||||||
@@ -205,8 +205,9 @@ def test_device_with_invalid_area_id(
|
|||||||
|
|
||||||
# Check for the specific error message in stdout
|
# Check for the specific error message in stdout
|
||||||
captured = capsys.readouterr()
|
captured = capsys.readouterr()
|
||||||
|
print(captured.out)
|
||||||
assert (
|
assert (
|
||||||
"Device 'Test Device' has an area_id 'nonexistent_area' that does not exist."
|
"Couldn't find ID 'nonexistent_area'. Please check you have defined an ID with that name in your configuration."
|
||||||
in captured.out
|
in captured.out
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user