diff --git a/custom_components/generac/__init__.py b/custom_components/generac/__init__.py index 27bdc21..5d109cb 100644 --- a/custom_components/generac/__init__.py +++ b/custom_components/generac/__init__.py @@ -11,13 +11,13 @@ from homeassistant.exceptions import ConfigEntryNotReady from .api import GeneracApiClient -from .utils import async_client_session from .const import CONF_PASSWORD from .const import CONF_USERNAME from .const import DOMAIN from .const import PLATFORMS from .const import STARTUP_MESSAGE from .coordinator import GeneracDataUpdateCoordinator +from .utils import async_client_session _LOGGER: logging.Logger = logging.getLogger(__package__) diff --git a/custom_components/generac/config_flow.py b/custom_components/generac/config_flow.py index d595ce1..a75d5c8 100644 --- a/custom_components/generac/config_flow.py +++ b/custom_components/generac/config_flow.py @@ -6,13 +6,12 @@ from homeassistant.core import callback from .api import GeneracApiClient -from .utils import async_client_session from .api import InvalidCredentialsException from .const import CONF_OPTIONS from .const import CONF_PASSWORD from .const import CONF_USERNAME from .const import DOMAIN - +from .utils import async_client_session _LOGGER: logging.Logger = logging.getLogger(__package__) diff --git a/custom_components/generac/utils.py b/custom_components/generac/utils.py index 1be734a..4b48267 100644 --- a/custom_components/generac/utils.py +++ b/custom_components/generac/utils.py @@ -1,7 +1,6 @@ """Helper to create an aiohttp client session for the Generac API.""" - -from aiohttp import ClientSession, CookieJar - +from aiohttp import ClientSession +from aiohttp import CookieJar from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client @@ -11,4 +10,3 @@ async def async_client_session(hass: HomeAssistant) -> ClientSession: return aiohttp_client.async_create_clientsession( hass, cookie_jar=CookieJar(unsafe=True, quote_cookie=False) ) - \ No newline at end of file diff --git a/local_test.py b/local_test.py index 61f5442..9cc715a 100644 --- a/local_test.py +++ b/local_test.py @@ -1,12 +1,25 @@ +# This script is a standalone test for the Generac API client. +# It logs in to the Generac API using credentials from environment variables +# and prints the device data in JSON format using a custom JSON encoder. +# Make sure to set the environment variables GENERAC_USER and GENERAC_PASS before running this script +# to avoid hardcoding sensitive information in the script. +# This script is intended to be run outside of Home Assistant, for testing purposes. +# It uses the aiohttp library for asynchronous HTTP requests and custom JSON encoding for dataclasses. +# Ensure you have aiohttp installed in your environment. +# If you're using Home Assistant, aiohttp is already included. +# You can install it using pip: pip install aiohttp import asyncio import dataclasses import json import logging import os -import aiohttp + +import aiohttp # type: ignore from custom_components.generac.api import GeneracApiClient logging.basicConfig(level=logging.DEBUG) + + # Your custom JSON encoder class EnhancedJSONEncoder(json.JSONEncoder): def default(self, o): @@ -14,9 +27,10 @@ def default(self, o): return dataclasses.asdict(o) return super().default(o) + # Async main logic async def main(): - jar = aiohttp.CookieJar(unsafe=True,quote_cookie=False) + jar = aiohttp.CookieJar(unsafe=True, quote_cookie=False) async with aiohttp.ClientSession(cookie_jar=jar) as session: api = GeneracApiClient( os.environ["GENERAC_USER"], os.environ["GENERAC_PASS"], session @@ -25,6 +39,7 @@ async def main(): device_data = await api.get_device_data() print(json.dumps(device_data, cls=EnhancedJSONEncoder)) + # Run it using asyncio.run (preferred method in Python 3.7+) if __name__ == "__main__": - asyncio.run(main()) \ No newline at end of file + asyncio.run(main())