89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
"""Tests for entity creation and device info."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from homeassistant.const import Platform
|
||
from homeassistant.core import HomeAssistant
|
||
from homeassistant.helpers import device_registry as dr
|
||
from homeassistant.helpers import entity_registry as er
|
||
|
||
from custom_components.main_gate_controller.const import (
|
||
DOMAIN,
|
||
GATE_STATE_CLOSED,
|
||
MANUFACTURER,
|
||
MODEL,
|
||
)
|
||
|
||
|
||
async def test_setup_creates_entities_and_device(
|
||
hass: HomeAssistant, loaded_entry
|
||
):
|
||
"""A loaded entry creates one button, one sensor and one device."""
|
||
entity_registry = er.async_get(hass)
|
||
device_registry = dr.async_get(hass)
|
||
|
||
button_unique_id = f"{loaded_entry.entry_id}_open"
|
||
sensor_unique_id = f"{loaded_entry.entry_id}_status"
|
||
|
||
button_entity_id = entity_registry.async_get_entity_id(
|
||
Platform.BUTTON, DOMAIN, button_unique_id
|
||
)
|
||
sensor_entity_id = entity_registry.async_get_entity_id(
|
||
Platform.SENSOR, DOMAIN, sensor_unique_id
|
||
)
|
||
assert button_entity_id is not None
|
||
assert sensor_entity_id is not None
|
||
|
||
button_state = hass.states.get(button_entity_id)
|
||
sensor_state = hass.states.get(sensor_entity_id)
|
||
assert button_state is not None
|
||
assert sensor_state is not None
|
||
assert sensor_state.state == GATE_STATE_CLOSED
|
||
|
||
attrs = sensor_state.attributes
|
||
assert attrs["remaining_seconds"] is None
|
||
assert attrs["running"] is False
|
||
assert attrs["duration"] == 20.0
|
||
|
||
device = device_registry.async_get_device(identifiers={(DOMAIN, loaded_entry.entry_id)})
|
||
assert device is not None
|
||
assert device.name == "Main Gate"
|
||
assert device.manufacturer == MANUFACTURER
|
||
assert device.model == MODEL
|
||
|
||
|
||
async def test_setup_states_remain_consistent_after_restart(hass: HomeAssistant):
|
||
"""After a (logical) restart the state is closed and not running."""
|
||
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
||
|
||
from tests.conftest import make_entry_data
|
||
|
||
entry = MockConfigEntry(
|
||
domain=DOMAIN,
|
||
title="Main Gate",
|
||
data=make_entry_data(switch_entity_id="switch.main_gate_relay", name="Main Gate"),
|
||
unique_id="switch.main_gate_relay",
|
||
)
|
||
entry.add_to_hass(hass)
|
||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||
await hass.async_block_till_done()
|
||
|
||
sensor_state = hass.states.get(f"sensor.{entry.title.lower().replace(' ', '_')}_status")
|
||
assert sensor_state is None or sensor_state.state == GATE_STATE_CLOSED
|
||
|
||
|
||
async def test_unload_entry(hass: HomeAssistant, loaded_entry):
|
||
"""Unloading removes entities and clears the runtime data attribute."""
|
||
# Capture the runtime data reference before unload – HA deletes the
|
||
# attribute on unload by design (see homeassistant.config_entries).
|
||
controller_before = loaded_entry.runtime_data
|
||
assert controller_before is not None
|
||
|
||
assert await hass.config_entries.async_unload(loaded_entry.entry_id)
|
||
await hass.async_block_till_done()
|
||
|
||
# The cycle was cancelled; the controller is reset to a safe closed state.
|
||
assert controller_before.is_running is False
|
||
assert controller_before.status == GATE_STATE_CLOSED
|
||
assert not hasattr(loaded_entry, "runtime_data")
|