|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
7 | | -from custom_components.mitsubishi.const import DOMAIN |
| 7 | +from custom_components.mitsubishi.const import ( |
| 8 | + CONF_EXPERIMENTAL_FEATURES, |
| 9 | + CONF_EXTERNAL_TEMP_ENTITY, |
| 10 | + DOMAIN, |
| 11 | +) |
8 | 12 | from custom_components.mitsubishi.select import ( |
9 | 13 | MitsubishiPowerSavingSelect, |
| 14 | + MitsubishiTemperatureSourceSelect, |
10 | 15 | async_setup_entry, |
11 | 16 | ) |
12 | 17 |
|
@@ -97,3 +102,126 @@ async def test_power_saving_select_extra_state_attributes( |
97 | 102 |
|
98 | 103 | attributes = select.extra_state_attributes |
99 | 104 | assert attributes == {"source": "Mitsubishi AC"} |
| 105 | + |
| 106 | + |
| 107 | +@pytest.fixture |
| 108 | +def mock_config_entry_experimental(): |
| 109 | + """Create a mock config entry with experimental features enabled.""" |
| 110 | + from homeassistant.const import CONF_HOST |
| 111 | + from pytest_homeassistant_custom_component.common import MockConfigEntry |
| 112 | + |
| 113 | + return MockConfigEntry( |
| 114 | + domain=DOMAIN, |
| 115 | + title="Test Mitsubishi AC", |
| 116 | + unique_id="00:11:22:33:44:56", |
| 117 | + data={ |
| 118 | + CONF_HOST: "192.168.1.100", |
| 119 | + "encryption_key": "unregistered", |
| 120 | + "admin_username": "admin", |
| 121 | + "admin_password": "password", |
| 122 | + "scan_interval": 30, |
| 123 | + }, |
| 124 | + options={ |
| 125 | + CONF_EXPERIMENTAL_FEATURES: True, |
| 126 | + CONF_EXTERNAL_TEMP_ENTITY: "sensor.room_temp", |
| 127 | + }, |
| 128 | + entry_id="test_entry_exp_id", |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.asyncio |
| 133 | +async def test_async_setup_entry_with_experimental( |
| 134 | + hass, mock_coordinator, mock_config_entry_experimental |
| 135 | +): |
| 136 | + """Test setup with experimental features enabled creates temperature source select.""" |
| 137 | + hass.data[DOMAIN] = {mock_config_entry_experimental.entry_id: mock_coordinator} |
| 138 | + async_add_entities = MagicMock() |
| 139 | + await async_setup_entry(hass, mock_config_entry_experimental, async_add_entities) |
| 140 | + async_add_entities.assert_called_once() |
| 141 | + entities = async_add_entities.call_args[0][0] |
| 142 | + assert len(entities) == 2 |
| 143 | + assert isinstance(entities[0], MitsubishiPowerSavingSelect) |
| 144 | + assert isinstance(entities[1], MitsubishiTemperatureSourceSelect) |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.asyncio |
| 148 | +async def test_temperature_source_select_init( |
| 149 | + hass, mock_coordinator, mock_config_entry_experimental |
| 150 | +): |
| 151 | + """Test temperature source select entity initialization.""" |
| 152 | + select = MitsubishiTemperatureSourceSelect(mock_coordinator, mock_config_entry_experimental) |
| 153 | + |
| 154 | + assert select._attr_name == "Temperature Source" |
| 155 | + assert select._attr_icon == "mdi:thermometer" |
| 156 | + assert select._attr_options == ["Internal", "Remote"] |
| 157 | + assert select.unique_id.endswith("_temperature_source_select") |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.asyncio |
| 161 | +async def test_temperature_source_select_current_option_internal( |
| 162 | + hass, mock_coordinator, mock_config_entry_experimental |
| 163 | +): |
| 164 | + """Test temperature source select current option when in internal mode.""" |
| 165 | + mock_coordinator.remote_temp_mode = False |
| 166 | + select = MitsubishiTemperatureSourceSelect(mock_coordinator, mock_config_entry_experimental) |
| 167 | + |
| 168 | + assert select.current_option == "Internal" |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.asyncio |
| 172 | +async def test_temperature_source_select_current_option_remote( |
| 173 | + hass, mock_coordinator, mock_config_entry_experimental |
| 174 | +): |
| 175 | + """Test temperature source select current option when in remote mode.""" |
| 176 | + mock_coordinator.remote_temp_mode = True |
| 177 | + select = MitsubishiTemperatureSourceSelect(mock_coordinator, mock_config_entry_experimental) |
| 178 | + |
| 179 | + assert select.current_option == "Remote" |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.asyncio |
| 183 | +async def test_temperature_source_select_switch_to_internal( |
| 184 | + hass, mock_coordinator, mock_config_entry_experimental |
| 185 | +): |
| 186 | + """Test switching temperature source to internal.""" |
| 187 | + mock_coordinator.set_remote_temp_mode = MagicMock() |
| 188 | + select = MitsubishiTemperatureSourceSelect(mock_coordinator, mock_config_entry_experimental) |
| 189 | + select.hass = hass |
| 190 | + select.async_write_ha_state = MagicMock() |
| 191 | + |
| 192 | + with patch.object(hass, "async_add_executor_job", new=AsyncMock()) as mock_executor: |
| 193 | + await select.async_select_option("Internal") |
| 194 | + |
| 195 | + mock_coordinator.set_remote_temp_mode.assert_called_once_with(False) |
| 196 | + mock_executor.assert_called_once() |
| 197 | + select.async_write_ha_state.assert_called_once() |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.asyncio |
| 201 | +async def test_temperature_source_select_switch_to_remote_no_entity( |
| 202 | + hass, mock_coordinator, mock_config_entry |
| 203 | +): |
| 204 | + """Test switching to remote fails when no external entity configured.""" |
| 205 | + mock_coordinator.set_remote_temp_mode = MagicMock() |
| 206 | + select = MitsubishiTemperatureSourceSelect(mock_coordinator, mock_config_entry) |
| 207 | + select.hass = hass |
| 208 | + select.async_write_ha_state = MagicMock() |
| 209 | + |
| 210 | + with patch.object(hass, "async_add_executor_job", new=AsyncMock()) as mock_executor: |
| 211 | + await select.async_select_option("Remote") |
| 212 | + |
| 213 | + # Should not change mode when no entity configured |
| 214 | + mock_coordinator.set_remote_temp_mode.assert_not_called() |
| 215 | + mock_executor.assert_not_called() |
| 216 | + |
| 217 | + |
| 218 | +@pytest.mark.asyncio |
| 219 | +async def test_temperature_source_select_extra_state_attributes_no_entity( |
| 220 | + hass, mock_coordinator, mock_config_entry |
| 221 | +): |
| 222 | + """Test temperature source select extra state attributes without external entity.""" |
| 223 | + select = MitsubishiTemperatureSourceSelect(mock_coordinator, mock_config_entry) |
| 224 | + select.hass = hass |
| 225 | + |
| 226 | + attributes = select.extra_state_attributes |
| 227 | + assert attributes == {"source": "Mitsubishi AC"} |
0 commit comments