Migrate Brother to use DataUpdateCoordinator (#32800)

* Use DataCoordinator to update Brother data

* Simplify error text

* Improve tests

* Rename DEFAULT_SCAN_INTERVAL to SCAN_INTERVAL

* Add entity_registry_enabled_default property

* Add quality_scale to manifest.json file

* Remove misleading coordinator.data.data
This commit is contained in:
Maciej Bieniek authored and GitHub committed 2020-03-17 22:21:56 -05:00
1 parent 609263e1bb
commit c1ceab09e5
6 files changed
+188 -68

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
"""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.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
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()
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):
"""Test for setup failure if connection to broker is missing."""
entry = MockConfigEntry(
domain=DOMAIN,
title="HL-L2340DW 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)
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()
assert hass.data[DOMAIN][entry.entry_id]
assert await hass.config_entries.async_unload(entry.entry_id)
assert not hass.data[DOMAIN]
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"
+16 -2
View File
@@ -8,10 +8,24 @@
"31010400000001",
"6f010400001d4c",
"81010400000050",
"8601040000000a"
"8601040000000a",
"7e01040000064b",
"7301040000064b",
"7401040000064b",
"7501040000064b",
"790104000023f0",
"7a0104000023f0",
"7b0104000023f0",
"800104000023f0"
],
"1.3.6.1.4.1.2435.2.3.9.1.1.7.0": "MFG:Brother;CMD:PJL,HBP,URF;MDL:HL-L2340DW series;CLS:PRINTER;CID:Brother Laser Type1;URF:W8,CP1,IS4-1,MT1-3-4-5-8,OB10,PQ4,RS300-600,V1.3,DM1;",
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.11.0": ["82010400002b06"],
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.11.0": [
"82010400002b06",
"a4010400004005",
"a5010400004005",
"a6010400004005",
"a7010400004005"
],
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.1.0": "0123456789",
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.4.5.2.0": "WAITING "
}