Skip to content

Commit 30bd077

Browse files
committed
fix: implement control_no_check method for on/off switch
1 parent aa942fd commit 30bd077

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

custom_components/solis_cloud_control/coordinator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ async def control(
7575

7676
while attempt <= retry_count:
7777
await self._api_client.control(inverter_sn, cid, value, old_value)
78-
current_value = await self._api_client.read(inverter_sn, cid)
7978

79+
current_value = await self._api_client.read(inverter_sn, cid)
8080
if current_value == value:
8181
await self.async_request_refresh()
8282
return
@@ -92,3 +92,13 @@ async def control(
9292
await asyncio.sleep(retry_delay)
9393
else:
9494
raise HomeAssistantError(f"Failed to set value for CID {cid}. Expected: {value}, got: {current_value}")
95+
96+
async def control_no_check(
97+
self,
98+
cid: int,
99+
value: str,
100+
old_value: str | None = None,
101+
) -> None:
102+
inverter_sn = self._inverter.info.serial_number
103+
await self._api_client.control(inverter_sn, cid, value, old_value)
104+
await self.async_request_refresh()

custom_components/solis_cloud_control/switch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ def __init__(
8585
self._attr_assumed_state = True
8686

8787
async def async_turn_on(self, **kwargs: dict[str, any]) -> None: # noqa: ARG002
88-
await self.coordinator.control(self.on_off.on_cid, self.on_off.on_value)
88+
_LOGGER.info("Turning on inverter")
89+
await self.coordinator.control_no_check(self.on_off.on_cid, self.on_off.on_value)
8990
self._attr_is_on = True
9091

9192
async def async_turn_off(self, **kwargs: dict[str, any]) -> None: # noqa: ARG002
92-
await self.coordinator.control(self.on_off.off_cid, self.on_off.off_value)
93+
_LOGGER.info("Turning off inverter")
94+
await self.coordinator.control_no_check(self.on_off.off_cid, self.on_off.off_value)
9395
self._attr_is_on = False
9496

9597

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def mock_api_client():
4646
def mock_coordinator():
4747
coordinator = Mock()
4848
coordinator.control = AsyncMock()
49+
coordinator.control_no_check = AsyncMock()
4950
coordinator.data = {}
5051
coordinator.config_entry = Mock()
5152
coordinator.config_entry.entry_id = "any_entry_id"

tests/test_coordinator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,36 @@ async def test_control_retry_and_fail(hass: HomeAssistant, coordinator, mock_api
102102
assert mock_api_client.control.call_count == 2 # Initial + 1 retry
103103
assert mock_api_client.read.call_count == 2 # Initial + 1 retry
104104
mock_refresh.assert_not_called()
105+
106+
107+
async def test_control_no_check(hass: HomeAssistant, coordinator, mock_api_client, any_inverter):
108+
any_cid = 123
109+
any_value = "any_value"
110+
any_old_value = "any_old_value"
111+
112+
mock_api_client.control.return_value = None
113+
114+
with patch.object(coordinator, "async_request_refresh", AsyncMock()) as mock_refresh:
115+
await coordinator.control_no_check(any_cid, any_value, any_old_value)
116+
117+
mock_api_client.control.assert_called_once_with(
118+
any_inverter.info.serial_number, any_cid, any_value, any_old_value
119+
)
120+
mock_api_client.read.assert_not_called()
121+
mock_refresh.assert_called_once()
122+
123+
124+
async def test_control_no_check_api_error(hass: HomeAssistant, coordinator, mock_api_client):
125+
any_cid = 123
126+
any_value = "any_value"
127+
any_error = "any error"
128+
129+
mock_api_client.control.side_effect = SolisCloudControlApiError(any_error)
130+
131+
with pytest.raises(SolisCloudControlApiError) as exc_info:
132+
await coordinator.control_no_check(any_cid, any_value)
133+
134+
assert str(exc_info.value) == any_error
135+
136+
mock_api_client.control.assert_called_once_with(coordinator._inverter.info.serial_number, any_cid, any_value, None)
137+
mock_api_client.read.assert_not_called()

tests/test_switch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def test_init(self, on_off_switch):
2424

2525
async def test_turn_on(self, on_off_switch):
2626
await on_off_switch.async_turn_on()
27-
on_off_switch.coordinator.control.assert_awaited_once_with(
27+
on_off_switch.coordinator.control_no_check.assert_awaited_once_with(
2828
on_off_switch.on_off.on_cid, on_off_switch.on_off.on_value
2929
)
3030
assert on_off_switch.is_on is True
3131

3232
async def test_turn_off(self, on_off_switch):
3333
await on_off_switch.async_turn_off()
34-
on_off_switch.coordinator.control.assert_awaited_once_with(
34+
on_off_switch.coordinator.control_no_check.assert_awaited_once_with(
3535
on_off_switch.on_off.off_cid, on_off_switch.on_off.off_value
3636
)
3737
assert on_off_switch.is_on is False

0 commit comments

Comments
 (0)