core/tests/components/nam/test_button.py

52 lines
1.7 KiB
Python

"""Test button of Nettigo Air Monitor integration."""
from unittest.mock import patch
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, ButtonDeviceClass
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util
from . import init_integration
async def test_button(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None:
"""Test states of the button."""
await init_integration(hass)
state = hass.states.get("button.nettigo_air_monitor_restart")
assert state
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_DEVICE_CLASS) == ButtonDeviceClass.RESTART
entry = entity_registry.async_get("button.nettigo_air_monitor_restart")
assert entry
assert entry.unique_id == "aa:bb:cc:dd:ee:ff-restart"
async def test_button_press(hass: HomeAssistant) -> None:
"""Test button press."""
await init_integration(hass)
now = dt_util.utcnow()
with (
patch(
"homeassistant.components.nam.NettigoAirMonitor.async_restart"
) as mock_restart,
patch("homeassistant.core.dt_util.utcnow", return_value=now),
):
await hass.services.async_call(
BUTTON_DOMAIN,
"press",
{ATTR_ENTITY_ID: "button.nettigo_air_monitor_restart"},
blocking=True,
)
await hass.async_block_till_done()
mock_restart.assert_called_once()
state = hass.states.get("button.nettigo_air_monitor_restart")
assert state
assert state.state == now.isoformat()