From d410da1c20fa59c7c837346d421544b7313aa1cf Mon Sep 17 00:00:00 2001 From: mle Date: Sat, 11 May 2024 00:01:22 +0200 Subject: [PATCH] Add support for generic modbus setting read --- goodwe/dt.py | 3 +++ goodwe/es.py | 3 +++ goodwe/et.py | 3 +++ tests/test_et.py | 3 +++ 4 files changed, 12 insertions(+) diff --git a/goodwe/dt.py b/goodwe/dt.py index c36a4de..bdc9178 100644 --- a/goodwe/dt.py +++ b/goodwe/dt.py @@ -176,6 +176,9 @@ async def read_runtime_data(self) -> Dict[str, Any]: return data async def read_setting(self, setting_id: str) -> Any: + if setting_id.startswith("modbus"): + response = await self._read_from_socket(self._read_command(int(setting_id[7:]), 1)) + return int.from_bytes(response.read(2), byteorder="big", signed=True) setting = self._settings.get(setting_id) if not setting: raise ValueError(f'Unknown setting "{setting_id}"') diff --git a/goodwe/es.py b/goodwe/es.py index 33c4d18..b4d2ab1 100644 --- a/goodwe/es.py +++ b/goodwe/es.py @@ -220,6 +220,9 @@ async def read_setting(self, setting_id: str) -> Any: if not setting: raise ValueError(f'Unknown setting "{setting_id}"') return await self._read_setting(setting) + elif setting_id.startswith("modbus"): + response = await self._read_from_socket(self._read_command(int(setting_id[7:]), 1)) + return int.from_bytes(response.read(2), byteorder="big", signed=True) else: all_settings = await self.read_settings_data() return all_settings.get(setting_id) diff --git a/goodwe/et.py b/goodwe/et.py index d74b041..891611d 100644 --- a/goodwe/et.py +++ b/goodwe/et.py @@ -610,6 +610,9 @@ async def read_runtime_data(self) -> Dict[str, Any]: return data async def read_setting(self, setting_id: str) -> Any: + if setting_id.startswith("modbus"): + response = await self._read_from_socket(self._read_command(int(setting_id[7:]), 1)) + return int.from_bytes(response.read(2), byteorder="big", signed=True) setting = self._settings.get(setting_id) if not setting: raise ValueError(f'Unknown setting "{setting_id}"') diff --git a/tests/test_et.py b/tests/test_et.py index 13f52ef..0fb8b35 100644 --- a/tests/test_et.py +++ b/tests/test_et.py @@ -255,6 +255,9 @@ def test_GW10K_ET_read_setting(self): self.loop.run_until_complete(self.read_setting('time')) self.assertEqual('f703b090000337b0', self.request.hex()) + self.loop.run_until_complete(self.read_setting('modbus_47000')) + self.assertEqual('f703b798000136c7', self.request.hex()) + def test_GW10K_ET_write_setting(self): self.loop.run_until_complete(self.write_setting('grid_export_limit', 100)) self.assertEqual('f706b996006459c7', self.request.hex())