Browse Source

Initial commit, getting devices & 12h of usage

Jason Tarka 11 tháng trước cách đây
commit
5c315fddfd
4 tập tin đã thay đổi với 121 bổ sung0 xóa
  1. 2 0
      .gitignore
  2. 21 0
      README.md
  3. 79 0
      main.py
  4. 19 0
      requirements.txt

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+keys.json
+env/

+ 21 - 0
README.md

@@ -0,0 +1,21 @@
+# Emporia Vue Energy Monitor Readings
+
+## Setting Up
+
+```sh
+# One of these:
+pip install virtualenv
+sudo apt install python3-venv
+
+# Then setup the virtual environment:
+python3 -m venv env
+
+# Activate the environment:
+source env/bin/activate
+
+# Install required packages
+pip install -r requirements.txt
+
+# Update set of required packages
+pip freeze > requirements.txt
+```

+ 79 - 0
main.py

@@ -0,0 +1,79 @@
+import pyemvue
+import json
+import datetime
+from pyemvue.enums import Scale, Unit
+
+def printRecursive(usageDict, info, depth=0):
+	for gid, device in usageDict.items():
+		for channelNum, channel in device.channels.items():
+			name = channel.name
+			if name == 'Main':
+				name = info[gid].device_name
+			print('-'*depth, f'{gid}\t{channelNum}\t{name}\t{channel.usage}\tkWh')
+			if channel.nested_devices:
+				printRecursive(channel.nested_devices, info, depth+1)
+
+def usageOverTime(vue, device):
+	now = datetime.datetime.now(datetime.timezone.utc)
+	search_end = now
+	# search_end = now - datetime.timedelta(hours=12)
+	search_start = search_end - datetime.timedelta(hours=12)
+
+	usage_over_time, start_time = vue.get_chart_usage(
+			device.channels[0],
+			search_start,
+			search_end,
+			scale=Scale.MINUTE.value,
+			unit=Unit.KWH.value
+		)
+	print('Usage for the last 7 days, starting', start_time.isoformat())
+	currTime = start_time
+	for usage in usage_over_time:
+		print(currTime, usage*1000, 'Wh')
+		currTime += datetime.timedelta(minutes=1)
+
+if __name__ == '__main__':
+	with open('keys.json') as f:
+		keys = json.load(f)
+	vue = pyemvue.PyEmVue()
+
+	if 'id_token' in keys:
+		# Login using access tokens.
+		vue.login(
+				id_token=keys['id_token'],
+				access_token=keys['access_token'],
+				refresh_token=keys['refresh_token'],
+				token_storage_file='keys.json'
+			)
+	else:
+		# Login with username & password, and update keys.json.
+		vue.login(
+				username=keys['username'],
+				password=keys['password'],
+				token_storage_file='keys.json'
+			)
+	
+	devices = vue.get_devices()
+
+	# Show current usage for all devices.
+	deviceGIDs = []
+	deviceInfo = {}
+	for device in devices:
+		gid = device.device_gid
+		if not gid in deviceGIDs:
+			deviceGIDs.append(gid)
+			deviceInfo[gid] = device
+		else:
+			deviceInfo[gid].channels += device.channels
+	
+	usageDict = vue.get_device_list_usage(
+			deviceGids=deviceGIDs,
+			instant=None,
+			scale=Scale.MINUTE.value,
+			unit=Unit.KWH.value
+		)
+	print(f'device_gid\tchannel_num\tname\tusage\tunit')
+	printRecursive(usageDict, deviceInfo)
+
+	# Show previous usage for the first device.
+	usageOverTime(vue, devices[0])

+ 19 - 0
requirements.txt

@@ -0,0 +1,19 @@
+boto3==1.35.95
+botocore==1.35.95
+certifi==2024.12.14
+cffi==1.17.1
+charset-normalizer==3.4.1
+cryptography==44.0.0
+envs==1.4
+idna==3.10
+jmespath==1.0.1
+pycognito==2024.5.1
+pycparser==2.22
+pyemvue==0.18.6
+PyJWT==2.10.1
+python-dateutil==2.9.0.post0
+requests==2.32.3
+s3transfer==0.10.4
+six==1.17.0
+typing_extensions==4.12.2
+urllib3==2.3.0