mirror of
https://github.com/esphome/esphome.git
synced 2026-11-10 23:25:58 +00:00
60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
"""Code generation tests for the enabled switch.
|
|
|
|
The client stays stopped until the hub has an enabled state: codegen sets it when
|
|
no enabled switch is configured, and the switch answers from its restore otherwise.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
import re
|
|
|
|
import pytest
|
|
|
|
GenerateMain = Callable[[str | Path], str]
|
|
ConfigPath = Callable[[str], Path]
|
|
|
|
|
|
def test_without_switch_codegen_enables_the_client(
|
|
generate_main: GenerateMain, component_config_path: ConfigPath
|
|
) -> None:
|
|
"""With no enabled switch, nothing else would start the client."""
|
|
main_cpp = generate_main(component_config_path("device_info_default.yaml"))
|
|
|
|
assert "->set_enabled(true);" in main_cpp
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("config", "restore_mode"),
|
|
[
|
|
("switch_enabled.yaml", "SWITCH_RESTORE_DEFAULT_ON"),
|
|
("switch_enabled_disabled_restore.yaml", "SWITCH_RESTORE_DISABLED"),
|
|
],
|
|
)
|
|
def test_switch_answers_the_hub(
|
|
generate_main: GenerateMain,
|
|
component_config_path: ConfigPath,
|
|
config: str,
|
|
restore_mode: str,
|
|
) -> None:
|
|
"""The hub waits for the switch's restore instead of being enabled by codegen."""
|
|
main_cpp = generate_main(component_config_path(config))
|
|
|
|
assert "->set_enabled(true);" not in main_cpp
|
|
assert f"sendspin_enabled->set_restore_mode(switch_::{restore_mode});" in main_cpp
|
|
assert "App.register_switch(sendspin_enabled," in main_cpp
|
|
|
|
|
|
def test_switch_sets_the_hub_then_publishes(
|
|
generate_main: GenerateMain, component_config_path: ConfigPath
|
|
) -> None:
|
|
"""Turning the switch sets the hub first, then publishes."""
|
|
main_cpp = generate_main(component_config_path("switch_enabled.yaml"))
|
|
|
|
# The generated body is the hub call followed directly by the publish
|
|
assert re.search(
|
|
r"->set_enabled\(state\);\s*entity->publish_state\(state\);", main_cpp
|
|
)
|
|
assert "->set_enabled_switch(sendspin_enabled);" in main_cpp
|