Tests improvements to the Brother integration (#32982)
* Add tests for states of the sensors * Revert async_update method * Tests improvement * Fix icon name * Tests improvement * Simplify tests * Test improvement * Patch the library instead of the coordinator * Suggested change * Remove return_value
This commit is contained in:
1 parent
0ed7bc3b8e
commit
3461f3a1ed
6 files changed
+375
-94
No files matched your search
@@ -1,38 +1,26 @@
|
||||
"""Test init of Brother integration."""
|
||||
from datetime import timedelta
|
||||
import json
|
||||
|
||||
from asynctest import patch
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.brother as brother
|
||||
from homeassistant.components.brother.const import DOMAIN
|
||||
from homeassistant.config_entries import (
|
||||
ENTRY_STATE_LOADED,
|
||||
ENTRY_STATE_NOT_LOADED,
|
||||
ENTRY_STATE_SETUP_RETRY,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_TYPE, STATE_UNAVAILABLE
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.brother import init_integration
|
||||
|
||||
|
||||
async def test_async_setup_entry(hass):
|
||||
"""Test a successful setup entry."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="HL-L2340DW 0123456789",
|
||||
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
|
||||
)
|
||||
with patch(
|
||||
"brother.Brother._get_data",
|
||||
return_value=json.loads(load_fixture("brother_printer_data.json")),
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
await init_integration(hass)
|
||||
|
||||
state = hass.states.get("sensor.hl_l2340dw_status")
|
||||
assert state is not None
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
assert state.state == "waiting"
|
||||
state = hass.states.get("sensor.hl_l2340dw_status")
|
||||
assert state is not None
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
assert state.state == "waiting"
|
||||
|
||||
|
||||
async def test_config_not_ready(hass):
|
||||
@@ -40,73 +28,25 @@ async def test_config_not_ready(hass):
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="HL-L2340DW 0123456789",
|
||||
unique_id="0123456789",
|
||||
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
|
||||
)
|
||||
with patch(
|
||||
"brother.Brother._get_data", side_effect=ConnectionError()
|
||||
), pytest.raises(ConfigEntryNotReady):
|
||||
await brother.async_setup_entry(hass, entry)
|
||||
|
||||
with patch("brother.Brother._get_data", side_effect=ConnectionError()):
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert entry.state == ENTRY_STATE_SETUP_RETRY
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test successful unload of entry."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="HL-L2340DW 0123456789",
|
||||
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
|
||||
)
|
||||
with patch(
|
||||
"brother.Brother._get_data",
|
||||
return_value=json.loads(load_fixture("brother_printer_data.json")),
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
entry = await init_integration(hass)
|
||||
|
||||
assert hass.data[DOMAIN][entry.entry_id]
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
assert not hass.data[DOMAIN]
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_availability(hass):
|
||||
"""Ensure that we mark the entities unavailable correctly when device is offline."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="HL-L2340DW 0123456789",
|
||||
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
|
||||
)
|
||||
with patch(
|
||||
"brother.Brother._get_data",
|
||||
return_value=json.loads(load_fixture("brother_printer_data.json")),
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.hl_l2340dw_status")
|
||||
assert state is not None
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
assert state.state == "waiting"
|
||||
|
||||
future = utcnow() + timedelta(minutes=5)
|
||||
with patch("brother.Brother._get_data", side_effect=ConnectionError()):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.hl_l2340dw_status")
|
||||
assert state is not None
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
future = utcnow() + timedelta(minutes=10)
|
||||
with patch(
|
||||
"brother.Brother._get_data",
|
||||
return_value=json.loads(load_fixture("brother_printer_data.json")),
|
||||
):
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.hl_l2340dw_status")
|
||||
assert state is not None
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
assert state.state == "waiting"
|
||||
assert entry.state == ENTRY_STATE_NOT_LOADED
|
||||
assert not hass.data.get(DOMAIN)
|
||||
Reference in new issue
Block a user