diff --git a/examples/user_consumption.py b/examples/user_consumption.py new file mode 100644 index 0000000..ff256df --- /dev/null +++ b/examples/user_consumption.py @@ -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]} ") + 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()) diff --git a/pyeasee/site.py b/pyeasee/site.py index 7da71ad..5b81792 100644 --- a/pyeasee/site.py +++ b/pyeasee/site.py @@ -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