Skip to content

Commit b28fba8

Browse files
Fix test fixtures and add temperature source select tests
- Fix mock_config_entry_experimental fixture to properly pass options parameter to MockConfigEntry constructor instead of setting _options - Add tests for MitsubishiTemperatureSourceSelect entity - Add tests for coordinator remote_temp_mode and experimental_features
1 parent 4dd6a7e commit b28fba8

2 files changed

Lines changed: 176 additions & 3 deletions

File tree

tests/test_coordinator.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ async def test_coordinator_init(hass, mock_mitsubishi_controller, mock_config_en
2424

2525

2626
@pytest.mark.asyncio
27-
async def test_coordinator_init_custom_interval(hass, mock_mitsubishi_controller, mock_config_entry):
27+
async def test_coordinator_init_custom_interval(
28+
hass, mock_mitsubishi_controller, mock_config_entry
29+
):
2830
"""Test coordinator initialization with custom scan interval."""
2931
custom_interval = 60
3032
coordinator = MitsubishiDataUpdateCoordinator(
@@ -69,7 +71,9 @@ async def test_async_update_data_success(hass, mock_mitsubishi_controller, mock_
6971

7072

7173
@pytest.mark.asyncio
72-
async def test_async_update_data_fetch_status_fails(hass, mock_mitsubishi_controller, mock_config_entry):
74+
async def test_async_update_data_fetch_status_fails(
75+
hass, mock_mitsubishi_controller, mock_config_entry
76+
):
7377
"""Test data update when fetch_status fails."""
7478
coordinator = MitsubishiDataUpdateCoordinator(
7579
hass, mock_mitsubishi_controller, mock_config_entry
@@ -79,3 +83,44 @@ async def test_async_update_data_fetch_status_fails(hass, mock_mitsubishi_contro
7983

8084
with pytest.raises(UpdateFailed, match="foobar"):
8185
await coordinator._async_update_data()
86+
87+
88+
@pytest.mark.asyncio
89+
async def test_coordinator_remote_temp_mode_property(
90+
hass, mock_mitsubishi_controller, mock_config_entry
91+
):
92+
"""Test remote_temp_mode property returns False by default."""
93+
coordinator = MitsubishiDataUpdateCoordinator(
94+
hass, mock_mitsubishi_controller, mock_config_entry
95+
)
96+
97+
assert coordinator.remote_temp_mode is False
98+
99+
100+
@pytest.mark.asyncio
101+
async def test_coordinator_experimental_features_disabled(
102+
hass, mock_mitsubishi_controller, mock_config_entry
103+
):
104+
"""Test experimental_features_enabled property when disabled."""
105+
coordinator = MitsubishiDataUpdateCoordinator(
106+
hass, mock_mitsubishi_controller, mock_config_entry
107+
)
108+
109+
assert coordinator.experimental_features_enabled is False
110+
111+
112+
@pytest.mark.asyncio
113+
async def test_coordinator_set_remote_temp_mode(
114+
hass, mock_mitsubishi_controller, mock_config_entry
115+
):
116+
"""Test set_remote_temp_mode method."""
117+
mock_config_entry.add_to_hass(hass)
118+
coordinator = MitsubishiDataUpdateCoordinator(
119+
hass, mock_mitsubishi_controller, mock_config_entry
120+
)
121+
122+
coordinator.set_remote_temp_mode(True)
123+
assert coordinator.remote_temp_mode is True
124+
125+
coordinator.set_remote_temp_mode(False)
126+
assert coordinator.remote_temp_mode is False

tests/test_select.py

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
import pytest
66

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+
)
812
from custom_components.mitsubishi.select import (
913
MitsubishiPowerSavingSelect,
14+
MitsubishiTemperatureSourceSelect,
1015
async_setup_entry,
1116
)
1217

@@ -97,3 +102,126 @@ async def test_power_saving_select_extra_state_attributes(
97102

98103
attributes = select.extra_state_attributes
99104
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

Comments
 (0)