Skip to content

Commit

Permalink
Fix writing single byte settings on ES inverters
Browse files Browse the repository at this point in the history
  • Loading branch information
mletenay committed May 12, 2024
1 parent 5f1811d commit 337a799
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 1 addition & 2 deletions goodwe/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,9 @@ async def _write_setting(self, setting: Sensor, value: Any):
# modbus can address/store only 16 bit values, read the other 8 bytes
if self._is_modbus_setting(setting):
response = await self._read_from_socket(self._read_command(setting.offset, 1))
raw_value = setting.encode_value(value, response.response_data()[0:2])
else:
response = await self._read_from_socket(Aa55ReadCommand(setting.offset, 1))
raw_value = setting.encode_value(value, response.response_data()[2:4])
raw_value = setting.encode_value(value, response.response_data()[0:2])
else:
raw_value = setting.encode_value(value)
if len(raw_value) <= 2:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from goodwe.es import ES
from goodwe.exceptions import RequestFailedException
from goodwe.inverter import OperationMode
from goodwe.protocol import ProtocolCommand, ProtocolResponse
from goodwe.protocol import Aa55ReadCommand, ProtocolCommand, ProtocolResponse


class EsMock(TestCase, ES):
Expand All @@ -26,6 +26,8 @@ async def _read_from_socket(self, command: ProtocolCommand) -> ProtocolResponse:
root_dir = os.path.dirname(os.path.abspath(__file__))
filename = self._mock_responses.get(command)
if filename is not None:
if filename.startswith('aa55'):
return ProtocolResponse(bytes.fromhex(filename), command)
with open(root_dir + '/sample/es/' + filename, 'r') as f:
response = bytes.fromhex(f.read())
if not command.validator(response):
Expand All @@ -52,6 +54,8 @@ def __init__(self, methodName='runTest'):
self.mock_response(self._READ_DEVICE_VERSION_INFO, 'GW5048D-ES_device_info.hex')
self.mock_response(self._READ_DEVICE_RUNNING_DATA, 'GW5048D-ES_running_data.hex')
self.mock_response(self._READ_DEVICE_SETTINGS_DATA, 'GW5048D-ES_settings_data.hex')
self.mock_response(Aa55ReadCommand(1793, 1), 'aa557fc0019a08000000000000007f0360')
self.mock_response(Aa55ReadCommand(1800, 1), 'aa557fc0019a02007f035a')

def test_GW5048D_ES_device_info(self):
self.loop.run_until_complete(self.read_device_info())
Expand Down Expand Up @@ -163,6 +167,16 @@ def test_read_setting(self):
data = self.loop.run_until_complete(self.read_setting('grid_export_limit'))
self.assertEqual(10000, data)

data = self.loop.run_until_complete(self.read_setting('eco_mode_1'))
self.assertEqual(
"EcoModeV1(id_='eco_mode_1', offset=1793, name='Eco Mode Group 1', size_=8, unit='', kind=<SensorKind.BAT: 4>)",
repr(data))
data = self.loop.run_until_complete(self.read_setting('eco_mode_2_switch'))
self.assertEqual(0, data)

def test_write_setting(self):
self.loop.run_until_complete(self.write_setting('eco_mode_2_switch', 0))


class GW5048_EM_Test(EsMock):

Expand Down

0 comments on commit 337a799

Please sign in to comment.