Skip to content

Commit fd7aefb

Browse files
authored
Add method to configure CoIoT protocol (gen1) (#1026)
* Add method to configure CoIoT protocol (gen1) * add tests * comment
1 parent 815d3fe commit fd7aefb

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

aioshelly/block_device/device.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ async def set_thermostat_state(self, channel: int = 0, **kwargs: Any) -> None:
443443
"""Set thermostat state (Shelly TRV)."""
444444
await self.http_request("get", f"thermostat/{channel}", kwargs)
445445

446+
async def configure_coiot_protocol(self, address: str, port: int) -> None:
447+
"""Configure CoIoT protocol."""
448+
params = {"coiot_enable": "true", "coiot_peer": f"{address}:{port}"}
449+
await self.http_request("post", "settings/advanced", params)
450+
446451
@property
447452
def requires_auth(self) -> bool:
448453
"""Device check for authentication."""

tests/block_device/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
import pytest_asyncio
77
from aiohttp.client import ClientSession
88

9+
import aioshelly.block_device.device as device_module
10+
from aioshelly.block_device import BlockDevice
11+
912

1013
@pytest_asyncio.fixture
1114
async def client_session() -> AsyncGenerator[ClientSession, None]:
1215
"""Fixture for a ClientSession."""
1316
return MagicMock(spec=ClientSession)
17+
18+
19+
@pytest_asyncio.fixture
20+
async def mock_block_device() -> AsyncGenerator[BlockDevice, None]:
21+
"""Fixture for a mock block device."""
22+
# Create a fake BlockDevice instance
23+
fake_block = device_module.BlockDevice.__new__(device_module.BlockDevice)
24+
yield fake_block

tests/block_device/test_device.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44
import socket
5-
from unittest.mock import MagicMock, Mock
5+
from unittest.mock import AsyncMock, MagicMock, Mock
66

77
import pytest
88
from aiohttp.client import ClientSession
@@ -65,3 +65,36 @@ async def test_block_device_ip_address_property_guard(
6565

6666
with pytest.raises(RuntimeError, match="Block device ip_address is None"):
6767
_ = block_device.ip_address
68+
69+
70+
@pytest.mark.asyncio
71+
async def test_configure_coiot_protocol_sends_correct_parameters(
72+
mock_block_device: BlockDevice,
73+
) -> None:
74+
"""Test that configure_coiot_protocol calls http_request with expected params."""
75+
mock_block_device.http_request = AsyncMock()
76+
77+
test_address = "10.10.10.10"
78+
test_port = 5683
79+
80+
await mock_block_device.configure_coiot_protocol(test_address, test_port)
81+
82+
mock_block_device.http_request.assert_awaited_once_with(
83+
"post",
84+
"settings/advanced",
85+
{
86+
"coiot_enable": "true",
87+
"coiot_peer": f"{test_address}:{test_port}",
88+
},
89+
)
90+
91+
92+
@pytest.mark.asyncio
93+
async def test_configure_coiot_protocol_propagates_exceptions(
94+
mock_block_device: BlockDevice,
95+
) -> None:
96+
"""Test that exceptions from http_request bubble up (no silent swallow)."""
97+
mock_block_device.http_request = AsyncMock(side_effect=RuntimeError("HTTP failure"))
98+
99+
with pytest.raises(RuntimeError):
100+
await mock_block_device.configure_coiot_protocol("10.10.10.10", 5683)

0 commit comments

Comments
 (0)