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:
Maciej Bieniek 2020-03-20 10:21:43 +01:00 committed by GitHub
parent 0ed7bc3b8e
commit 3461f3a1ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 375 additions and 94 deletions

View File

@ -48,7 +48,7 @@ PRINTER_TYPES = ["laser", "ink"]
SENSOR_TYPES = { SENSOR_TYPES = {
ATTR_STATUS: { ATTR_STATUS: {
ATTR_ICON: "icon:mdi:printer", ATTR_ICON: "mdi:printer",
ATTR_LABEL: ATTR_STATUS.title(), ATTR_LABEL: ATTR_STATUS.title(),
ATTR_UNIT: None, ATTR_UNIT: None,
}, },

View File

@ -147,3 +147,7 @@ class BrotherPrinterSensor(Entity):
async def async_will_remove_from_hass(self): async def async_will_remove_from_hass(self):
"""Disconnect from update signal.""" """Disconnect from update signal."""
self.coordinator.async_remove_listener(self.async_write_ha_state) self.coordinator.async_remove_listener(self.async_write_ha_state)
async def async_update(self):
"""Update Brother entity."""
await self.coordinator.async_request_refresh()

View File

@ -1 +1,28 @@
"""Tests for Brother Printer.""" """Tests for Brother Printer integration."""
import json
from asynctest import patch
from homeassistant.components.brother.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_TYPE
from tests.common import MockConfigEntry, load_fixture
async def init_integration(hass) -> MockConfigEntry:
"""Set up the Brother integration in Home Assistant."""
entry = MockConfigEntry(
domain=DOMAIN,
title="HL-L2340DW 0123456789",
unique_id="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)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry

View File

@ -1,38 +1,26 @@
"""Test init of Brother integration.""" """Test init of Brother integration."""
from datetime import timedelta
import json
from asynctest import patch from asynctest import patch
import pytest
import homeassistant.components.brother as brother
from homeassistant.components.brother.const import DOMAIN 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.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): async def test_async_setup_entry(hass):
"""Test a successful setup entry.""" """Test a successful setup entry."""
entry = MockConfigEntry( await init_integration(hass)
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") state = hass.states.get("sensor.hl_l2340dw_status")
assert state is not None assert state is not None
assert state.state != STATE_UNAVAILABLE assert state.state != STATE_UNAVAILABLE
assert state.state == "waiting" assert state.state == "waiting"
async def test_config_not_ready(hass): async def test_config_not_ready(hass):
@ -40,73 +28,25 @@ async def test_config_not_ready(hass):
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
title="HL-L2340DW 0123456789", title="HL-L2340DW 0123456789",
unique_id="0123456789",
data={CONF_HOST: "localhost", CONF_TYPE: "laser"}, data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
) )
with patch(
"brother.Brother._get_data", side_effect=ConnectionError() with patch("brother.Brother._get_data", side_effect=ConnectionError()):
), pytest.raises(ConfigEntryNotReady): entry.add_to_hass(hass)
await brother.async_setup_entry(hass, entry) await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_unload_entry(hass): async def test_unload_entry(hass):
"""Test successful unload of entry.""" """Test successful unload of entry."""
entry = MockConfigEntry( entry = await init_integration(hass)
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 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 await hass.config_entries.async_unload(entry.entry_id)
assert not hass.data[DOMAIN] await hass.async_block_till_done()
assert entry.state == ENTRY_STATE_NOT_LOADED
async def test_availability(hass): assert not hass.data.get(DOMAIN)
"""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"

View File

@ -0,0 +1,266 @@
"""Test sensor of Brother integration."""
from datetime import timedelta
import json
from asynctest import patch
from homeassistant.components.brother.const import UNIT_PAGES
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNAVAILABLE,
TIME_DAYS,
UNIT_PERCENTAGE,
)
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed, load_fixture
from tests.components.brother import init_integration
ATTR_REMAINING_PAGES = "remaining_pages"
ATTR_COUNTER = "counter"
async def test_sensors(hass):
"""Test states of the sensors."""
await init_integration(hass)
registry = await hass.helpers.entity_registry.async_get_registry()
state = hass.states.get("sensor.hl_l2340dw_status")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:printer"
assert state.state == "waiting"
entry = registry.async_get("sensor.hl_l2340dw_status")
assert entry
assert entry.unique_id == "0123456789_status"
state = hass.states.get("sensor.hl_l2340dw_black_toner_remaining")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:printer-3d-nozzle"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "75"
entry = registry.async_get("sensor.hl_l2340dw_black_toner_remaining")
assert entry
assert entry.unique_id == "0123456789_black_toner_remaining"
state = hass.states.get("sensor.hl_l2340dw_cyan_toner_remaining")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:printer-3d-nozzle"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "10"
entry = registry.async_get("sensor.hl_l2340dw_cyan_toner_remaining")
assert entry
assert entry.unique_id == "0123456789_cyan_toner_remaining"
state = hass.states.get("sensor.hl_l2340dw_magenta_toner_remaining")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:printer-3d-nozzle"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "8"
entry = registry.async_get("sensor.hl_l2340dw_magenta_toner_remaining")
assert entry
assert entry.unique_id == "0123456789_magenta_toner_remaining"
state = hass.states.get("sensor.hl_l2340dw_yellow_toner_remaining")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:printer-3d-nozzle"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "2"
entry = registry.async_get("sensor.hl_l2340dw_yellow_toner_remaining")
assert entry
assert entry.unique_id == "0123456789_yellow_toner_remaining"
state = hass.states.get("sensor.hl_l2340dw_drum_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:chart-donut"
assert state.attributes.get(ATTR_REMAINING_PAGES) == 11014
assert state.attributes.get(ATTR_COUNTER) == 986
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "92"
entry = registry.async_get("sensor.hl_l2340dw_drum_remaining_life")
assert entry
assert entry.unique_id == "0123456789_drum_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_black_drum_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:chart-donut"
assert state.attributes.get(ATTR_REMAINING_PAGES) == 16389
assert state.attributes.get(ATTR_COUNTER) == 1611
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "92"
entry = registry.async_get("sensor.hl_l2340dw_black_drum_remaining_life")
assert entry
assert entry.unique_id == "0123456789_black_drum_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_cyan_drum_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:chart-donut"
assert state.attributes.get(ATTR_REMAINING_PAGES) == 16389
assert state.attributes.get(ATTR_COUNTER) == 1611
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "92"
entry = registry.async_get("sensor.hl_l2340dw_cyan_drum_remaining_life")
assert entry
assert entry.unique_id == "0123456789_cyan_drum_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_magenta_drum_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:chart-donut"
assert state.attributes.get(ATTR_REMAINING_PAGES) == 16389
assert state.attributes.get(ATTR_COUNTER) == 1611
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "92"
entry = registry.async_get("sensor.hl_l2340dw_magenta_drum_remaining_life")
assert entry
assert entry.unique_id == "0123456789_magenta_drum_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_yellow_drum_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:chart-donut"
assert state.attributes.get(ATTR_REMAINING_PAGES) == 16389
assert state.attributes.get(ATTR_COUNTER) == 1611
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "92"
entry = registry.async_get("sensor.hl_l2340dw_yellow_drum_remaining_life")
assert entry
assert entry.unique_id == "0123456789_yellow_drum_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_fuser_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:water-outline"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "97"
entry = registry.async_get("sensor.hl_l2340dw_fuser_remaining_life")
assert entry
assert entry.unique_id == "0123456789_fuser_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_belt_unit_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:current-ac"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "97"
entry = registry.async_get("sensor.hl_l2340dw_belt_unit_remaining_life")
assert entry
assert entry.unique_id == "0123456789_belt_unit_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_pf_kit_1_remaining_life")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:printer-3d"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PERCENTAGE
assert state.state == "98"
entry = registry.async_get("sensor.hl_l2340dw_pf_kit_1_remaining_life")
assert entry
assert entry.unique_id == "0123456789_pf_kit_1_remaining_life"
state = hass.states.get("sensor.hl_l2340dw_page_counter")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:file-document-outline"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PAGES
assert state.state == "986"
entry = registry.async_get("sensor.hl_l2340dw_page_counter")
assert entry
assert entry.unique_id == "0123456789_page_counter"
state = hass.states.get("sensor.hl_l2340dw_duplex_unit_pages_counter")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:file-document-outline"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PAGES
assert state.state == "538"
entry = registry.async_get("sensor.hl_l2340dw_duplex_unit_pages_counter")
assert entry
assert entry.unique_id == "0123456789_duplex_unit_pages_counter"
state = hass.states.get("sensor.hl_l2340dw_b_w_counter")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:file-document-outline"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PAGES
assert state.state == "709"
entry = registry.async_get("sensor.hl_l2340dw_b_w_counter")
assert entry
assert entry.unique_id == "0123456789_b/w_counter"
state = hass.states.get("sensor.hl_l2340dw_color_counter")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:file-document-outline"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UNIT_PAGES
assert state.state == "902"
entry = registry.async_get("sensor.hl_l2340dw_color_counter")
assert entry
assert entry.unique_id == "0123456789_color_counter"
state = hass.states.get("sensor.hl_l2340dw_uptime")
assert state
assert state.attributes.get(ATTR_ICON) == "mdi:timer"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TIME_DAYS
assert state.state == "48"
entry = registry.async_get("sensor.hl_l2340dw_uptime")
assert entry
assert entry.unique_id == "0123456789_uptime"
async def test_availability(hass):
"""Ensure that we mark the entities unavailable correctly when device is offline."""
await init_integration(hass)
state = hass.states.get("sensor.hl_l2340dw_status")
assert state
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
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
assert state.state != STATE_UNAVAILABLE
assert state.state == "waiting"
async def test_manual_update_entity(hass):
"""Test manual update entity via service homeasasistant/update_entity."""
await init_integration(hass)
await async_setup_component(hass, "homeassistant", {})
with patch("homeassistant.components.brother.Brother.async_update") as mock_update:
await hass.services.async_call(
"homeassistant",
"update_entity",
{ATTR_ENTITY_ID: ["sensor.hl_l2340dw_status"]},
blocking=True,
)
assert len(mock_update.mock_calls) == 1

View File

@ -1,31 +1,75 @@
{ {
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.10.0": ["000104000003da"], "1.3.6.1.2.1.1.3.0": "413613515",
"1.3.6.1.2.1.43.10.2.1.4.1.1": "986",
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.10.0": [
"000104000003da",
"010104000002c5",
"02010400000386",
"0601040000021a",
"0701040000012d",
"080104000000ed"
],
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.17.0": "1.17", "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.17.0": "1.17",
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.8.0": [ "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.8.0": [
"63010400000001",
"110104000003da", "110104000003da",
"410104000023f0",
"31010400000001", "31010400000001",
"32010400000001",
"33010400000002",
"34010400000002",
"35010400000001",
"410104000023f0",
"54010400000001",
"55010400000001",
"63010400000001",
"68010400000001",
"690104000025e4",
"6a0104000025e4",
"6d010400002648",
"6f010400001d4c", "6f010400001d4c",
"81010400000050", "700104000003e8",
"8601040000000a", "71010400000320",
"7e01040000064b", "720104000000c8",
"7301040000064b", "7301040000064b",
"7401040000064b", "7401040000064b",
"7501040000064b", "7501040000064b",
"76010400000001",
"77010400000001",
"78010400000001",
"790104000023f0", "790104000023f0",
"7a0104000023f0", "7a0104000023f0",
"7b0104000023f0", "7b0104000023f0",
"800104000023f0" "7e01040000064b",
"800104000023f0",
"81010400000050",
"8201040000000a",
"8301040000000a",
"8401040000000a",
"8601040000000a"
], ],
"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.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": [ "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.11.0": [
"7301040000bd05",
"7701040000be65",
"82010400002b06", "82010400002b06",
"8801040000bd34",
"a4010400004005", "a4010400004005",
"a5010400004005", "a5010400004005",
"a6010400004005", "a6010400004005",
"a7010400004005" "a7010400004005"
], ],
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.21.0": [
"00002302000025",
"00020016010200",
"00210200022202",
"020000a1040000"
],
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.20.0": [
"00a40100a50100",
"0100a301008801",
"01017301007701",
"870100a10100a2",
"a60100a70100a0"
],
"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.5.1.0": "0123456789",
"1.3.6.1.4.1.2435.2.3.9.4.2.1.5.4.5.2.0": "WAITING " "1.3.6.1.4.1.2435.2.3.9.4.2.1.5.4.5.2.0": "WAITING "
} }