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

Refactor package for better model naming #441

Merged
merged 2 commits into from
Jul 9, 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
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ async def main():
email="test@autarco.com",
password="password",
) as client:
account = await client.get_account()
account_sites = await client.get_account()

inverters = await client.get_inverters(account.public_key)
solar = await client.get_solar(account.public_key)
location = await client.get_location(account.public_key)
inverters = await client.get_inverters(account_sites[0].public_key)
solar = await client.get_solar(account_sites[0].public_key)
site = await client.get_site(account_sites[0].public_key)
print(inverters)
print(solar)
print(account)
Expand All @@ -76,6 +76,9 @@ You can read the following data with this package:

### Account

With all the sites you have access to.

- Site ID
- Public Key
- System Name
- Retailer Name
Expand All @@ -96,7 +99,7 @@ You can read the following data with this package:
- Energy Production - Month (kWh)
- Energy Production - Total (kWh)

### Location
### Site

- Public Key
- Name
Expand Down
4 changes: 2 additions & 2 deletions examples/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio

from autarco import Account, Autarco
from autarco import AccountSite, Autarco


async def main() -> None:
Expand All @@ -11,7 +11,7 @@ async def main() -> None:
email="test@autarco.com",
password="password",
) as client:
account: Account = await client.get_account()
account: list[AccountSite] = await client.get_account()
print(account)


Expand Down
48 changes: 28 additions & 20 deletions examples/all_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio

from autarco import Account, Autarco, Inverter, Location, Solar
from autarco import AccountSite, Autarco, Inverter, Site, Solar


async def main() -> None:
Expand All @@ -11,26 +11,34 @@ async def main() -> None:
email="test@autarco.com",
password="password",
) as autarco:
account: Account = await autarco.get_account()
account_sites: list[AccountSite] = await autarco.get_account()

inverters: dict[str, Inverter] = await autarco.get_inverters(account.public_key)
solar: Solar = await autarco.get_solar(account.public_key)
location: Location = await autarco.get_location(account.public_key)
inverters: dict[str, Inverter] = await autarco.get_inverters(
account_sites[0].public_key
)
solar: Solar = await autarco.get_solar(account_sites[0].public_key)
site: Site = await autarco.get_site(account_sites[0].public_key)

print("--- ACCOUNT ---")
print(account)
print(account_sites)
print()
for item in account_sites:
print(f"Site ID: {item.site_id}")
print(f"Public Key: {item.public_key}")
print(f"Name: {item.system_name}")
print(f"Retailer: {item.retailer}")
print(f"Health: {item.health}")
print()
print(f"Public Key: {account.public_key}")
print(f"Name: {account.system_name}")
print(f"Retailer: {account.retailer}")
print(f"Health: {account.health}")

print("--- INVERTER(S) ---")
print(inverters)
print()
for item in inverters.values():
print(f"Serial Number: {item.serial_number}")
print(item)
for inverter in inverters.values():
print(f"Serial Number: {inverter.serial_number}")
print(f"Out AC Power: {inverter.out_ac_power}")
print(f"Out AC Energy Total: {inverter.out_ac_energy_total}")
print(f"Grid Turned Off: {inverter.grid_turned_off}")
print(f"Health: {inverter.health}")
print()

print("--- SOLAR ---")
Expand All @@ -42,14 +50,14 @@ async def main() -> None:
print(f"Energy Production - Total: {solar.energy_production_total}")
print()

print("--- LOCATION ---")
print(location)
print("--- SITE ---")
print(site)
print()
print(f"Address: {location.address}")
print(f"Timezone: {location.timezone}")
print(f"Created At: {location.created_at}")
print(f"Consumption Meter: {location.has_consumption_meter}")
print(f"Has Battery: {location.has_battery}")
print(f"Address: {site.address}")
print(f"Timezone: {site.timezone}")
print(f"Created At: {site.created_at}")
print(f"Consumption Meter: {site.has_consumption_meter}")
print(f"Has Battery: {site.has_battery}")


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions examples/inverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio

from autarco import Account, Autarco
from autarco import AccountSite, Autarco


async def main() -> None:
Expand All @@ -11,8 +11,8 @@ async def main() -> None:
email="test@autarco.com",
password="password",
) as client:
account: Account = await client.get_account()
inverters = await client.get_inverters(account.public_key)
account_sites: list[AccountSite] = await client.get_account()
inverters = await client.get_inverters(account_sites[0].public_key)
print(inverters)


Expand Down
10 changes: 5 additions & 5 deletions examples/location.py → examples/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import asyncio

from autarco import Account, Autarco
from autarco import AccountSite, Autarco


async def main() -> None:
"""Show example on getting Autarco - location data."""
"""Show example on getting Autarco - site data."""
async with Autarco(
email="test@autarco.com",
password="password",
) as client:
account: Account = await client.get_account()
location = await client.get_location(account.public_key)
print(location)
account_sites: list[AccountSite] = await client.get_account()
site = await client.get_site(account_sites[0].public_key)
print(site)


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions examples/solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio

from autarco import Account, Autarco
from autarco import AccountSite, Autarco


async def main() -> None:
Expand All @@ -11,8 +11,8 @@ async def main() -> None:
email="test@autarco.com",
password="password",
) as client:
account: Account = await client.get_account()
solar = await client.get_solar(account.public_key)
account_sites: list[AccountSite] = await client.get_account()
solar = await client.get_solar(account_sites[0].public_key)
print(solar)


Expand Down
6 changes: 3 additions & 3 deletions src/autarco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
AutarcoConnectionError,
AutarcoError,
)
from .models import Account, DateStrategy, Inverter, Location, Solar
from .models import AccountSite, DateStrategy, Inverter, Site, Solar

__all__ = [
"Account",
"AccountSite",
"Autarco",
"AutarcoAuthenticationError",
"AutarcoConnectionError",
"AutarcoError",
"DateStrategy",
"Inverter",
"Location",
"Site",
"Solar",
]
30 changes: 15 additions & 15 deletions src/autarco/autarco.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
AutarcoError,
)
from .models import (
Account,
AccountResponse,
AccountSite,
EnergyResponse,
Inverter,
Location,
PowerResponse,
Site,
Solar,
)

Expand All @@ -37,7 +37,7 @@ class Autarco:
email: str
password: str

request_timeout: float = 10.0
request_timeout: float = 15.0
session: ClientSession | None = None

_close_session: bool = False
Expand Down Expand Up @@ -128,23 +128,23 @@ async def _request(

return text

async def get_account(self) -> Account:
"""Get information about the account.
async def get_account(self) -> list[AccountSite]:
"""Get account with list of sites.

Returns
-------
An Account object. Note: it returns the first account found.
A list of Site objects.

"""
response = await self._request("")
return AccountResponse.from_json(response).data[0]
return AccountResponse.from_json(response).sites

async def get_inverters(self, public_key: str) -> dict[str, Inverter]:
"""Get a list of all used inverters.

Args:
----
public_key: The public key from your account.
public_key: The public key from the specific site.

Returns:
-------
Expand All @@ -155,11 +155,11 @@ async def get_inverters(self, public_key: str) -> dict[str, Inverter]:
return PowerResponse.from_json(response).inverters

async def get_solar(self, public_key: str) -> Solar:
"""Get information about the solar production.
"""Get information about the solar production from a site.

Args:
----
public_key: The public key from your account.
public_key: The public key from your site.

Returns:
-------
Expand All @@ -174,20 +174,20 @@ async def get_solar(self, public_key: str) -> Solar:
combined = {**power_class.stats["kpis"], **energy_class.stats["kpis"]}
return Solar.from_dict(combined)

async def get_location(self, public_key: str) -> Location:
"""Get information about your system location.
async def get_site(self, public_key: str) -> Site:
"""Get information about your system site.

Args:
----
public_key: The public key from your account.
public_key: The public key from your site.

Returns:
-------
An Location object.
An Site object.

"""
response = await self._request(f"{public_key}/")
return Location.from_json(response)
return Site.from_json(response)

async def close(self) -> None:
"""Close open client session."""
Expand Down
29 changes: 15 additions & 14 deletions src/autarco/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@ class EnergyResponse(DataClassORJSONMixin):

@dataclass
class AccountResponse(DataClassORJSONMixin):
"""Object representing an Account model response from the API."""
"""Object representing an Account Response model from the API."""

data: list[Account]
sites: list[AccountSite] = field(metadata=field_options(alias="data"))


@dataclass
class AccountSite(DataClassORJSONMixin):
"""Object representing an Account Site model response from the API."""

site_id: int
public_key: str
system_name: str = field(metadata=field_options(alias="name"))
retailer: str = field(metadata=field_options(alias="name_retailer"))
health: str


@dataclass
Expand All @@ -68,18 +79,8 @@ class Solar(DataClassDictMixin):


@dataclass
class Account(DataClassORJSONMixin):
"""Object representing a Account model response from the API."""

public_key: str
system_name: str = field(metadata=field_options(alias="name"))
retailer: str = field(metadata=field_options(alias="name_retailer"))
health: str


@dataclass
class Location(DataClassORJSONMixin):
"""Object representing an Location model response from the API."""
class Site(DataClassORJSONMixin):
"""Object representing an Site model response from the API."""

# pylint: disable-next=too-few-public-methods
class Config(BaseConfig):
Expand Down
8 changes: 5 additions & 3 deletions tests/__snapshots__/test_models.ambr
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# serializer version: 1
# name: test_get_account
Account(public_key='blabla', system_name='Mijn Autarco', retailer='Klaas Schoute', health='OK')
list([
AccountSite(site_id=1000, public_key='blabla', system_name='Mijn Autarco', retailer='Klaas Schoute', health='OK'),
])
# ---
# name: test_get_inverters
dict({
'123456789876': Inverter(serial_number='123456789876', out_ac_power=100, out_ac_energy_total=6605, grid_turned_off=False, health='OK'),
'987654321234': Inverter(serial_number='987654321234', out_ac_power=100, out_ac_energy_total=3607, grid_turned_off=False, health='OK'),
})
# ---
# name: test_get_location
Location(public_key='blabla', name='My Autarco solar installation', address=Address(street='Streetname 00', zip_code='1111 AA', city='Valkenswaard', country='Nederland'), timezone='Europe/Amsterdam', created_at=datetime.date(2023, 5, 15), has_consumption_meter=False, has_battery=False)
# name: test_get_site
Site(public_key='blabla', name='My Autarco solar installation', address=Address(street='Streetname 00', zip_code='1111 AA', city='Valkenswaard', country='Nederland'), timezone='Europe/Amsterdam', created_at=datetime.date(2023, 5, 15), has_consumption_meter=False, has_battery=False)
# ---
# name: test_get_solar
Solar(power_production=200, energy_production_today=4, energy_production_month=58, energy_production_total=10379)
Expand Down
File renamed without changes.
Loading