Skip to content

Commit

Permalink
update example to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
jrester committed Jan 15, 2024
1 parent ee4a4a2 commit f937a99
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ However, if you believe there exists a solution, feel free to open an issue deta

## Usage

For a basic Overview of the functionality of this library you can take a look at `examples/example.py`:
For a basic Overview of the functionality of this library you can take a look at `examples/example.py`. You can run the example, by cloning the repo and executing in your shell:

```bash
$ export POWERWALL_IP=<ip of your Powerwall>
$ export POWERWALL_PASSWORD=<your password>
$ python3 examples/example.py
$ tox -e example
```

### Setup
Expand Down
72 changes: 39 additions & 33 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import os

from tesla_powerwall import MeterResponse, Powerwall
Expand All @@ -24,45 +25,50 @@ def print_meter_row(meter_data: MeterResponse):
)


ip = getenv("POWERWALL_IP")
password = getenv("POWERWALL_PASSWORD")
async def main():
ip = getenv("POWERWALL_IP")
password = getenv("POWERWALL_PASSWORD")

power_wall = Powerwall(ip)
power_wall.login(password)
site_name = power_wall.get_site_info().site_name
meters_agg = power_wall.get_meters()
power_wall = Powerwall(ip)
await power_wall.login(password)
site_name = (await power_wall.get_site_info()).site_name
meters_agg = await power_wall.get_meters()

print(f"{site_name}:\n")
print(f"{site_name}:\n")

row_format = "{:>18}: {}"
row_format = "{:>18}: {}"

values = [
("Charge (%)", round(power_wall.get_charge())),
("Capacity", power_wall.get_capacity()),
("Nominal Energy", power_wall.get_energy()),
("Grid Status", power_wall.get_grid_status().value),
("Backup Reserve (%)", round(power_wall.get_backup_reserve_percentage())),
("Device Type", power_wall.get_device_type().value),
("Software Version", power_wall.get_version()),
]
values = [
("Charge (%)", round(await power_wall.get_charge())),
("Capacity", await power_wall.get_capacity()),
("Nominal Energy", await power_wall.get_energy()),
("Grid Status", (await power_wall.get_grid_status()).value),
("Backup Reserve (%)", round(await power_wall.get_backup_reserve_percentage())),
("Device Type", (await power_wall.get_device_type()).value),
("Software Version", await power_wall.get_version()),
]

for val in values:
print(row_format.format(*val))

for val in values:
print(row_format.format(*val))
print("\n")

print("\n")

print(
"{:>8} {:>8} {:>17} {:>17} {:>8} {:>17} {:>17}".format(
"Meter",
"Power",
"Energy exported",
"Energy imported",
"Active",
"Drawing from",
"Sending to",
print(
"{:>8} {:>8} {:>17} {:>17} {:>8} {:>17} {:>17}".format(
"Meter",
"Power",
"Energy exported",
"Energy imported",
"Active",
"Drawing from",
"Sending to",
)
)
)

for meter in meters_agg.meters.values():
print_meter_row(meter)
for meter in meters_agg.meters.values():
print_meter_row(meter)

await power_wall.close()


asyncio.run(main())
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ commands = python -m unittest discover tests/unit
[testenv:integration]
passenv = POWERWALL_IP,POWERWALL_PASSWORD
commands = python -m unittest discover tests/integration

[testenv:example]
passenv = POWERWALL_IP,POWERWALL_PASSWORD
commands = python examples/example.py

0 comments on commit f937a99

Please sign in to comment.