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

Add functions to retreive users and consumption per user #116

Merged
merged 1 commit into from
Nov 14, 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
30 changes: 30 additions & 0 deletions examples/user_consumption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# List consumption per user for all sites assosiated with an account
import asyncio
import sys

from pyeasee import Easee


async def async_main():

if len(sys.argv) < 3:
print(f"Usage: {sys.argv[0]} <username> <password>")
return

print(f"Logging in using: {sys.argv[1]} {sys.argv[2]}")
easee = Easee(sys.argv[1], sys.argv[2])

sites = await easee.get_sites()
for site in sites:
print(f"Site {site.name} ({site.id})")
users = await site.get_users()
for user in users["siteUsers"]:
print(f" User {user['userId']}: {user['name']}, {user['email']}")
consumptions = await site.get_user_monthly_consumption(user["userId"])
for consumption in consumptions:
print(f" {consumption['year']}-{consumption['month']}: {consumption['totalEnergyUsage']} kWh")

await easee.close()


asyncio.run(async_main())
27 changes: 27 additions & 0 deletions pyeasee/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,30 @@ async def get_cost_between_dates(self, from_date: datetime, to_date: datetime):
return costs
except (ServerFailureException, ForbiddenServiceException):
return None

async def get_users(self):
"""Get a list of users connected to this site"""

try:
users = await (await self.easee.get(f"/api/sites/{self.id}/users")).json()
return users
except (ServerFailureException, ForbiddenServiceException):
return None

async def get_user_monthly_consumption(self, user_id):
"""Get user consumption"""

try:
consumption = await (await self.easee.get(f"/api/sites/{self.id}/users/{user_id}/monthly")).json()
return consumption
except (ServerFailureException, ForbiddenServiceException):
return None

async def get_user_yearly_consumption(self, user_id):
"""Get user consumption"""

try:
consumption = await (await self.easee.get(f"/api/sites/{self.id}/users/{user_id}/yearly")).json()
return consumption
except (ServerFailureException, ForbiddenServiceException):
return None
Loading