2020-03-20 09:21:43 +00:00
|
|
|
"""Test sensor of Brother integration."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2024-04-20 10:33:08 +00:00
|
|
|
from datetime import timedelta
|
2020-03-20 09:21:43 +00:00
|
|
|
import json
|
2024-04-20 10:33:08 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
|
|
|
from syrupy import SnapshotAssertion
|
2020-03-20 09:21:43 +00:00
|
|
|
|
2021-11-24 21:50:35 +00:00
|
|
|
from homeassistant.components.brother.const import DOMAIN
|
2024-04-20 10:33:08 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform
|
2022-09-18 21:28:17 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-03-09 13:24:34 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2020-03-20 09:21:43 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2024-04-20 10:33:08 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
2020-03-20 09:21:43 +00:00
|
|
|
|
2022-09-19 07:48:25 +00:00
|
|
|
from . import init_integration
|
|
|
|
|
2024-04-20 15:14:42 +00:00
|
|
|
from tests.common import async_fire_time_changed, load_fixture, snapshot_platform
|
2020-03-20 09:21:43 +00:00
|
|
|
|
|
|
|
|
2024-04-20 10:33:08 +00:00
|
|
|
async def test_sensors(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
entity_registry_enabled_by_default: None,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
) -> None:
|
2020-03-20 09:21:43 +00:00
|
|
|
"""Test states of the sensors."""
|
2024-04-20 10:33:08 +00:00
|
|
|
hass.config.set_time_zone("UTC")
|
|
|
|
freezer.move_to("2024-04-20 12:00:00+00:00")
|
2022-10-09 12:48:01 +00:00
|
|
|
|
2024-04-20 10:33:08 +00:00
|
|
|
with patch("homeassistant.components.brother.PLATFORMS", [Platform.SENSOR]):
|
|
|
|
entry = await init_integration(hass)
|
2020-11-22 21:42:16 +00:00
|
|
|
|
2024-04-20 15:14:42 +00:00
|
|
|
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
|
2020-11-22 21:42:16 +00:00
|
|
|
|
|
|
|
|
2022-09-18 21:28:17 +00:00
|
|
|
async def test_availability(hass: HomeAssistant) -> None:
|
2020-03-20 09:21:43 +00:00
|
|
|
"""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)
|
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
|
|
|
):
|
2020-03-20 09:21:43 +00:00
|
|
|
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)
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("brother.Brother.initialize"),
|
|
|
|
patch(
|
|
|
|
"brother.Brother._get_data",
|
|
|
|
return_value=json.loads(load_fixture("printer_data.json", "brother")),
|
|
|
|
),
|
2020-03-20 09:21:43 +00:00
|
|
|
):
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
2022-09-18 21:28:17 +00:00
|
|
|
async def test_manual_update_entity(hass: HomeAssistant) -> None:
|
2022-01-17 14:15:45 +00:00
|
|
|
"""Test manual update entity via service homeassistant/update_entity."""
|
2020-03-20 09:21:43 +00:00
|
|
|
await init_integration(hass)
|
|
|
|
|
2021-11-02 03:47:05 +00:00
|
|
|
data = json.loads(load_fixture("printer_data.json", "brother"))
|
2021-04-22 11:20:14 +00:00
|
|
|
|
2020-03-20 09:21:43 +00:00
|
|
|
await async_setup_component(hass, "homeassistant", {})
|
2021-04-22 11:20:14 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.brother.Brother.async_update", return_value=data
|
|
|
|
) as mock_update:
|
2020-03-20 09:21:43 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"homeassistant",
|
|
|
|
"update_entity",
|
|
|
|
{ATTR_ENTITY_ID: ["sensor.hl_l2340dw_status"]},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(mock_update.mock_calls) == 1
|
2022-09-18 21:28:17 +00:00
|
|
|
|
|
|
|
|
2023-11-12 18:45:30 +00:00
|
|
|
async def test_unique_id_migration(
|
|
|
|
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
|
|
|
) -> None:
|
2022-09-18 21:28:17 +00:00
|
|
|
"""Test states of the unique_id migration."""
|
|
|
|
|
2023-11-12 18:45:30 +00:00
|
|
|
entity_registry.async_get_or_create(
|
2022-09-18 21:28:17 +00:00
|
|
|
SENSOR_DOMAIN,
|
|
|
|
DOMAIN,
|
|
|
|
"0123456789_b/w_counter",
|
|
|
|
suggested_object_id="hl_l2340dw_b_w_counter",
|
|
|
|
disabled_by=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
await init_integration(hass)
|
|
|
|
|
2023-11-12 18:45:30 +00:00
|
|
|
entry = entity_registry.async_get("sensor.hl_l2340dw_b_w_counter")
|
2022-09-18 21:28:17 +00:00
|
|
|
assert entry
|
|
|
|
assert entry.unique_id == "0123456789_bw_counter"
|