Skip to content

Commit

Permalink
fix satellite modbus error (#1732)
Browse files Browse the repository at this point in the history
* fix satellite modbus error

* mock contextmanager
  • Loading branch information
LKuemmel authored Jul 8, 2024
1 parent af54e07 commit 1a47509
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ def get_values(self) -> None:
if self.version is not None:
with self.__client_error_context:
try:
self._client.check_hardware(self.fault_state)
if self.version is False:
raise ValueError(
"Firmware des openWB Satellit ist nicht mit openWB 2 kompatibel. "
"Bitte den Support kontaktieren.")
self.delay_second_cp(self.CP0_DELAY)
with self._client.client:
self._client.check_hardware(self.fault_state)
if self.version is False:
raise ValueError(
"Firmware des openWB Satellit ist nicht mit openWB 2 kompatibel. "
"Bitte den Support kontaktieren.")
currents = self._client.meter_client.get_currents()
phases_in_use = sum(1 for current in currents if current > 3)
plug_state, charge_state, _ = self._client.evse_client.get_plug_charge_state()
Expand Down Expand Up @@ -112,9 +112,9 @@ def set_current(self, current: float) -> None:
with SingleComponentUpdateContext(self.fault_state, update_always=False):
with self.__client_error_context:
try:
self._client.check_hardware(self.fault_state)
self.delay_second_cp(self.CP0_DELAY)
with self._client.client:
self._client.check_hardware(self.fault_state)
if self.version:
self._client.evse_client.set_current(int(current))
else:
Expand All @@ -128,8 +128,8 @@ def switch_phases(self, phases_to_use: int, duration: int) -> None:
with SingleComponentUpdateContext(self.fault_state, update_always=False):
with self.__client_error_context:
try:
self._client.check_hardware(self.fault_state)
with self._client.client:
self._client.check_hardware(self.fault_state)
if phases_to_use == 1:
self._client.client.delegate.write_register(
0x0001, 256, unit=self.ID_PHASE_SWITCH_UNIT)
Expand Down
14 changes: 10 additions & 4 deletions packages/modules/common/hardware_check_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from typing import List, Optional, Tuple, Union
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch

import pytest
from modules.common import sdm
from modules.common.evse import Evse
from modules.common.hardware_check import (
EVSE_BROKEN, LAN_ADAPTER_BROKEN, METER_BROKEN, METER_NO_SERIAL_NUMBER, METER_PROBLEM, USB_ADAPTER_BROKEN,
SeriesHardwareCheckMixin, check_meter_values)
from modules.common.modbus import NO_CONNECTION, ModbusSerialClient_, ModbusTcpClient_
from modules.common.modbus import NO_CONNECTION, ModbusClient, ModbusSerialClient_, ModbusTcpClient_
from modules.conftest import SAMPLE_IP, SAMPLE_PORT
from modules.internal_chargepoint_handler.clients import ClientHandler

Expand Down Expand Up @@ -51,9 +51,12 @@ def test_hardware_check_fails(evse_side_effect,
handle_exception_mock = Mock(side_effect=handle_exception_side_effect, return_value=handle_exception_return_value)
monkeypatch.setattr(SeriesHardwareCheckMixin, "handle_exception", handle_exception_mock)

mock_modbus_client = MagicMock(spec=client_spec, address=SAMPLE_IP, port=SAMPLE_PORT)
mock_modbus_client.__enter__.return_value = mock_modbus_client

# execution and evaluation
with pytest.raises(Exception, match=expected_error_msg):
ClientHandler(0, Mock(spec=client_spec, address=SAMPLE_IP, port=SAMPLE_PORT), [1], Mock())
ClientHandler(0, mock_modbus_client, [1], Mock())


def test_hardware_check_succeeds(monkeypatch):
Expand All @@ -66,9 +69,12 @@ def test_hardware_check_succeeds(monkeypatch):
mock_find_meter_client = Mock(spec=sdm.Sdm630, return_value=mock_meter_client)
monkeypatch.setattr(ClientHandler, "find_meter_client", mock_find_meter_client)

mock_modbus_client = MagicMock(spec=ModbusClient)
mock_modbus_client.__enter__.return_value = mock_modbus_client

# execution and evaluation
# keine Exception
ClientHandler(0, Mock(), [1], Mock())
ClientHandler(0, mock_modbus_client, [1], Mock())


@pytest.mark.parametrize(
Expand Down
3 changes: 2 additions & 1 deletion packages/modules/internal_chargepoint_handler/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def __init__(self,
self.evse_client = self._evse_factory(client, evse_ids)
self.meter_client = self.find_meter_client(CP0_METERS if self.local_charge_point_num == 0 else CP1_METERS,
client)
self.check_hardware(fault_state)
with client:
self.check_hardware(fault_state)
self.read_error = 0

def _evse_factory(self, client: Union[ModbusSerialClient_, ModbusTcpClient_], evse_ids: List[int]) -> evse.Evse:
Expand Down

0 comments on commit 1a47509

Please sign in to comment.