"""Tests for service."""
from pymultimatic.model import QuickModes, QuickVeto
import pytest
import voluptuous

from homeassistant.components import multimatic
from homeassistant.components.multimatic import DOMAIN

from tests.components.multimatic import (
    SystemManagerMock,
    active_holiday_mode,
    call_service,
    setup_multimatic,
)


@pytest.fixture(autouse=True)
def fixture_only_climate(mock_manager):
    """Mock multimatic to only handle services."""
    orig_platforms = multimatic.PLATFORMS
    multimatic.PLATFORMS = ["climate"]
    yield
    multimatic.PLATFORMS = orig_platforms


async def test_valid_config(hass):
    """Test setup with valid config."""
    assert await setup_multimatic(hass)
    assert len(hass.services.async_services()[DOMAIN]) == 9


async def test_remove_quick_mode(hass):
    """Test remove existing quick mode."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_quick_mode"] = QuickModes.ONE_DAY_AT_HOME
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(hass, "multimatic", "remove_quick_mode", None)
    SystemManagerMock.instance.remove_quick_mode.assert_called_once_with()


async def test_remove_quick_mode_wrong_data(hass):
    """Test remove existing quick mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "remove_quick_mode", {"test": "boom"})


async def test_remove_holiday_mode(hass):
    """Remove existing holiday mode."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_holiday_mode"] = active_holiday_mode()
    assert await setup_multimatic(hass)
    await call_service(hass, "multimatic", "remove_holiday_mode", None)
    SystemManagerMock.instance.remove_holiday_mode.assert_called_once_with()


async def test_remove_holiday_mode_wrong_data(hass):
    """Remove existing holiday mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "remove_holiday_mode", {"test": "boom"})


async def test_set_quick_mode(hass):
    """Set quick mode."""
    assert await setup_multimatic(hass)
    await call_service(hass, "multimatic", "set_quick_mode", {"quick_mode": "QM_PARTY"})
    SystemManagerMock.instance.set_quick_mode.assert_called_once_with(QuickModes.PARTY)


async def test_set_quick_mode_wrong_data(hass):
    """Set quick mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "set_quick_mode", {"test": "boom"})


async def test_set_holiday_mode_correct_date(hass):
    """Test holiday mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        "multimatic",
        "set_holiday_mode",
        {"start_date": "2010-10-25", "end_date": "2010-10-26", "temperature": "10"},
    )
    SystemManagerMock.instance.set_holiday_mode.assert_called_once()


async def test_set_holiday_mode_wrong_date_format(hass):
    """Test holiday mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        "multimatic",
        "set_holiday_mode",
        {
            "start_date": "2010-10-25T00:00:00.000Z",
            "end_date": "2010-10-26T00:00:00.000Z",
            "temperature": "10",
        },
    )
    SystemManagerMock.instance.set_holiday_mode.assert_called_once()


async def test_set_holiday_mode_wrong_data(hass):
    """Test holiday mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "set_holiday_mode", {"test": "boom"})


async def test_remove_quick_veto_wrong_data(hass):
    """Remove quick veto with wrong entity id."""
    assert await setup_multimatic(hass)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.test123"}
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_not_called()
    SystemManagerMock.instance.remove_zone_quick_veto.assert_not_called()


async def test_remove_quick_veto_room(hass):
    """Remove quick veto with already existing quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_rooms"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.room_1"}
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_called_once_with("1")


async def test_no_remove_quick_veto_room(hass):
    """Remove quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.room_1"}
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_not_called()


async def test_no_remove_quick_veto_zone(hass):
    """Remove quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.zone_1"}
    )
    SystemManagerMock.instance.remove_zone_quick_veto.assert_not_called()


async def test_remove_quick_veto_zone(hass):
    """Remove quick veto with already existing quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_zones"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.zone_1"}
    )
    SystemManagerMock.instance.remove_zone_quick_veto.assert_called_once_with("zone_1")


async def test_set_quick_veto_room(hass):
    """Set quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.room_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_room_quick_veto.assert_called_once_with(
        "1", QuickVeto(300, 25.0)
    )


async def test_set_quick_veto_room_with_quick_veto(hass):
    """Set quick veto with quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_rooms"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.room_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_room_quick_veto.assert_called_once_with(
        "1", QuickVeto(300, 25.0)
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_called_once_with("1")


async def test_set_quick_veto_zone_with_quick_veto(hass):
    """Set quick veto with quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_zones"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.zone_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_zone_quick_veto.assert_called_once_with(
        "zone_1", QuickVeto(300, 25.0)
    )
    SystemManagerMock.instance.remove_zone_quick_veto.assert_called_once_with("zone_1")


async def test_set_quick_veto_zone(hass):
    """Set quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.zone_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_zone_quick_veto.assert_called_once_with(
        "zone_1", QuickVeto(300, 25.0)
    )


async def test_set_quick_mode_cooling_for_x_days(hass):
    """Test cooling for x days quick mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_mode",
        {"quick_mode": "QM_COOLING_FOR_X_DAYS", "duration": 1},
    )
    quick_mode = QuickModes.get("QM_COOLING_FOR_X_DAYS", 1)
    SystemManagerMock.instance.set_quick_mode.assert_called_once_with(quick_mode)


async def test_set_quick_mode_cooling_for_x_days_no_duration(hass):
    """Test cooling for x days quick mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_mode",
        {"quick_mode": "QM_COOLING_FOR_X_DAYS"},
    )
    quick_mode = QuickModes.get("QM_COOLING_FOR_X_DAYS", 1)
    SystemManagerMock.instance.set_quick_mode.assert_called_once_with(quick_mode)


async def test_set_ventilation_day_level(hass):
    """Test set ventilation day level."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_ventilation_day_level",
        {"level": 1},
    )
    SystemManagerMock.instance.set_ventilation_day_level.assert_called_once_with(1)


async def test_set_ventilation_night_level(hass):
    """Test set ventilation night level."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_ventilation_night_level",
        {"level": 1},
    )
    SystemManagerMock.instance.set_ventilation_night_level.assert_called_once_with(1)

"""Tests for service."""
from pymultimatic.model import QuickModes, QuickVeto
import pytest
import voluptuous

from homeassistant.components import multimatic
from homeassistant.components.multimatic import DOMAIN

from tests.components.multimatic import (
    SystemManagerMock,
    active_holiday_mode,
    call_service,
    setup_multimatic,
)


@pytest.fixture(autouse=True)
def fixture_only_climate(mock_manager):
    """Mock multimatic to only handle services."""
    orig_platforms = multimatic.PLATFORMS
    multimatic.PLATFORMS = ["climate"]
    yield
    multimatic.PLATFORMS = orig_platforms


async def test_valid_config(hass):
    """Test setup with valid config."""
    assert await setup_multimatic(hass)
    assert len(hass.services.async_services()[DOMAIN]) == 9


async def test_remove_quick_mode(hass):
    """Test remove existing quick mode."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_quick_mode"] = QuickModes.ONE_DAY_AT_HOME
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(hass, "multimatic", "remove_quick_mode", None)
    SystemManagerMock.instance.remove_quick_mode.assert_called_once_with()


async def test_remove_quick_mode_wrong_data(hass):
    """Test remove existing quick mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "remove_quick_mode", {"test": "boom"})


async def test_remove_holiday_mode(hass):
    """Remove existing holiday mode."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_holiday_mode"] = active_holiday_mode()
    assert await setup_multimatic(hass)
    await call_service(hass, "multimatic", "remove_holiday_mode", None)
    SystemManagerMock.instance.remove_holiday_mode.assert_called_once_with()


async def test_remove_holiday_mode_wrong_data(hass):
    """Remove existing holiday mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "remove_holiday_mode", {"test": "boom"})


async def test_set_quick_mode(hass):
    """Set quick mode."""
    assert await setup_multimatic(hass)
    await call_service(hass, "multimatic", "set_quick_mode", {"quick_mode": "QM_PARTY"})
    SystemManagerMock.instance.set_quick_mode.assert_called_once_with(QuickModes.PARTY)


async def test_set_quick_mode_wrong_data(hass):
    """Set quick mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "set_quick_mode", {"test": "boom"})


async def test_set_holiday_mode_correct_date(hass):
    """Test holiday mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        "multimatic",
        "set_holiday_mode",
        {"start_date": "2010-10-25", "end_date": "2010-10-26", "temperature": "10"},
    )
    SystemManagerMock.instance.set_holiday_mode.assert_called_once()


async def test_set_holiday_mode_wrong_date_format(hass):
    """Test holiday mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        "multimatic",
        "set_holiday_mode",
        {
            "start_date": "2010-10-25T00:00:00.000Z",
            "end_date": "2010-10-26T00:00:00.000Z",
            "temperature": "10",
        },
    )
    SystemManagerMock.instance.set_holiday_mode.assert_called_once()


async def test_set_holiday_mode_wrong_data(hass):
    """Test holiday mode with wrong data."""
    assert await setup_multimatic(hass)
    with pytest.raises(voluptuous.error.MultipleInvalid):
        await call_service(hass, "multimatic", "set_holiday_mode", {"test": "boom"})


async def test_remove_quick_veto_wrong_data(hass):
    """Remove quick veto with wrong entity id."""
    assert await setup_multimatic(hass)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.test123"}
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_not_called()
    SystemManagerMock.instance.remove_zone_quick_veto.assert_not_called()


async def test_remove_quick_veto_room(hass):
    """Remove quick veto with already existing quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_rooms"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.room_1"}
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_called_once_with("1")


async def test_no_remove_quick_veto_room(hass):
    """Remove quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.room_1"}
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_not_called()


async def test_no_remove_quick_veto_zone(hass):
    """Remove quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.zone_1"}
    )
    SystemManagerMock.instance.remove_zone_quick_veto.assert_not_called()


async def test_remove_quick_veto_zone(hass):
    """Remove quick veto with already existing quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_zones"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass, "multimatic", "remove_quick_veto", {"entity_id": "climate.zone_1"}
    )
    SystemManagerMock.instance.remove_zone_quick_veto.assert_called_once_with("zone_1")


async def test_set_quick_veto_room(hass):
    """Set quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.room_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_room_quick_veto.assert_called_once_with(
        "1", QuickVeto(300, 25.0)
    )


async def test_set_quick_veto_room_with_quick_veto(hass):
    """Set quick veto with quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_rooms"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.room_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_room_quick_veto.assert_called_once_with(
        "1", QuickVeto(300, 25.0)
    )
    SystemManagerMock.instance.remove_room_quick_veto.assert_called_once_with("1")


async def test_set_quick_veto_zone_with_quick_veto(hass):
    """Set quick veto with quick veto."""
    SystemManagerMock.init_defaults()
    SystemManagerMock.data["get_zones"][0].quick_veto = QuickVeto(
        duration=30, target=10
    )
    assert await setup_multimatic(hass, with_defaults=False)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.zone_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_zone_quick_veto.assert_called_once_with(
        "zone_1", QuickVeto(300, 25.0)
    )
    SystemManagerMock.instance.remove_zone_quick_veto.assert_called_once_with("zone_1")


async def test_set_quick_veto_zone(hass):
    """Set quick veto without quick veto."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_veto",
        {"entity_id": "climate.zone_1", "duration": 300, "temperature": 25},
    )
    SystemManagerMock.instance.set_zone_quick_veto.assert_called_once_with(
        "zone_1", QuickVeto(300, 25.0)
    )


async def test_set_quick_mode_cooling_for_x_days(hass):
    """Test cooling for x days quick mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_mode",
        {"quick_mode": "QM_COOLING_FOR_X_DAYS", "duration": 1},
    )
    quick_mode = QuickModes.get("QM_COOLING_FOR_X_DAYS", 1)
    SystemManagerMock.instance.set_quick_mode.assert_called_once_with(quick_mode)


async def test_set_quick_mode_cooling_for_x_days_no_duration(hass):
    """Test cooling for x days quick mode."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_quick_mode",
        {"quick_mode": "QM_COOLING_FOR_X_DAYS"},
    )
    quick_mode = QuickModes.get("QM_COOLING_FOR_X_DAYS", 1)
    SystemManagerMock.instance.set_quick_mode.assert_called_once_with(quick_mode)


async def test_set_ventilation_day_level(hass):
    """Test set ventilation day level."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_ventilation_day_level",
        {"level": 1},
    )
    SystemManagerMock.instance.set_ventilation_day_level.assert_called_once_with(1)


async def test_set_ventilation_night_level(hass):
    """Test set ventilation night level."""
    assert await setup_multimatic(hass)
    await call_service(
        hass,
        DOMAIN,
        "set_ventilation_night_level",
        {"level": 1},
    )
    SystemManagerMock.instance.set_ventilation_night_level.assert_called_once_with(1)

Tests for service. Mock multimatic to only handle services. Test setup with valid config. Test remove existing quick mode. Test remove existing quick mode with wrong data. Remove existing holiday mode. Remove existing holiday mode with wrong data. Set quick mode. Set quick mode with wrong data. Test holiday mode. Test holiday mode. Test holiday mode with wrong data. Remove quick veto with wrong entity id. Remove quick veto with already existing quick veto. Remove quick veto without quick veto. Remove quick veto without quick veto. Remove quick veto with already existing quick veto. Set quick veto without quick veto. Set quick veto with quick veto. Set quick veto with quick veto. Set quick veto without quick veto. Test cooling for x days quick mode. Test cooling for x days quick mode. Test set ventilation day level. Test set ventilation night level.