-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexample_entities.py
More file actions
101 lines (91 loc) · 4.16 KB
/
example_entities.py
File metadata and controls
101 lines (91 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Example application.
Demonstrates the use of entity classes as a higher level interface to the
Soliscloud API. Fill in your own API key and secret in config.json before
running.
By using the entity classes you can more easily access the data
attributes of the plant, inverters and collectors.
There are 2 ways to create the entity classes:
1. Use the class method 'from_data' to create entity instances
from data retrieved from the API. It will create one or more entity
instances from the data passed in. This method will not retrieve any data
from the API, so you need to do that yourself first. It will not
recursively create all entities, i.e. plants, inverters and collectors.
You need to call it separately for each entity class.
2. Use the class method 'from_session' to create entity instances
by passing in the aiohttp session and API credentials. This method will
retrieve the data from the API and create the entity instances. It
will recursively create all entities, i.e. plants, inverters
and collectors when called on the Plant class and inverters and collectors
when called on the Inverter class.
"""
from __future__ import annotations
import asyncio
import logging
import json
from aiohttp import ClientSession
from soliscloud_api import SoliscloudError, SoliscloudHttpError, \
SoliscloudTimeoutError, SoliscloudApiError
from soliscloud_api import Plant, Inverter, Collector # noqa: F401
from soliscloud_api import SoliscloudAPI as api
logging.basicConfig(level=logging.DEBUG)
async def main():
"""Run main function."""
# Put your own key and secret in the config.json file
with open('config.json', 'r') as file:
data = json.load(file)
api_key = data['key']
api_secret = bytearray(data['secret'], 'utf-8')
# Australian accounts require nmi, uncomment if required.
# (NOT TESTED!)
# api_nmi = data['nmi']
async with ClientSession() as websession:
try:
the_api = api(api.DEFAULT_DOMAIN, websession)
plants = await Plant.from_session(
websession, api_key, api_secret)
# Whole entity tree is now created, print it out
print("*** from_session ***")
print(plants[0])
# Alternatively, get started by getting data from API
# and then create entity instances from that data.
plant_data = await the_api.station_detail_list(
api_key, api_secret, page_no=1, page_size=100)
plants = Plant.from_data(plant_data)
# use plant_id of your own plant here to get inverters
# for that plant.
# use inverter_id to only get one specific inverter
inverter_data = await the_api.inverter_list(
api_key, api_secret, page_no=1, page_size=100)
inverters = Inverter.from_data(
inverter_data, plant_id=plants[0].plant_id)
plants[0].add_inverters(inverters)
print("\n*** from_data ***")
print(plants[0])
p = plants[0]
# Now access some attributes of the entities
print("\n*** Accessing attributes ***")
print(f"Plant id: {p.plant_id}")
print(f"Plant name: {p.data['station_name']}")
print(f"Number of inverters: {len(p.inverters)}")
for inv in inverters:
print(f"Inverter id: {inv.inverter_id}")
# The attributes if the inverter are in inv.data
# If an attribute has a unit then the value is of
# dimensioned type
print(f"Inverter state: {inv.data['state']}")
# Print full energy type attribute
print(f"Total energy: {inv.data['etotal']}")
# print value and unit separately
print(f"Total energy value: {inv.data['etotal'].value}")
print(f"Total energy unit: {inv.data['etotal'].unit}")
except (
SoliscloudError,
SoliscloudHttpError,
SoliscloudTimeoutError,
SoliscloudApiError,
) as error:
print(f"Error: {error}")
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
loop.close()