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

Better logging #141

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions custom_components/fusion_solar/device_real_kpi_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .fusion_solar.const import ATTR_DEVICE_REAL_KPI_DEV_ID, ATTR_DEVICE_REAL_KPI_DATA_ITEM_MAP, \
PARAM_DEVICE_TYPE_ID_STRING_INVERTER, PARAM_DEVICE_TYPE_ID_EMI, PARAM_DEVICE_TYPE_ID_GRID_METER, \
PARAM_DEVICE_TYPE_ID_RESIDENTIAL_INVERTER, PARAM_DEVICE_TYPE_ID_BATTERY, PARAM_DEVICE_TYPE_ID_POWER_SENSOR
from .fusion_solar.openapi.openapi_api import FusionSolarOpenApiAccessFrequencyTooHighError
from .fusion_solar.openapi.openapi_api import FusionSolarOpenApiError, FusionSolarOpenApiAccessFrequencyTooHighError

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -61,6 +61,8 @@ async def _async_update_data(self):
except FusionSolarOpenApiAccessFrequencyTooHighError as e:
self.skip = True
return False
except FusionSolarOpenApiError as error:
raise UpdateFailed(f'OpenAPI Error: {error}')

for response_data in response:
key = f'{DOMAIN}-{response_data[ATTR_DEVICE_REAL_KPI_DEV_ID]}'
Expand All @@ -86,7 +88,8 @@ def device_ids_grouped_per_type_id(self):

station_from_registry = device_registry.async_get_device(identifiers={(DOMAIN, device.station_code)})
if station_from_registry is not None and station_from_registry.disabled:
_LOGGER.debug(f'Device {device.name} ({device.device_id}) linked to a disabled station ({device.station_code}).')
_LOGGER.debug(
f'Device {device.name} ({device.device_id}) linked to a disabled station ({device.station_code}).')
continue

if device.type_id not in device_ids_grouped_per_type_id:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ def getRealTimeKpi(self, id: str):
response = get(url, headers=headers)
jsonData = response.json()

if not jsonData[ATTR_SUCCESS]:
raise FusionSolarKioskApiError(
f'Retrieving the data failed with failCode: {jsonData[ATTR_FAIL_CODE]}, data: {jsonData[ATTR_DATA]}')
if ATTR_SUCCESS not in jsonData or not jsonData[ATTR_SUCCESS]:
raise FusionSolarKioskApiError(f'Retrieving the data failed. Raw response: {response.text}')

# convert encoded html string to JSON
jsonData[ATTR_DATA] = json.loads(html.unescape(jsonData[ATTR_DATA]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ def _do_call(self, url: str, json: dict):
f'Retrieving the data for {url} failed with failCode: {json_data[ATTR_FAIL_CODE]}, message: {json_data[ATTR_DATA]}'
)

if ATTR_DATA not in json_data:
raise FusionSolarOpenApiError(f'Retrieving the data failed. Raw response: {response.text}')

return json_data

except KeyError as error:
Expand All @@ -206,9 +209,9 @@ class FusionSolarOpenApiError(Exception):
pass


class FusionSolarOpenApiAccessFrequencyTooHighError(Exception):
class FusionSolarOpenApiAccessFrequencyTooHighError(FusionSolarOpenApiError):
pass


class FusionSolarOpenApiErrorInvalidAccessToCurrentInterfaceError(Exception):
class FusionSolarOpenApiErrorInvalidAccessToCurrentInterfaceError(FusionSolarOpenApiError):
pass
21 changes: 15 additions & 6 deletions custom_components/fusion_solar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.const import CONF_NAME, CONF_URL, CONF_HOST, CONF_USERNAME, CONF_PASSWORD
from homeassistant.exceptions import IntegrationError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .fusion_solar.const import ATTR_REALTIME_POWER, ATTR_TOTAL_CURRENT_DAY_ENERGY, \
ATTR_TOTAL_CURRENT_MONTH_ENERGY, ATTR_TOTAL_CURRENT_YEAR_ENERGY, ATTR_TOTAL_LIFETIME_ENERGY, \
Expand All @@ -19,8 +19,8 @@
PARAM_DEVICE_TYPE_ID_STRING_INVERTER, PARAM_DEVICE_TYPE_ID_GRID_METER, PARAM_DEVICE_TYPE_ID_RESIDENTIAL_INVERTER, \
PARAM_DEVICE_TYPE_ID_POWER_SENSOR, PARAM_DEVICE_TYPE_ID_EMI, PARAM_DEVICE_TYPE_ID_BATTERY
from .fusion_solar.kiosk.kiosk import FusionSolarKiosk
from .fusion_solar.kiosk.kiosk_api import FusionSolarKioskApi
from .fusion_solar.openapi.openapi_api import FusionSolarOpenApi
from .fusion_solar.kiosk.kiosk_api import FusionSolarKioskApi, FusionSolarKioskApiError
from .fusion_solar.openapi.openapi_api import FusionSolarOpenApi, FusionSolarOpenApiError
from .fusion_solar.energy_sensor import FusionSolarEnergySensorTotalCurrentDay, \
FusionSolarEnergySensorTotalCurrentMonth, FusionSolarEnergySensorTotalCurrentYear, \
FusionSolarEnergySensorTotalLifetime
Expand Down Expand Up @@ -76,7 +76,10 @@ async def async_update_kiosk_data():
_LOGGER.debug(DOMAIN)
_LOGGER.debug(kiosk.id)

data[f'{DOMAIN}-{kiosk.id}'] = await hass.async_add_executor_job(api.getRealTimeKpi, kiosk.id)
try:
data[f'{DOMAIN}-{kiosk.id}'] = await hass.async_add_executor_job(api.getRealTimeKpi, kiosk.id)
except FusionSolarKioskApiError as error:
raise UpdateFailed(f'Kiosk API Error: {error}')

return data

Expand Down Expand Up @@ -597,7 +600,10 @@ async def async_update_station_real_kpi_data():
if station_codes is None or len(station_codes) == 0:
return data

response = await hass.async_add_executor_job(api.get_station_real_kpi, station_codes)
try:
response = await hass.async_add_executor_job(api.get_station_real_kpi, station_codes)
except FusionSolarOpenApiError as error:
raise UpdateFailed(f'OpenAPI Error: {error}')

for response_data in response:
data[f'{DOMAIN}-{response_data[ATTR_STATION_CODE]}'] = response_data[ATTR_STATION_REAL_KPI_DATA_ITEM_MAP]
Expand Down Expand Up @@ -679,7 +685,10 @@ async def async_update_station_year_kpi_data():
if station_codes is None or len(station_codes) == 0:
return data

response = await hass.async_add_executor_job(api.get_kpi_station_year, station_codes)
try:
response = await hass.async_add_executor_job(api.get_kpi_station_year, station_codes)
except FusionSolarOpenApiError as error:
raise UpdateFailed(f'OpenAPI Error: {error}')

for response_data in response:
key = f'{DOMAIN}-{response_data[ATTR_STATION_CODE]}'
Expand Down
Loading