1
0
mirror of https://github.com/esphome/esphome.git synced 2025-07-14 11:03:30 +01:00
Files
.devcontainer
.github
.vscode
docker
esphome
script
tests
component_tests
components
dashboard
integration
fixtures
README.md
__init__.py
conftest.py
const.py
test_api_conditional_memory.py
test_api_custom_services.py
test_api_message_size_batching.py
test_api_reboot_timeout.py
test_api_vv_logging.py
test_areas_and_devices.py
test_batch_delay_zero_rapid_transitions.py
test_device_id_in_state.py
test_duplicate_entities.py
test_entity_icon.py
test_host_mode_basic.py
test_host_mode_batch_delay.py
test_host_mode_empty_string_options.py
test_host_mode_entity_fields.py
test_host_mode_fan_preset.py
test_host_mode_many_entities.py
test_host_mode_many_entities_multiple_connections.py
test_host_mode_noise_encryption.py
test_host_mode_reconnect.py
test_host_mode_sensor.py
test_large_message_batching.py
test_legacy_area.py
test_light_calls.py
test_loop_disable_enable.py
test_scheduler_bulk_cleanup.py
test_scheduler_defer_cancel.py
test_scheduler_defer_cancel_regular.py
test_scheduler_defer_fifo_simple.py
test_scheduler_defer_stress.py
test_scheduler_heap_stress.py
test_scheduler_null_name.py
test_scheduler_rapid_cancellation.py
test_scheduler_recursive_timeout.py
test_scheduler_simultaneous_callbacks.py
test_scheduler_string_lifetime.py
test_scheduler_string_name_stress.py
test_scheduler_string_test.py
types.py
script
test_build_components
test_packages
unit_tests
.gitignore
README.md
custom.h
dummy_main.cpp
pnglogo.png
.clang-format
.clang-tidy
.clang-tidy.hash
.coveragerc
.dockerignore
.editorconfig
.flake8
.gitattributes
.gitignore
.pre-commit-config.yaml
.yamllint
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Doxyfile
LICENSE
MANIFEST.in
README.md
netlify.toml
platformio.ini
pyproject.toml
requirements.txt
requirements_dev.txt
requirements_test.txt
sdkconfig.defaults

72 lines
2.6 KiB
Python

"""Integration test for Host mode with sensor."""
from __future__ import annotations
import asyncio
import aioesphomeapi
from aioesphomeapi import EntityState
import pytest
from .types import APIClientConnectedFactory, RunCompiledFunction
@pytest.mark.asyncio
async def test_host_mode_with_sensor(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""Test Host mode with a sensor component."""
# Write, compile and run the ESPHome device, then connect to API
loop = asyncio.get_running_loop()
async with run_compiled(yaml_config), api_client_connected() as client:
# Subscribe to state changes
states: dict[int, EntityState] = {}
sensor_future: asyncio.Future[EntityState] = loop.create_future()
def on_state(state: EntityState) -> None:
states[state.key] = state
# If this is our sensor with value 42.0, resolve the future
if (
isinstance(state, aioesphomeapi.SensorState)
and state.state == 42.0
and not sensor_future.done()
):
sensor_future.set_result(state)
client.subscribe_states(on_state)
# Wait for sensor with specific value (42.0) with timeout
try:
test_sensor_state = await asyncio.wait_for(sensor_future, timeout=5.0)
except asyncio.TimeoutError:
pytest.fail(
f"Sensor with value 42.0 not received within 5 seconds. "
f"Received states: {list(states.values())}"
)
# Verify the sensor state
assert test_sensor_state.state == 42.0
assert len(states) > 0, "No states received"
# Verify the optimized fields are working correctly
# Get entity info to check accuracy_decimals, state_class, etc.
entities, _ = await client.list_entities_services()
sensor_info: aioesphomeapi.SensorInfo | None = None
for entity in entities:
if isinstance(entity, aioesphomeapi.SensorInfo):
sensor_info = entity
break
assert sensor_info is not None, "Sensor entity info not found"
assert sensor_info.accuracy_decimals == 2, (
f"Expected accuracy_decimals=2, got {sensor_info.accuracy_decimals}"
)
assert sensor_info.state_class == 1, (
f"Expected state_class=1 (measurement), got {sensor_info.state_class}"
)
assert sensor_info.force_update is True, (
f"Expected force_update=True, got {sensor_info.force_update}"
)