"""Tests for the steamist switch."""

from datetime import timedelta
from unittest.mock import AsyncMock

import pytest

from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util

from . import (
    MOCK_ASYNC_GET_STATUS_ACTIVE,
    MOCK_ASYNC_GET_STATUS_INACTIVE,
    _async_setup_entry_with_status,
)

from tests.common import async_fire_time_changed


@pytest.mark.usefixtures("mock_aio_discovery")
async def test_steam_active(hass: HomeAssistant) -> None:
    """Test switches have expected values when steam is active."""
    client, _ = await _async_setup_entry_with_status(hass, MOCK_ASYNC_GET_STATUS_ACTIVE)
    assert len(hass.states.async_all("switch")) == 1
    assert hass.states.get("switch.steam_active").state == STATE_ON

    client.async_get_status = AsyncMock(return_value=MOCK_ASYNC_GET_STATUS_INACTIVE)
    await hass.services.async_call(
        SWITCH_DOMAIN,
        "turn_off",
        {ATTR_ENTITY_ID: "switch.steam_active"},
        blocking=True,
    )
    client.async_turn_off_steam.assert_called_once()
    async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=5))
    await hass.async_block_till_done()
    assert hass.states.get("switch.steam_active").state == STATE_OFF


@pytest.mark.usefixtures("mock_aio_discovery")
async def test_steam_inactive(hass: HomeAssistant) -> None:
    """Test switches have expected values when steam is not active."""
    client, _ = await _async_setup_entry_with_status(
        hass, MOCK_ASYNC_GET_STATUS_INACTIVE
    )

    assert len(hass.states.async_all("switch")) == 1
    assert hass.states.get("switch.steam_active").state == STATE_OFF

    client.async_get_status = AsyncMock(return_value=MOCK_ASYNC_GET_STATUS_ACTIVE)
    await hass.services.async_call(
        SWITCH_DOMAIN, "turn_on", {ATTR_ENTITY_ID: "switch.steam_active"}, blocking=True
    )
    client.async_turn_on_steam.assert_called_once()
    async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=5))
    await hass.async_block_till_done()
    assert hass.states.get("switch.steam_active").state == STATE_ON
