Files
localthings/tests/localthings/test_sensor.py
T
Marc Billow d6fef45029 feat(sensor): add disabled-by-default connection-mode diagnostic sensor
Exposes whether a device is currently in observe (push) or poll mode,
for troubleshooting the observe-mode feature without digging through
logs. Disabled by default since it's not everyday-use information.
2026-07-10 19:50:43 -05:00

83 lines
3.1 KiB
Python

"""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
from custom_components.localthings.observe import MODE_POLL
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}"
)
async def test_connection_mode_sensor_registered_disabled_by_default(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""The connection-mode diagnostic sensor is registered but disabled by
default — it's for troubleshooting, not everyday use."""
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)
mode_entries = [e for e in entries if e.unique_id.endswith('_connection_mode')]
assert len(mode_entries) == 1
entry = mode_entries[0]
assert entry.disabled_by is not None
assert entry.entity_category == 'diagnostic'
async def test_connection_mode_sensor_reflects_coordinator_mode(
hass: HomeAssistant, mock_entry, mock_coordinator_session
) -> None:
"""native_value reads coordinator.observe_mode directly, live."""
from custom_components.localthings.sensor import LocalThingsConnectionModeSensor
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
coordinator = hass.data[DOMAIN][mock_entry.entry_id]
sensor = LocalThingsConnectionModeSensor(coordinator)
assert sensor.native_value == MODE_POLL
coordinator._observe._set_mode('observe')
assert sensor.native_value == 'observe'