Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from mjmeli/add-gateway-to-select-default-meter
Browse files Browse the repository at this point in the history
add gateway to the select_default_meter return
  • Loading branch information
mjmeli authored Oct 25, 2021
2 parents cfe6b74 + 2242e88 commit fc0ccff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
8 changes: 5 additions & 3 deletions examples/example_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ async def main() -> None: # noqa
jsonpickle.encode(account_details, indent=2, unpicklable=False)
)

_LOGGER.info(f"Searching for default meter")
meter = await duke_energy.select_default_meter()
_LOGGER.info(f"Selected default meter {meter.serial_num}")
_LOGGER.info("Searching for default meter")
meter, gateway = await duke_energy.select_default_meter()
_LOGGER.info(
f"Selected default meter {meter.serial_num} with gateway {gateway.id}"
)

_LOGGER.info("Getting gateway status:")
gw_status = await duke_energy.get_gateway_status()
Expand Down
33 changes: 18 additions & 15 deletions src/pyduke_energy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import date, datetime, timedelta, timezone
import logging
import time
from typing import List, Optional
from typing import List, Optional, Tuple
from urllib.parse import urljoin

from aiohttp import ClientSession, ClientTimeout, FormData
Expand Down Expand Up @@ -159,7 +159,7 @@ def reset_selected_meter(self) -> None:
self._gateway_auth_info.activation_date = None
self._gateway_auth_info.clear_access_token() # resets authentication

async def select_default_meter(self) -> MeterInfo:
async def select_default_meter(self) -> Tuple[MeterInfo, GatewayStatus]:
"""Find the meter that is used for the gateway by iterating through the accounts and meters."""
account_list = await self.get_account_list()
self._logger.debug(
Expand All @@ -169,9 +169,11 @@ async def select_default_meter(self) -> MeterInfo:
)

for account in account_list:
found_meter = await self._check_account_for_default_meter(account)
if found_meter:
return found_meter
found_meter, found_gateway = await self._check_account_for_default_meter(
account
)
if found_meter and found_gateway:
return found_meter, found_gateway

# No meters were found. This is an error.
self.reset_selected_meter() # in case any were set by the failing code above
Expand All @@ -181,7 +183,7 @@ async def select_default_meter(self) -> MeterInfo:

async def _check_account_for_default_meter(
self, account: Account
) -> Optional[MeterInfo]:
) -> Tuple[Optional[MeterInfo], Optional[GatewayStatus]]:
try:
self._logger.debug("Checking account '%s' for gateway", account.src_acct_id)
account_details = await self.get_account_details(account)
Expand All @@ -193,11 +195,12 @@ async def _check_account_for_default_meter(
),
)
for meter in account_details.meter_infos:
found_meter = await self._check_account_meter_for_default_meter(
account, meter
)
if found_meter:
return found_meter
(
found_meter,
found_gateway,
) = await self._check_account_meter_for_default_meter(account, meter)
if found_meter and found_gateway:
return found_meter, found_gateway
except Exception as ex:
# Try the next account if anything fails above
self._logger.debug(
Expand All @@ -206,11 +209,11 @@ async def _check_account_for_default_meter(
ex,
)

return None
return None, None

async def _check_account_meter_for_default_meter(
self, account: Account, meter: MeterInfo
) -> Optional[MeterInfo]:
) -> Tuple[Optional[MeterInfo], Optional[GatewayStatus]]:
try:
self._logger.debug(
"Checking meter '%s' for gateway [meter_type=%s, is_certified_smart_meter=%s]",
Expand All @@ -233,7 +236,7 @@ async def _check_account_meter_for_default_meter(
meter.serial_num,
gw_status.id,
)
return meter
return meter, gw_status

self._logger.debug("No gateway status for meter '%s'", meter.serial_num)
except Exception as ex:
Expand All @@ -245,7 +248,7 @@ async def _check_account_meter_for_default_meter(
ex,
)

return None
return None, None

async def get_gateway_status(self) -> GatewayStatus:
"""Get the status of the selected gateway."""
Expand Down

0 comments on commit fc0ccff

Please sign in to comment.