-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an opinionated client implementation
Though it's nice to have a raw restful client, it's better to have something that exposes a class model. This is that client, focussed towards the type of calls useful for energy consumers. This is just the start, and has a method to simply return the meters attached to an account as a list of python objects. This also drops support for python 3.7
- Loading branch information
1 parent
6505c61
commit 8497dbf
Showing
19 changed files
with
1,163 additions
and
531 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import List | ||
|
||
from .mappers import meters_from_response | ||
from octopus_energy import Meter, OctopusEnergyRestClient | ||
|
||
|
||
class OctopusEnergyConsumerClient: | ||
"""An opinionated take on the consumer features of the Octopus Energy API. | ||
This client uses python model classes instead of raw json-as-a-dict. It does not expose all of | ||
the octopus energy API, only bits that are useful for a consumer of octopus energy products. It | ||
focuses on consumption and prices, and blending the two of them together to provide useful | ||
data for a consumer. | ||
This client is useful if you want to know your own personal energy consumption statistics. | ||
This client uses async i/o. | ||
""" | ||
|
||
def __init__(self, account_number: str, api_token: str): | ||
"""Initializes the Octopus Energy Consumer Client. | ||
Args: | ||
account_number: Your Octopus Energy Account Number. | ||
api_token: Your Octopus Energy API Key. | ||
""" | ||
self.account_number = account_number | ||
self.rest_client = OctopusEnergyRestClient(api_token) | ||
|
||
def __enter__(self): | ||
raise TypeError("Use async context manager (async with) instead") | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pass | ||
|
||
async def __aenter__(self): | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
await self.close() | ||
|
||
async def close(self): | ||
"""Clean up resources used by the client. | ||
Once the client is closed, you cannot use it to make any further calls. | ||
""" | ||
await self.rest_client.close() | ||
|
||
async def get_meters(self) -> List[Meter]: | ||
"""Gets all meters associated with your account.""" | ||
return meters_from_response(await self.rest_client.get_account_details(self.account_number)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.