|
| 1 | +"""Tests for home_connect time entities.""" |
| 2 | + |
| 3 | +from collections.abc import Awaitable, Callable, Generator |
| 4 | +from datetime import time |
| 5 | +from unittest.mock import MagicMock, Mock |
| 6 | + |
| 7 | +from homeconnect.api import HomeConnectError |
| 8 | +import pytest |
| 9 | + |
| 10 | +from homeassistant.components.home_connect.const import ATTR_VALUE |
| 11 | +from homeassistant.components.time import DOMAIN as TIME_DOMAIN, SERVICE_SET_VALUE |
| 12 | +from homeassistant.config_entries import ConfigEntryState |
| 13 | +from homeassistant.const import ATTR_ENTITY_ID, ATTR_TIME, Platform |
| 14 | +from homeassistant.core import HomeAssistant |
| 15 | + |
| 16 | +from .conftest import get_all_appliances |
| 17 | + |
| 18 | +from tests.common import MockConfigEntry |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def platforms() -> list[str]: |
| 23 | + """Fixture to specify platforms to test.""" |
| 24 | + return [Platform.TIME] |
| 25 | + |
| 26 | + |
| 27 | +async def test_time( |
| 28 | + bypass_throttle: Generator[None], |
| 29 | + hass: HomeAssistant, |
| 30 | + config_entry: MockConfigEntry, |
| 31 | + integration_setup: Callable[[], Awaitable[bool]], |
| 32 | + setup_credentials: None, |
| 33 | + get_appliances: Mock, |
| 34 | +) -> None: |
| 35 | + """Test time entity.""" |
| 36 | + get_appliances.side_effect = get_all_appliances |
| 37 | + assert config_entry.state is ConfigEntryState.NOT_LOADED |
| 38 | + assert await integration_setup() |
| 39 | + assert config_entry.state is ConfigEntryState.LOADED |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("appliance", ["Oven"], indirect=True) |
| 43 | +@pytest.mark.parametrize( |
| 44 | + ("entity_id", "setting_key", "setting_value", "expected_state"), |
| 45 | + [ |
| 46 | + ( |
| 47 | + f"{TIME_DOMAIN}.oven_alarm_clock", |
| 48 | + "BSH.Common.Setting.AlarmClock", |
| 49 | + {ATTR_VALUE: 59}, |
| 50 | + str(time(second=59)), |
| 51 | + ), |
| 52 | + ( |
| 53 | + f"{TIME_DOMAIN}.oven_alarm_clock", |
| 54 | + "BSH.Common.Setting.AlarmClock", |
| 55 | + {ATTR_VALUE: None}, |
| 56 | + "unknown", |
| 57 | + ), |
| 58 | + ( |
| 59 | + f"{TIME_DOMAIN}.oven_alarm_clock", |
| 60 | + "BSH.Common.Setting.AlarmClock", |
| 61 | + None, |
| 62 | + "unknown", |
| 63 | + ), |
| 64 | + ], |
| 65 | +) |
| 66 | +@pytest.mark.usefixtures("bypass_throttle") |
| 67 | +async def test_time_entity_functionality( |
| 68 | + appliance: Mock, |
| 69 | + entity_id: str, |
| 70 | + setting_key: str, |
| 71 | + setting_value: dict, |
| 72 | + expected_state: str, |
| 73 | + bypass_throttle: Generator[None], |
| 74 | + hass: HomeAssistant, |
| 75 | + config_entry: MockConfigEntry, |
| 76 | + integration_setup: Callable[[], Awaitable[bool]], |
| 77 | + setup_credentials: None, |
| 78 | + get_appliances: MagicMock, |
| 79 | +) -> None: |
| 80 | + """Test time entity functionality.""" |
| 81 | + get_appliances.return_value = [appliance] |
| 82 | + appliance.status.update({setting_key: setting_value}) |
| 83 | + |
| 84 | + assert config_entry.state is ConfigEntryState.NOT_LOADED |
| 85 | + assert await integration_setup() |
| 86 | + assert config_entry.state is ConfigEntryState.LOADED |
| 87 | + assert hass.states.is_state(entity_id, expected_state) |
| 88 | + |
| 89 | + new_value = 30 |
| 90 | + assert hass.states.get(entity_id).state != new_value |
| 91 | + await hass.services.async_call( |
| 92 | + TIME_DOMAIN, |
| 93 | + SERVICE_SET_VALUE, |
| 94 | + { |
| 95 | + ATTR_ENTITY_ID: entity_id, |
| 96 | + ATTR_TIME: time(second=new_value), |
| 97 | + }, |
| 98 | + blocking=True, |
| 99 | + ) |
| 100 | + appliance.set_setting.assert_called_once_with(setting_key, new_value) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("problematic_appliance", ["Oven"], indirect=True) |
| 104 | +@pytest.mark.parametrize( |
| 105 | + ("entity_id", "setting_key", "mock_attr"), |
| 106 | + [ |
| 107 | + ( |
| 108 | + f"{TIME_DOMAIN}.oven_alarm_clock", |
| 109 | + "BSH.Common.Setting.AlarmClock", |
| 110 | + "set_setting", |
| 111 | + ), |
| 112 | + ], |
| 113 | +) |
| 114 | +@pytest.mark.usefixtures("bypass_throttle") |
| 115 | +async def test_time_entity_error( |
| 116 | + problematic_appliance: Mock, |
| 117 | + entity_id: str, |
| 118 | + setting_key: str, |
| 119 | + mock_attr: str, |
| 120 | + hass: HomeAssistant, |
| 121 | + config_entry: MockConfigEntry, |
| 122 | + integration_setup: Callable[[], Awaitable[bool]], |
| 123 | + setup_credentials: None, |
| 124 | + get_appliances: MagicMock, |
| 125 | +) -> None: |
| 126 | + """Test time entity error.""" |
| 127 | + get_appliances.return_value = [problematic_appliance] |
| 128 | + |
| 129 | + assert config_entry.state is ConfigEntryState.NOT_LOADED |
| 130 | + problematic_appliance.status.update({setting_key: {}}) |
| 131 | + assert await integration_setup() |
| 132 | + assert config_entry.state is ConfigEntryState.LOADED |
| 133 | + |
| 134 | + with pytest.raises(HomeConnectError): |
| 135 | + getattr(problematic_appliance, mock_attr)() |
| 136 | + |
| 137 | + await hass.services.async_call( |
| 138 | + TIME_DOMAIN, |
| 139 | + SERVICE_SET_VALUE, |
| 140 | + { |
| 141 | + ATTR_ENTITY_ID: entity_id, |
| 142 | + ATTR_TIME: time(minute=1), |
| 143 | + }, |
| 144 | + blocking=True, |
| 145 | + ) |
| 146 | + assert getattr(problematic_appliance, mock_attr).call_count == 2 |
0 commit comments