Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
3.1 KiB
Python
Raw Permalink Normal View History

"""Test the identify button for HomeWizard."""
2023-10-30 14:07:42 +01:00
from unittest.mock import MagicMock
from homewizard_energy.errors import DisabledError, RequestError
2023-01-27 09:09:46 +01:00
import pytest
2023-10-30 14:07:42 +01:00
from syrupy.assertion import SnapshotAssertion
2022-11-30 10:28:28 +01:00
from homeassistant.components import button
2023-10-30 14:07:42 +01:00
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
2023-10-30 14:07:42 +01:00
from homeassistant.helpers import device_registry as dr, entity_registry as er
2023-10-30 14:07:42 +01:00
pytestmark = [
pytest.mark.usefixtures("init_integration"),
pytest.mark.freeze_time("2021-01-01 12:00:00"),
]
@pytest.mark.parametrize("device_fixture", ["SDM230", "SDM630", "HWE-KWH1", "HWE-KWH3"])
async def test_identify_button_entity_not_loaded_when_not_available(
2023-10-30 14:07:42 +01:00
hass: HomeAssistant,
) -> None:
"""Does not load button when device has no support for it."""
2023-10-30 14:07:42 +01:00
assert not hass.states.get("button.device_identify")
2023-10-30 14:07:42 +01:00
async def test_identify_button(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
mock_homewizardenergy: MagicMock,
snapshot: SnapshotAssertion,
) -> None:
"""Loads button when device has support."""
2023-10-30 14:07:42 +01:00
assert (state := hass.states.get("button.device_identify"))
assert snapshot == state
2023-10-30 14:07:42 +01:00
assert (entity_entry := entity_registry.async_get(state.entity_id))
assert snapshot == entity_entry
2023-10-30 14:07:42 +01:00
assert entity_entry.device_id
assert (device_entry := device_registry.async_get(entity_entry.device_id))
assert snapshot == device_entry
2023-10-30 14:07:42 +01:00
assert len(mock_homewizardenergy.identify.mock_calls) == 0
2022-11-30 10:28:28 +01:00
await hass.services.async_call(
button.DOMAIN,
button.SERVICE_PRESS,
2023-10-30 14:07:42 +01:00
{ATTR_ENTITY_ID: state.entity_id},
2022-11-30 10:28:28 +01:00
blocking=True,
)
2023-10-30 14:07:42 +01:00
assert len(mock_homewizardenergy.identify.mock_calls) == 1
2023-10-30 14:07:42 +01:00
assert (state := hass.states.get(state.entity_id))
assert state.state == "2021-01-01T12:00:00+00:00"
# Raise RequestError when identify is called
2023-10-30 14:07:42 +01:00
mock_homewizardenergy.identify.side_effect = RequestError()
with pytest.raises(
HomeAssistantError,
match=r"^An error occurred while communicating with your HomeWizard device$",
):
await hass.services.async_call(
button.DOMAIN,
button.SERVICE_PRESS,
2023-10-30 14:07:42 +01:00
{ATTR_ENTITY_ID: state.entity_id},
blocking=True,
)
2023-10-30 14:07:42 +01:00
assert len(mock_homewizardenergy.identify.mock_calls) == 2
2023-10-30 14:07:42 +01:00
assert (state := hass.states.get(state.entity_id))
assert state.state == "2021-01-01T12:00:00+00:00"
# Raise RequestError when identify is called
2023-10-30 14:07:42 +01:00
mock_homewizardenergy.identify.side_effect = DisabledError()
with pytest.raises(
HomeAssistantError,
match=r"^The local API is disabled$",
):
await hass.services.async_call(
button.DOMAIN,
button.SERVICE_PRESS,
2023-10-30 14:07:42 +01:00
{ATTR_ENTITY_ID: state.entity_id},
blocking=True,
)
2023-10-30 14:07:42 +01:00
assert len(mock_homewizardenergy.identify.mock_calls) == 3
assert (state := hass.states.get(state.entity_id))
assert state.state == "2021-01-01T12:00:00+00:00"