Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests for RpcDevice #791

Merged
merged 20 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions aioshelly/rpc_device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections.abc import Callable, Iterable
from enum import Enum, auto
from functools import partial
from typing import TYPE_CHECKING, Any, cast
from typing import Any, cast

from aiohttp import ClientSession

Expand Down Expand Up @@ -283,13 +283,13 @@ async def poll(self) -> None:
calls.append(("Shelly.GetComponents", {"dynamic_only": True}))
results = await self.call_rpc_multiple(calls, DEVICE_POLL_TIMEOUT)
if (status := results[0]) is None:
raise RpcCallError("empty response to Shelly.GetStatus")
raise RpcCallError(0, "empty response to Shelly.GetStatus")
if self._status is None:
raise NotInitialized
self._status.update(status)
if has_dynamic:
if (dynamic := results[1]) is None:
raise RpcCallError("empty response to Shelly.GetComponents")
raise RpcCallError(0, "empty response to Shelly.GetComponents")
self._parse_dynamic_components(dynamic)
await self._retrieve_blutrv_components(dynamic)

Expand Down Expand Up @@ -556,9 +556,8 @@ def _parse_dynamic_components(self, components: dict[str, Any]) -> None:
if any(supported in component["key"] for supported in VIRTUAL_COMPONENTS)
]

if TYPE_CHECKING:
assert self._config is not None
assert self._status is not None
if not self._config or not self._status:
raise NotInitialized

self._config.update(
{
Expand Down Expand Up @@ -587,7 +586,7 @@ async def _retrieve_blutrv_components(self, components: dict[str, Any]) -> None:
if _key[0] != BLU_TRV_IDENTIFIER:
continue

result = await self.call_rpc("BluTrv.GetRemoteConfig", {"id": _key[1]})
result = await self.call_rpc("BluTrv.GetRemoteConfig", {"id": int(_key[1])})

cfg: dict[str, Any] = result["config"]["trv:0"]
# addr, name and model_id must be added from Shelly.GetComponents call
Expand Down
2 changes: 2 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ lint.ignore = [
"D102",
"D103",
"D104",
"PLR0913",
"PLR2004",
"S101",
"SLF001",
]
Expand Down
30 changes: 30 additions & 0 deletions tests/rpc_device/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from aiohttp.http_websocket import WSMessage, WSMsgType
from orjson import dumps

from aioshelly.common import ConnectionOptions
from aioshelly.rpc_device.device import RpcDevice, WsServer
from aioshelly.rpc_device.wsrpc import DEFAULT_HTTP_PORT, AuthData, RPCSource, WsRPC


Expand Down Expand Up @@ -139,3 +141,31 @@ async def ws_rpc_with_auth(ws_rpc: WsRPCMocker) -> AsyncGenerator[WsRPCMocker, N
"""Fixture for an RPC WebSocket with authentication."""
ws_rpc._auth_data = AuthData("any", "any", "any")
yield ws_rpc


@pytest_asyncio.fixture
async def ws_context() -> AsyncGenerator[WsServer, None]:
"""Fixture for a WsServer."""
mock = MagicMock(spec=WsServer)

yield mock


@pytest_asyncio.fixture
async def rpc_device(
client_session: ClientSession, ws_context: WsServer, ws_rpc: WsRPCMocker
) -> AsyncGenerator[RpcDevice, None]:
"""Fixture for RpcDevice."""
await ws_rpc.disconnect()

options = ConnectionOptions(
"10.10.10.10",
"username",
"password",
)

rpc_device = await RpcDevice.create(client_session, ws_context, options)
rpc_device._wsrpc = ws_rpc
rpc_device.call_rpc_multiple = AsyncMock()

yield rpc_device
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"v": 14,
"ts": 1739221213,
"config": {
"sys": {
"device": {
"name": ""
},
"location": {
"lat": 11.1111,
"lon": 22.2222
},
"ui": {
"lock": false,
"t_units": "C",
"flip": false,
"brightness": 7
},
"ble": {
"interval_ms": 333,
"beacon_count": 5
},
"cfg_rev": 34
},
"temperature:0": {
"id": 0,
"offset_C": 0
},
"trv:0": {
"id": 0,
"enable": true,
"override_enable": true,
"min_valve_position": 0,
"default_boost_duration": 900,
"default_override_duration": 2147483647,
"default_override_target_C": 8,
"flags": [
"floor_heating",
"auto_calibrate",
"anticlog"
]
}
}
}
110 changes: 110 additions & 0 deletions tests/rpc_device/fixtures/shellyblugatewaygen3/Shelly.GetComponents
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"components": [
{
"key": "blutrv:200",
"status": {
"id": 200,
"target_C": 19,
"current_C": 21,
"pos": 0,
"rssi": -58,
"battery": 100,
"packet_id": 29,
"last_updated_ts": 1739224517,
"paired": true,
"rpc": true,
"rsv": 14
},
"config": {
"id": 200,
"addr": "aa:bb:cc:dd:ee:ff",
"name": "Shelly BLU TRV [DDEEFF]",
"key": null,
"trv": "bthomedevice:200",
"temp_sensors": [],
"dw_sensors": [],
"override_delay": 30,
"meta": {}
},
"attrs": {
"flags": 17,
"model_id": 8
}
},
{
"key": "bthomedevice:200",
"status": {
"id": 200,
"rssi": -58,
"battery": 100,
"packet_id": 29,
"last_updated_ts": 1739224517,
"paired": true,
"rpc": true,
"rsv": 14
},
"config": {
"id": 200,
"addr": "aa:bb:cc:dd:ee:ff",
"name": "Shelly BLU TRV [DDEEFF]",
"key": null,
"meta": null
},
"attrs": {
"flags": 17,
"model_id": 8
}
},
{
"key": "bthomesensor:200",
"status": {
"id": 200,
"value": 100,
"last_updated_ts": 1739224517
},
"config": {
"id": 200,
"addr": "aa:bb:cc:dd:ee:ff",
"name": null,
"obj_id": 1,
"idx": 0,
"meta": null
}
},
{
"key": "bthomesensor:202",
"status": {
"id": 202,
"value": 19,
"last_updated_ts": 1739224517
},
"config": {
"id": 202,
"addr": "aa:bb:cc:dd:ee:ff",
"name": null,
"obj_id": 69,
"idx": 0,
"meta": null
}
},
{
"key": "bthomesensor:203",
"status": {
"id": 203,
"value": 0,
"last_updated_ts": 1739224517
},
"config": {
"id": 203,
"addr": "aa:bb:cc:dd:ee:ff",
"name": null,
"obj_id": 69,
"idx": 1,
"meta": null
}
}
],
"cfg_rev": 32,
"offset": 0,
"total": 5
}
88 changes: 88 additions & 0 deletions tests/rpc_device/fixtures/shellyblugatewaygen3/Shelly.GetConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"ble": {
"enable": true,
"rpc": {
"enable": true
}
},
"blugw": {
"sys_led_enable": true
},
"blutrv:200": {
"addr": "f8:44:77:1c:86:7e",
"default_boost_duration": 900,
"default_override_duration": 2147483647,
"default_override_target_C": 8,
"enable": true,
"flags": [
"floor_heating",
"auto_calibrate",
"anticlog"
],
"id": 0,
"local_name": "SBTR-001AEU",
"min_valve_position": 0,
"name": "Shelly BLU TRV [7744F8]",
"override_enable": true
},
"bthome": {},
"cloud": {
"enable": false,
"server": "shelly-api-eu.shelly.cloud:6022/jrpc"
},
"mqtt": {
"client_id": "shelly-blu-gateway-gen3-78742c",
"enable": false,
"enable_control": true,
"enable_rpc": true,
"rpc_ntf": true,
"server": "mqtt.test.server",
"ssl_ca": null,
"status_ntf": true,
"topic_prefix": "shellies-gen3/shelly-blu-gateway-gen3-78742c",
"use_client_cert": false,
"user": "mqtt_client"
},
"sys": {
"cfg_rev": 32,
"debug": {
"file_level": null,
"level": 2,
"mqtt": {
"enable": false
},
"udp": {
"addr": null
},
"websocket": {
"enable": false
}
},
"device": {
"discoverable": true,
"eco_mode": false,
"fw_id": "20250203-144328/1.5.0-beta2-gbf89ed5",
"mac": "AABBCCDDEEFF",
"name": "Test Name"
},
"location": {
"lat": 52.2201,
"lon": 21.0121,
"tz": "Europe/Warsaw"
},
"rpc_udp": {
"dst_addr": null,
"listen_port": null
},
"sntp": {
"server": "sntp.test.server"
},
"ui_data": {}
},
"wifi": "Wifi-Network-Name",
"ws": {
"enable": false,
"server": null,
"ssl_ca": "ca.pem"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Shelly BLU Gateway Gen3 [DDEEFF]",
"id": "shellyblugwg3-aabbccddeeff",
"mac": "AABBCCDDEEFF",
"slot": 0,
"model": "S3GW-1DBT001",
"gen": 3,
"fw_id": "20250203-144328/1.5.0-beta2-gbf89ed5",
"ver": "1.5.0-beta2",
"app": "BluGwG3",
"auth_en": true,
"auth_domain": "shellyblugwg3-aabbccddeeff"
}
Loading