2020-03-18 03:21:56 +00:00
|
|
|
"""Test init of Brother integration."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2022-09-18 21:28:17 +00:00
|
|
|
from brother import SnmpError
|
|
|
|
import pytest
|
|
|
|
|
2020-03-18 03:21:56 +00:00
|
|
|
from homeassistant.components.brother.const import DOMAIN
|
2021-05-20 17:19:20 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2020-03-18 03:21:56 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_TYPE, STATE_UNAVAILABLE
|
2022-09-18 21:28:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-03-18 03:21:56 +00:00
|
|
|
|
2022-09-19 07:48:25 +00:00
|
|
|
from . import init_integration
|
|
|
|
|
2020-03-20 09:21:43 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2020-03-18 03:21:56 +00:00
|
|
|
|
|
|
|
|
2022-09-18 21:28:17 +00:00
|
|
|
async def test_async_setup_entry(hass: HomeAssistant) -> None:
|
2020-03-18 03:21:56 +00:00
|
|
|
"""Test a successful setup entry."""
|
2020-03-20 09:21:43 +00:00
|
|
|
await init_integration(hass)
|
2020-03-18 03:21:56 +00:00
|
|
|
|
2020-03-20 09:21:43 +00:00
|
|
|
state = hass.states.get("sensor.hl_l2340dw_status")
|
|
|
|
assert state is not None
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
|
|
|
assert state.state == "waiting"
|
2020-03-18 03:21:56 +00:00
|
|
|
|
|
|
|
|
2022-09-18 21:28:17 +00:00
|
|
|
async def test_config_not_ready(hass: HomeAssistant) -> None:
|
2020-03-18 03:21:56 +00:00
|
|
|
"""Test for setup failure if connection to broker is missing."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="HL-L2340DW 0123456789",
|
2020-03-20 09:21:43 +00:00
|
|
|
unique_id="0123456789",
|
2020-03-18 03:21:56 +00:00
|
|
|
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
|
|
|
|
)
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("brother.Brother.initialize"),
|
|
|
|
patch("brother.Brother._get_data", side_effect=ConnectionError()),
|
2022-09-18 21:28:17 +00:00
|
|
|
):
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("exc", [(SnmpError("SNMP Error")), (ConnectionError)])
|
|
|
|
async def test_error_on_init(hass: HomeAssistant, exc: Exception) -> None:
|
|
|
|
"""Test for error on init."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="HL-L2340DW 0123456789",
|
|
|
|
unique_id="0123456789",
|
|
|
|
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("brother.Brother.initialize", side_effect=exc):
|
2020-03-18 03:21:56 +00:00
|
|
|
entry.add_to_hass(hass)
|
2020-03-20 09:21:43 +00:00
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
2020-03-18 03:21:56 +00:00
|
|
|
|
|
|
|
|
2022-09-18 21:28:17 +00:00
|
|
|
async def test_unload_entry(hass: HomeAssistant) -> None:
|
2020-03-20 09:21:43 +00:00
|
|
|
"""Test successful unload of entry."""
|
|
|
|
entry = await init_integration(hass)
|
2020-03-18 03:21:56 +00:00
|
|
|
|
2020-03-20 09:21:43 +00:00
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.LOADED
|
2020-03-18 03:21:56 +00:00
|
|
|
|
2024-05-19 09:33:21 +00:00
|
|
|
with patch("homeassistant.components.brother.lcd.unconfigure") as mock_unconfigure:
|
|
|
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert mock_unconfigure.called
|
2020-03-18 03:21:56 +00:00
|
|
|
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.NOT_LOADED
|
2020-03-20 09:21:43 +00:00
|
|
|
assert not hass.data.get(DOMAIN)
|