2023-02-20 08:51:01 +00:00
|
|
|
"""Test init of APCUPSd integration."""
|
|
|
|
from collections import OrderedDict
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-02-26 06:57:31 +00:00
|
|
|
from homeassistant.components.apcupsd import DOMAIN
|
2023-02-20 08:51:01 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
|
|
|
from homeassistant.const import STATE_UNAVAILABLE
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2023-06-13 14:38:56 +00:00
|
|
|
from . import CONF_DATA, MOCK_MINIMAL_STATUS, MOCK_STATUS, async_init_integration
|
2023-02-20 08:51:01 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("status", (MOCK_STATUS, MOCK_MINIMAL_STATUS))
|
|
|
|
async def test_async_setup_entry(hass: HomeAssistant, status: OrderedDict) -> None:
|
|
|
|
"""Test a successful setup entry."""
|
|
|
|
# Minimal status does not contain "SERIALNO" field, which is used to determine the
|
|
|
|
# unique ID of this integration. But, the integration should work fine without it.
|
2023-06-13 14:38:56 +00:00
|
|
|
await async_init_integration(hass, status=status)
|
2023-02-20 08:51:01 +00:00
|
|
|
|
|
|
|
# Verify successful setup by querying the status sensor.
|
|
|
|
state = hass.states.get("binary_sensor.ups_online_status")
|
|
|
|
assert state is not None
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
|
|
|
assert state.state == "on"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_multiple_integrations(hass: HomeAssistant) -> None:
|
|
|
|
"""Test successful setup for multiple entries."""
|
|
|
|
# Load two integrations from two mock hosts.
|
2023-02-26 06:57:31 +00:00
|
|
|
status1 = MOCK_STATUS | {"LOADPCT": "15.0 Percent", "SERIALNO": "XXXXX1"}
|
|
|
|
status2 = MOCK_STATUS | {"LOADPCT": "16.0 Percent", "SERIALNO": "XXXXX2"}
|
2023-02-20 08:51:01 +00:00
|
|
|
entries = (
|
2023-06-13 14:38:56 +00:00
|
|
|
await async_init_integration(hass, host="test1", status=status1),
|
|
|
|
await async_init_integration(hass, host="test2", status=status2),
|
2023-02-20 08:51:01 +00:00
|
|
|
)
|
|
|
|
|
2023-02-26 06:57:31 +00:00
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
|
|
|
assert all(entry.state is ConfigEntryState.LOADED for entry in entries)
|
2023-02-20 08:51:01 +00:00
|
|
|
|
2023-02-26 06:57:31 +00:00
|
|
|
state1 = hass.states.get("sensor.ups_load")
|
|
|
|
state2 = hass.states.get("sensor.ups_load_2")
|
|
|
|
assert state1 is not None and state2 is not None
|
|
|
|
assert state1.state != state2.state
|
2023-02-20 08:51:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_connection_error(hass: HomeAssistant) -> None:
|
|
|
|
"""Test connection error during integration setup."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
version=1,
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="APCUPSd",
|
|
|
|
data=CONF_DATA,
|
|
|
|
source=SOURCE_USER,
|
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
with patch("apcaccess.status.parse", side_effect=OSError()), patch(
|
|
|
|
"apcaccess.status.get"
|
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
assert entry.state is ConfigEntryState.SETUP_ERROR
|
|
|
|
|
|
|
|
|
2023-02-21 08:25:05 +00:00
|
|
|
async def test_unload_remove(hass: HomeAssistant) -> None:
|
2023-02-20 08:51:01 +00:00
|
|
|
"""Test successful unload of entry."""
|
|
|
|
# Load two integrations from two mock hosts.
|
|
|
|
entries = (
|
2023-06-13 14:38:56 +00:00
|
|
|
await async_init_integration(hass, host="test1", status=MOCK_STATUS),
|
|
|
|
await async_init_integration(hass, host="test2", status=MOCK_MINIMAL_STATUS),
|
2023-02-20 08:51:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Assert they are loaded.
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
|
|
|
assert all(entry.state is ConfigEntryState.LOADED for entry in entries)
|
|
|
|
|
|
|
|
# Unload the first entry.
|
|
|
|
assert await hass.config_entries.async_unload(entries[0].entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert entries[0].state is ConfigEntryState.NOT_LOADED
|
|
|
|
assert entries[1].state is ConfigEntryState.LOADED
|
|
|
|
|
|
|
|
# Unload the second entry.
|
|
|
|
assert await hass.config_entries.async_unload(entries[1].entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert all(entry.state is ConfigEntryState.NOT_LOADED for entry in entries)
|
|
|
|
|
|
|
|
# Remove both entries.
|
|
|
|
for entry in entries:
|
|
|
|
await hass.config_entries.async_remove(entry.entry_id)
|
2023-02-26 06:57:31 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 0
|