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

More verbose options #76

Merged
merged 2 commits into from
Apr 1, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ pytest

# Run demo
python src/demo.py --help
# To output debug logs to a file, change DEBUG_LOG_RESPONSE to True in opower.py and run:
python src/demo.py --verbose 2> out.txt
# To output debug logs and API responses to a file run:
python src/demo.py -vv 2> out.txt

# Build package
python -m pip install build
Expand Down
6 changes: 4 additions & 2 deletions src/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ async def _main() -> None:
action="store_true",
)
parser.add_argument(
"-v", "--verbose", help="enable verbose logging", action="store_true"
"-v", "--verbose", help="enable verbose logging", action="count", default=0
)
args = parser.parse_args()

logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
logging.basicConfig(
level=logging.DEBUG - args.verbose + 1 if args.verbose > 0 else logging.INFO
)

utility = args.utility or input(f"Utility, one of {supported_utilities}: ")
username = args.username or input("Username: ")
Expand Down
21 changes: 12 additions & 9 deletions src/opower/opower.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from .utilities import UtilityBase

_LOGGER = logging.getLogger(__file__)
DEBUG_LOG_RESPONSE = False


class MeterType(Enum):
Expand Down Expand Up @@ -245,8 +244,9 @@ async def async_get_forecast(self) -> list[Forecast]:
url, headers=self._get_headers(), raise_for_status=True
) as resp:
result = await resp.json()
if DEBUG_LOG_RESPONSE:
_LOGGER.debug("Fetched: %s", json.dumps(result, indent=2))
_LOGGER.log(
logging.DEBUG - 1, "Fetched: %s", json.dumps(result, indent=2)
)
except ClientResponseError as err:
# For some customers utilities don't provide forecast
_LOGGER.debug("Ignoring combined-forecast error: %s", err.status)
Expand Down Expand Up @@ -310,8 +310,9 @@ async def _async_get_customers(self) -> list[Any]:
url, headers=self._get_headers(), raise_for_status=True
) as resp:
result = await resp.json()
if DEBUG_LOG_RESPONSE:
_LOGGER.debug("Fetched: %s", json.dumps(result, indent=2))
_LOGGER.log(
logging.DEBUG - 1, "Fetched: %s", json.dumps(result, indent=2)
)
for customer in result["customers"]:
self.customers.append(customer)
assert self.customers
Expand All @@ -334,8 +335,9 @@ async def _async_get_user_accounts(self) -> list[Any]:
url, headers=self._get_headers(), raise_for_status=True
) as resp:
result = await resp.json()
if DEBUG_LOG_RESPONSE:
_LOGGER.debug("Fetched: %s", json.dumps(result, indent=2))
_LOGGER.log(
logging.DEBUG - 1, "Fetched: %s", json.dumps(result, indent=2)
)
for account in result["accounts"]:
self.user_accounts.append(account)

Expand Down Expand Up @@ -511,8 +513,9 @@ async def _async_fetch(
url, params=params, headers=headers, raise_for_status=True
) as resp:
result = await resp.json()
if DEBUG_LOG_RESPONSE:
_LOGGER.debug("Fetched: %s", json.dumps(result, indent=2))
_LOGGER.log(
logging.DEBUG - 1, "Fetched: %s", json.dumps(result, indent=2)
)
return list(result["reads"])
except ClientResponseError as err:
# Ignore server errors for BILL requests
Expand Down
12 changes: 5 additions & 7 deletions src/opower/utilities/smud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sacramento Municipal Utility District (SMUD)."""

#
# SMUD is a community owned, local monopoly power company in Sacramento, California.
#
Expand Down Expand Up @@ -237,7 +238,7 @@ def get_okta_url_from_response_redirect(

@staticmethod
async def log_response(response: ClientResponse, session: ClientSession) -> None:
"""Log any redirects and new cookies. Log full HTML when DEBUG_LOG_RESPONSE is set."""
"""Log any redirects and new cookies. Log full HTML when -vv is set."""
host = response.host # Is this the request URL or the final redirected url?

redirects = [r.url for r in response.history]
Expand All @@ -262,9 +263,6 @@ async def log_response(response: ClientResponse, session: ClientSession) -> None

SMUD.cookies[host] = last_cookie_names + response_cookie_names

if hasattr("opower", "DEBUG_LOG_RESPONSE") and getattr(
"opower", "DEBUG_LOG_RESPONSE"
):
response_html = await response.text()
_LOGGER.debug("Response %s:", response.url)
_LOGGER.debug(response_html)
response_html = await response.text()
_LOGGER.log(logging.DEBUG - 1, "Response %s:", response.url)
_LOGGER.log(logging.DEBUG - 1, response_html)
Loading