Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redo call is getStationList is not available #78

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions custom_components/fusion_solar/fusion_solar/openapi/openapi_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
ATTR_DEVICE_LONGITUDE, ATTR_DEVICE_NAME, ATTR_DEVICE_SOFTWARE_VERSION, ATTR_DEVICE_STATION_CODE, \
ATTR_DEVICE_TYPE_ID, ATTR_FAIL_CODE, ATTR_LIST, ATTR_PARAMS, ATTR_PARAMS_CURRENT_TIME, ATTR_PLANT_ADDRESS, \
ATTR_PLANT_CODE, ATTR_PLANT_NAME, ATTR_STATION_ADDRESS, ATTR_STATION_CODE, ATTR_STATION_CONTACT_PERSON, \
ATTR_STATION_LINKMAN, ATTR_STATION_NAME
ATTR_STATION_LINKMAN, ATTR_STATION_NAME, ATTR_MESSAGE

from .station import FusionSolarStation
from .device import FusionSolarDevice
Expand All @@ -25,12 +25,6 @@ def __init__(self, host: str, username: str, password: str):
self._username = username
self._password = password

def _should_use_stations(self) -> bool:
if self._host.startswith('https://au5.'):
return True

return False

def login(self) -> str:
url = self._host + '/thirdData/login'
headers = {
Expand All @@ -54,12 +48,13 @@ def login(self) -> str:
raise FusionSolarOpenApiError(f'Could not login with given credentials')

def get_station_list(self):
if self._should_use_stations():
return self.stations()

url = self._host + '/thirdData/getStationList'
json = {}
response = self._do_call(url, json)
try:
response = self._do_call(url, json)
except FusionSolarOpenApiErrorInvalidAccessToCurrentInterfaceError as error:
_LOGGER.debug(f'Could not use getStationList, trying stations: {error}')
return self.stations()

if ATTR_PARAMS in response and ATTR_PARAMS_CURRENT_TIME in response[ATTR_PARAMS]:
self._last_station_list_current_time = response[ATTR_PARAMS][ATTR_PARAMS_CURRENT_TIME]
Expand Down Expand Up @@ -190,6 +185,9 @@ def _do_call(self, url: str, json: dict):
self._token = None
return self._do_call(url, json)

if ATTR_FAIL_CODE in json_data and json_data[ATTR_FAIL_CODE] == 401:
raise FusionSolarOpenApiErrorInvalidAccessToCurrentInterfaceError(json_data[ATTR_MESSAGE])

if ATTR_FAIL_CODE in json_data and json_data[ATTR_FAIL_CODE] == 407:
_LOGGER.debug(
f'Access frequency to high, while calling {url}: {json_data[ATTR_DATA]}, failcode: {json_data[ATTR_FAIL_CODE]}')
Expand All @@ -216,3 +214,7 @@ class FusionSolarOpenApiError(Exception):

class FusionSolarOpenApiAccessFrequencyTooHighError(Exception):
pass


class FusionSolarOpenApiErrorInvalidAccessToCurrentInterfaceError(Exception):
pass