Skip to content

Commit

Permalink
fixes, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
LKuemmel committed May 25, 2022
1 parent 647c36c commit 340c1a7
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 35 deletions.
12 changes: 5 additions & 7 deletions modules/bezug_lgessv1/main.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/bin/bash
OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
OPENWBBASEDIR=$(cd "$(dirname "$0")/../../" && pwd)
RAMDISKDIR="${OPENWBBASEDIR}/ramdisk"
MODULEDIR=$(cd `dirname $0` && pwd)
#DMOD="EVU"
DMOD="MAIN"
Debug=$debug


#For development only
#Debug=1
Expand All @@ -15,9 +13,9 @@ else
MYLOGFILE="${RAMDISKDIR}/evu.log"
fi

bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.lg.device" "counter" "${lgessv1ip}" "${lgessv1pass}" >>${MYLOGFILE} 2>&1
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.lg.device" "counter" "${lgessv1ip}" "${lgessv1pass}" >>"${MYLOGFILE}" 2>&1
ret=$?

openwbDebugLog ${DMOD} 2 "EVU RET: ${ret}"
wattbezug=$(<${RAMDISKDIR}/wattbezug)
echo $wattbezug
wattbezug=$(<"${RAMDISKDIR}"/wattbezug)
echo "$wattbezug"
11 changes: 4 additions & 7 deletions modules/speicher_lgessv1/main.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/bin/bash

OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
OPENWBBASEDIR=$(cd "$(dirname "$0")/../../" && pwd)
RAMDISKDIR="${OPENWBBASEDIR}/ramdisk"
#MODULEDIR=$(cd `dirname $0` && pwd)
#DMOD="BATT"
DMOD="MAIN"
Debug=$debug


#For Development only
#Debug=1
Expand All @@ -17,13 +15,12 @@ else
fi


openwbDebugLog ${DMOD} 2 "Speicher Version: ${alphav123}"

bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.lg.device" "bat" "${lgessv1ip}" "${lgessv1pass}" >>$MYLOGFILE 2>&1
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.lg.device" "bat" "${lgessv1ip}" "${lgessv1pass}" >>"$MYLOGFILE" 2>&1
ret=$?

openwbDebugLog ${DMOD} 2 "RET: ${ret}"

speicherleistung=$(<${RAMDISKDIR}/speicherleistung)
speicherleistung=$(<"${RAMDISKDIR}"/speicherleistung)

openwbDebugLog ${DMOD} 1 "BattLeistung: ${speicherleistung}"
11 changes: 4 additions & 7 deletions modules/wr_lgessv1/main.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/bin/bash

OPENWBBASEDIR=$(cd `dirname $0`/../../ && pwd)
OPENWBBASEDIR=$(cd "$(dirname "$0")/../../" && pwd)
RAMDISKDIR="${OPENWBBASEDIR}/ramdisk"
#MODULEDIR=$(cd `dirname $0` && pwd)
DMOD="PV"
#DMOD="MAIN"
Debug=$debug

#For Development only
#Debug=1
Expand All @@ -16,10 +13,10 @@ else
MYLOGFILE="${RAMDISKDIR}/nurpv.log"
fi

bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.lg.device" "inverter" "${lgessv1ip}" "${lgessv1pass}" "1">>$MYLOGFILE 2>&1
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.lg.device" "inverter" "${lgessv1ip}" "${lgessv1pass}" "1">>"$MYLOGFILE" 2>&1
ret=$?

openwbDebugLog ${DMOD} 2 "RET: ${ret}"

watt=$(<${RAMDISKDIR}/pvwatt)
echo ${watt}
watt=$(<"${RAMDISKDIR}"/pvwatt)
echo "${watt}"
27 changes: 13 additions & 14 deletions packages/modules/lg/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ def update(self) -> None:
if self._components:
with MultiComponentUpdateContext(self._components):
session = req.get_http_session()
response = self.__request_data(session)
response = self._request_data(session)
# missing "auth" in response indicates success
if response.get('auth') == "auth_key failed" or response.get('auth') == "auth timeout" or response.get('auth') == "not done":
self.__update_session_key(session)
response = self.__request_data(session)
if (response.get('auth') == "auth_key failed" or
response.get('auth') == "auth timeout" or
response.get('auth') == "not done"):
self._update_session_key(session)
response = self._request_data(session)

for component in self._components:
self._components[component].update(response)
Expand All @@ -129,20 +131,17 @@ def update(self) -> None:
": Es konnten keine Werte gelesen werden, da noch keine Komponenten konfiguriert wurden."
)

def __update_session_key(self, session: Session):
def _update_session_key(self, session: Session):
try:
headers = {'Content-Type': 'application/json', }
data = json.dumps({"password": self.config.configuration.password})
response = session.post(self.config.configuration.ip_address+'/v1/login', headers=headers,
data=data, verify=False, timeout=5).json()
log.MainLogger().debug("response: " + str(response))
session_key = response["auth_key"]
outjson = {"auth_key": session_key}
response = session.put("https://"+self.config.configuration.ip_address+'/v1/login', headers=headers,
data=data, verify=False, timeout=5).json()
self.session_key = response["auth_key"]
except (HTTPError, KeyError):
raise FaultState.error("login failed! check password!")
self.session_key = session_key

def __request_data(self, session: Session) -> Dict:
def _request_data(self, session: Session) -> Dict:
headers = {'Content-Type': 'application/json', }
data = json.dumps({"auth_key": self.session_key})
return session.post("https://"+self.config.configuration.ip_address + "/v1/user/essinfo/home",
Expand All @@ -159,8 +158,8 @@ def read_legacy(component_type: str, ip: str, password: str, num: Optional[int]
"inverter": inverter
}
device_config = get_default_config()
device_config.configuration.ip = ip
device_config.configuration.password = password
device_config["configuration"]["ip_address"] = ip
device_config["configuration"]["password"] = password
dev = Device(device_config)

if os.path.isfile("/var/www/html/openWB/ramdisk/ess_session_key"):
Expand Down
145 changes: 145 additions & 0 deletions packages/modules/lg/lg_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import pytest
from unittest.mock import Mock

from modules.lg import bat, counter, device, inverter
from modules.common.component_state import BatState, CounterState, InverterState
from test_utils.mock_ramdisk import MockRamdisk


@pytest.fixture
def mock_ramdisk(monkeypatch):
return MockRamdisk(monkeypatch)


@pytest.fixture
def dev():
device_config = device.get_default_config()
device_config["configuration"]["ip_address"] = API_URL
device_config["configuration"]["password"] = "some password"
dev = device.Device(device_config)
dev.session_key = "67567d76-0c83-11ea-8a59-d84fb802005a"
return dev


def assert_battery_state_correct(state: BatState):
assert state.soc == 14.58333
assert state.power == -372


def assert_counter_state_correct(state: CounterState):
assert state.power == 11


def assert_inverter_state_correct(state: InverterState):
assert state.power == 0


def test_valid_login(monkeypatch, dev):
# setup
mock_bat_value_store = Mock()
monkeypatch.setattr(bat, "get_bat_value_store", Mock(return_value=mock_bat_value_store))
mock_counter_value_store = Mock()
monkeypatch.setattr(counter, "get_counter_value_store", Mock(return_value=mock_counter_value_store))
mock_inverter_value_store = Mock()
monkeypatch.setattr(inverter, "get_inverter_value_store", Mock(return_value=mock_inverter_value_store))
monkeypatch.setattr(device.Device, "_request_data", Mock(return_value=sample_auth_key_valid))
component_config = bat.get_default_config()
component_config["id"] = None
dev.add_component(component_config)
component_config = counter.get_default_config()
component_config["id"] = 1
dev.add_component(component_config)
component_config = inverter.get_default_config()
component_config["id"] = 2
dev.add_component(component_config)

# execution
dev.update()

# evaluation
assert_battery_state_correct(mock_bat_value_store.set.call_args[0][0])
assert_counter_state_correct(mock_counter_value_store.set.call_args[0][0])
assert_inverter_state_correct(mock_inverter_value_store.set.call_args[0][0])


def test_update_session_key(monkeypatch, dev):
# setup
mock_bat_value_store = Mock()
monkeypatch.setattr(bat, "get_bat_value_store", Mock(return_value=mock_bat_value_store))
mock_counter_value_store = Mock()
monkeypatch.setattr(counter, "get_counter_value_store", Mock(return_value=mock_counter_value_store))
mock_inverter_value_store = Mock()
monkeypatch.setattr(inverter, "get_inverter_value_store", Mock(return_value=mock_inverter_value_store))
monkeypatch.setattr(device.Device, "_update_session_key", Mock())
monkeypatch.setattr(device.Device, "_request_data", Mock(
side_effect=[sample_auth_key_invalid, sample_auth_key_valid]))
component_config = bat.get_default_config()
component_config["id"] = None
dev.add_component(component_config)
component_config = counter.get_default_config()
component_config["id"] = 1
dev.add_component(component_config)
component_config = inverter.get_default_config()
component_config["id"] = 2
dev.add_component(component_config)

# execution
dev.update()

# evaluation
assert_battery_state_correct(mock_bat_value_store.set.call_args[0][0])
assert_counter_state_correct(mock_counter_value_store.set.call_args[0][0])
assert_inverter_state_correct(mock_inverter_value_store.set.call_args[0][0])


API_URL = "https://sample-address"

sample_session_key = {
"status": "success",
"auth_key": "67567d76-0c83-11ea-8a59-d84fb802005a",
"regnum": "XXXXXXXXXXXX",
"role": "installer"
}

sample_auth_key_invalid = {
"auth": "auth_key failed"
}

sample_auth_key_valid = {
"statistics":
{
"pcs_pv_total_power": "0",
"batconv_power": "372",
"bat_use": "1",
"bat_status": "2",
"bat_user_soc": "14.58333",
"load_power": "361",
"load_today": "0.0",
"grid_power": "11",
"current_day_self_consumption": "94.8",
"current_pv_generation_sum": "7415",
"current_grid_feed_in_energy": "385"
},
"direction":
{
"is_direct_consuming_": "0",
"is_battery_charging_": "0",
"is_battery_discharging_": "1",
"is_grid_selling_": "0",
"is_grid_buying_": "0",
"is_charging_from_grid_": "0"
},
"operation":
{
"status": "start",
"mode": "1"
},
"wintermode":
{
"winter_status": "on"
},
"pcs_fault":
{
"pcs_status": "pcs_ok"
}
}

0 comments on commit 340c1a7

Please sign in to comment.