From 83eb73cc7f0f473550fa1148a709fcb9ef551b6c Mon Sep 17 00:00:00 2001 From: "Steven B." <51370195+sdb9696@users.noreply.github.com> Date: Fri, 20 Dec 2024 06:16:18 +0000 Subject: [PATCH] Add rssi and signal_level to smartcam (#1392) --- kasa/smartcam/modules/device.py | 45 ++++++++++++++++++++++++++++++++- kasa/smartcam/smartcamdevice.py | 6 +++++ kasa/smartcam/smartcammodule.py | 4 +++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/kasa/smartcam/modules/device.py b/kasa/smartcam/modules/device.py index 0541d75c..655a92da 100644 --- a/kasa/smartcam/modules/device.py +++ b/kasa/smartcam/modules/device.py @@ -14,6 +14,13 @@ class DeviceModule(SmartCamModule): QUERY_MODULE_NAME = "device_info" QUERY_SECTION_NAMES = ["basic_info", "info"] + def query(self) -> dict: + """Query to execute during the update cycle.""" + q = super().query() + q["getConnectionType"] = {"network": {"get_connection_type": []}} + + return q + def _initialize_features(self) -> None: """Initialize features after the initial update.""" self._add_feature( @@ -26,6 +33,32 @@ class DeviceModule(SmartCamModule): type=Feature.Type.Sensor, ) ) + if self.rssi is not None: + self._add_feature( + Feature( + self._device, + container=self, + id="rssi", + name="RSSI", + attribute_getter="rssi", + icon="mdi:signal", + unit_getter=lambda: "dBm", + category=Feature.Category.Debug, + type=Feature.Type.Sensor, + ) + ) + self._add_feature( + Feature( + self._device, + container=self, + id="signal_level", + name="Signal Level", + attribute_getter="signal_level", + icon="mdi:signal", + category=Feature.Category.Info, + type=Feature.Type.Sensor, + ) + ) async def _post_update_hook(self) -> None: """Overriden to prevent module disabling. @@ -37,4 +70,14 @@ class DeviceModule(SmartCamModule): @property def device_id(self) -> str: """Return the device id.""" - return self.data["basic_info"]["dev_id"] + return self.data[self.QUERY_GETTER_NAME]["basic_info"]["dev_id"] + + @property + def rssi(self) -> int | None: + """Return the device id.""" + return self.data["getConnectionType"].get("rssiValue") + + @property + def signal_level(self) -> int | None: + """Return the device id.""" + return self.data["getConnectionType"].get("rssi") diff --git a/kasa/smartcam/smartcamdevice.py b/kasa/smartcam/smartcamdevice.py index 6bc4963a..fdae3140 100644 --- a/kasa/smartcam/smartcamdevice.py +++ b/kasa/smartcam/smartcamdevice.py @@ -185,6 +185,7 @@ class SmartCamDevice(SmartDevice): initial_query = { "getDeviceInfo": {"device_info": {"name": ["basic_info", "info"]}}, "getAppComponentList": {"app_component": {"name": "app_component_list"}}, + "getConnectionType": {"network": {"get_connection_type": {}}}, } resp = await self.protocol.query(initial_query) self._last_update.update(resp) @@ -261,3 +262,8 @@ class SmartCamDevice(SmartDevice): "dev_name": self.alias, "oemId": self._info.get("oem_id"), } + + @property + def rssi(self) -> int | None: + """Return the device id.""" + return self.modules[SmartCamModule.SmartCamDeviceModule].rssi diff --git a/kasa/smartcam/smartcammodule.py b/kasa/smartcam/smartcammodule.py index 467d18c0..85addd65 100644 --- a/kasa/smartcam/smartcammodule.py +++ b/kasa/smartcam/smartcammodule.py @@ -33,6 +33,10 @@ class SmartCamModule(SmartModule): "BabyCryDetection" ) + SmartCamDeviceModule: Final[ModuleName[modules.DeviceModule]] = ModuleName( + "devicemodule" + ) + #: Module name to be queried QUERY_MODULE_NAME: str #: Section name or names to be queried