2022-07-06 14:29:53 +00:00
|
|
|
"""Test button of NextDNS integration."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2022-07-06 14:29:53 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2024-04-20 10:31:20 +00:00
|
|
|
from syrupy import SnapshotAssertion
|
|
|
|
|
2022-07-06 14:29:53 +00:00
|
|
|
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
2024-04-20 10:31:20 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
2022-07-22 22:58:48 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-06 14:29:53 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
|
|
|
|
from . import init_integration
|
|
|
|
|
|
|
|
|
2024-04-20 10:31:20 +00:00
|
|
|
async def test_button(
|
|
|
|
hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
|
|
|
|
) -> None:
|
2022-07-06 14:29:53 +00:00
|
|
|
"""Test states of the button."""
|
2024-04-20 10:31:20 +00:00
|
|
|
with patch("homeassistant.components.nextdns.PLATFORMS", [Platform.BUTTON]):
|
|
|
|
entry = await init_integration(hass)
|
2022-07-06 14:29:53 +00:00
|
|
|
|
2024-04-20 10:31:20 +00:00
|
|
|
entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
2022-07-06 14:29:53 +00:00
|
|
|
|
2024-04-20 10:31:20 +00:00
|
|
|
assert entity_entries
|
|
|
|
for entity_entry in entity_entries:
|
|
|
|
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
|
|
|
|
assert (state := hass.states.get(entity_entry.entity_id))
|
|
|
|
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
|
2022-07-06 14:29:53 +00:00
|
|
|
|
|
|
|
|
2022-07-22 22:58:48 +00:00
|
|
|
async def test_button_press(hass: HomeAssistant) -> None:
|
2022-07-06 14:29:53 +00:00
|
|
|
"""Test button press."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
now = dt_util.utcnow()
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("homeassistant.components.nextdns.NextDns.clear_logs") as mock_clear_logs,
|
|
|
|
patch("homeassistant.core.dt_util.utcnow", return_value=now),
|
|
|
|
):
|
2022-07-06 14:29:53 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
BUTTON_DOMAIN,
|
|
|
|
"press",
|
|
|
|
{ATTR_ENTITY_ID: "button.fake_profile_clear_logs"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
mock_clear_logs.assert_called_once()
|
|
|
|
|
|
|
|
state = hass.states.get("button.fake_profile_clear_logs")
|
|
|
|
assert state
|
|
|
|
assert state.state == now.isoformat()
|