1+ # This script is a standalone test for the Generac API client.
2+ # It logs in to the Generac API using credentials from environment variables
3+ # and prints the device data in JSON format using a custom JSON encoder.
4+ # Make sure to set the environment variables GENERAC_USER and GENERAC_PASS before running this script
5+ # to avoid hardcoding sensitive information in the script.
6+ # This script is intended to be run outside of Home Assistant, for testing purposes.
7+ # It uses the aiohttp library for asynchronous HTTP requests and custom JSON encoding for dataclasses.
8+ # Ensure you have aiohttp installed in your environment.
9+ # If you're using Home Assistant, aiohttp is already included.
10+ # You can install it using pip: pip install aiohttp
111import asyncio
212import dataclasses
313import json
414import logging
515import os
6- import aiohttp
16+
17+ import aiohttp # type: ignore
718from custom_components .generac .api import GeneracApiClient
819
920logging .basicConfig (level = logging .DEBUG )
21+
22+
1023# Your custom JSON encoder
1124class EnhancedJSONEncoder (json .JSONEncoder ):
1225 def default (self , o ):
1326 if dataclasses .is_dataclass (o ):
1427 return dataclasses .asdict (o )
1528 return super ().default (o )
1629
30+
1731# Async main logic
1832async def main ():
19- jar = aiohttp .CookieJar (unsafe = True ,quote_cookie = False )
33+ jar = aiohttp .CookieJar (unsafe = True , quote_cookie = False )
2034 async with aiohttp .ClientSession (cookie_jar = jar ) as session :
2135 api = GeneracApiClient (
2236 os .environ ["GENERAC_USER" ], os .environ ["GENERAC_PASS" ], session
@@ -25,6 +39,7 @@ async def main():
2539 device_data = await api .get_device_data ()
2640 print (json .dumps (device_data , cls = EnhancedJSONEncoder ))
2741
42+
2843# Run it using asyncio.run (preferred method in Python 3.7+)
2944if __name__ == "__main__" :
30- asyncio .run (main ())
45+ asyncio .run (main ())
0 commit comments