test: scaffold pytest-homeassistant-custom-component test harness

Adds HA component tests covering the localthings integration layer
(previously only the samsung_appliance library was tested):

- pyproject.toml: asyncio_mode=auto for async HA test patterns
- tests/localthings/conftest.py: hass_config_dir override, mock fixtures
  for probe and coordinator session, fridge fixture loader
- test_config_flow.py: first-device form, second-device cred reuse,
  happy path, cannot_connect error, duplicate-device abort (5 tests)
- test_coordinator.py: discovery on first refresh, device_info populated,
  idle interval default, UpdateFailed on persistent poll error (4 tests)
- test_sensor.py: sensors registered, sensor state from coordinator (2 tests)

Total: 70 library + 11 component = 81 tests
This commit is contained in:
Marc Billow committed 2026-06-30 16:34:10 -05:00
1 parent ee779c955b
commit 0bb27a9204
6 files changed
+321

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
"""Tests for the sensor platform."""
from __future__ import annotations
import pytest
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from custom_components.localthings.const import DOMAIN
async def test_sensors_registered(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""Sensors are registered for all SensorDesc entities in the fridge fixture."""
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
ent_reg = er.async_get(hass)
entries = er.async_entries_for_config_entry(ent_reg, mock_entry.entry_id)
sensor_entries = [e for e in entries if e.domain == 'sensor']
assert sensor_entries, "Expected at least one sensor entity"
async def test_sensor_state_from_coordinator(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""A temperature sensor reads its value from coordinator.data.
Entity IDs are auto-generated by HA and don't embed capability path
segments, so we filter by the mdi:thermometer icon that the fridge
capability assigns to current-temperature sensors.
"""
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
states = hass.states.async_all('sensor')
temp_states = [
s for s in states
if s.attributes.get('icon') == 'mdi:thermometer'
]
assert temp_states, (
f"No thermometer sensor found. Sensors: {[s.entity_id for s in states]}"
)
state = temp_states[0]
assert state.state not in ('unknown', 'unavailable'), (
f"Sensor state is {state.state}"
)