emerald-hws 0.0.4__tar.gz → 0.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: emerald_hws
3
- Version: 0.0.4
3
+ Version: 0.0.5
4
4
  Summary: A package to manipulate and monitor Emerald Heat Pump Hot Water Systems
5
5
  Author-email: Ross Williamson <ross@inertia.net.nz>
6
6
  Project-URL: Homepage, https://github.com/ross-w/emerald_hws_py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "emerald_hws"
7
- version = "0.0.4"
7
+ version = "0.0.5"
8
8
  dependencies = [
9
9
  "boto3",
10
10
  "AWSIoTPythonSDK"
@@ -1,7 +1,7 @@
1
1
  import json
2
2
  import requests
3
3
  import os
4
- # import logging
4
+ import logging
5
5
  import boto3
6
6
  import random
7
7
  from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
@@ -30,6 +30,8 @@ class EmeraldHWS():
30
30
  self.password = password
31
31
  self.token = ""
32
32
  self.properties = {}
33
+ self.logger = logging.getLogger()
34
+ self.logger.setLevel(logging.DEBUG)
33
35
 
34
36
  def getLoginToken(self):
35
37
  """ Performs an API request to get a token from the API
@@ -71,31 +73,42 @@ class EmeraldHWS():
71
73
  post_response_json = post_response.json()
72
74
 
73
75
  if post_response_json.get("code") == 200:
76
+ self.logger.debug("Successfully logged into Emerald API")
74
77
  self.properties = post_response_json.get("info").get("property")
75
78
  else:
76
79
  raise Exception("Unable to fetch properties from Emerald API")
77
80
 
78
- def connectMQTT(self):
79
- """ Establishes a connection to Amazon IOT core's MQTT service
81
+ def getTemporaryCreds(self):
82
+ """ Returns temporary credentials for IoT Core
80
83
  """
81
84
 
82
- cert_path = os.path.join(os.path.dirname(__file__), '__assets__', 'SFSRootCAG2.pem')
83
-
84
- # Cognito auth
85
85
  identityPoolID = self.COGNITO_IDENTITY_POOL_ID
86
86
  region = self.MQTT_HOST.split('.')[2]
87
87
  cognitoIdentityClient = boto3.client('cognito-identity', region_name=region)
88
88
 
89
89
  temporaryIdentityId = cognitoIdentityClient.get_id(IdentityPoolId=identityPoolID)
90
90
  identityID = temporaryIdentityId["IdentityId"]
91
+ self.logger.debug("AWS IoT IdentityID: {}".format(identityID))
91
92
 
92
93
  temporaryCredentials = cognitoIdentityClient.get_credentials_for_identity(IdentityId=identityID)
93
- AccessKeyId = temporaryCredentials["Credentials"]["AccessKeyId"]
94
- SecretKey = temporaryCredentials["Credentials"]["SecretKey"]
95
- SessionToken = temporaryCredentials["Credentials"]["SessionToken"]
94
+ self.logger.debug("Got new temporary credentials for AWS")
95
+ self.identityID = identityID
96
+ self.temporaryCredentials = temporaryCredentials
97
+
98
+
99
+ def connectMQTT(self):
100
+ """ Establishes a connection to Amazon IOT core's MQTT service
101
+ """
102
+
103
+ cert_path = os.path.join(os.path.dirname(__file__), '__assets__', 'SFSRootCAG2.pem')
104
+ self.getTemporaryCreds()
105
+
106
+ AccessKeyId = self.temporaryCredentials["Credentials"]["AccessKeyId"]
107
+ SecretKey = self.temporaryCredentials["Credentials"]["SecretKey"]
108
+ SessionToken = self.temporaryCredentials["Credentials"]["SessionToken"]
96
109
 
97
110
  # Init AWSIoTMQTTClient
98
- myAWSIoTMQTTClient = AWSIoTMQTTClient(identityID, useWebsocket=True)
111
+ myAWSIoTMQTTClient = AWSIoTMQTTClient(self.identityID, useWebsocket=True)
99
112
 
100
113
  # AWSIoTMQTTClient configuration
101
114
  myAWSIoTMQTTClient.configureEndpoint(self.MQTT_HOST, 443)
@@ -106,7 +119,8 @@ class EmeraldHWS():
106
119
  myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz
107
120
  myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
108
121
  myAWSIoTMQTTClient.configureMQTTOperationTimeout(10) # 10 sec
109
-
122
+ myAWSIoTMQTTClient.onOffline = self.on_offline
123
+ myAWSIoTMQTTClient.onOnline = self.on_online
110
124
  # Connect and subscribe to AWS IoT
111
125
  myAWSIoTMQTTClient.connect()
112
126
 
@@ -127,14 +141,28 @@ class EmeraldHWS():
127
141
  self.updateHWSState(hws_id, key, json_payload[1][key])
128
142
 
129
143
  def mqttCallback(self, client, userdata, message):
130
- # print("Received a new message: ")
131
- # print(message.payload.decode("utf-8"))
132
- # print("from topic: ")
133
- # print(message.topic)
134
- # print("--------------\n\n")
144
+ """ Calls decode update for received message
145
+ """
135
146
 
147
+ self.logger.debug("Received message from MQTT topic {}: {}".format(message.topic,message.payload.decode("utf-8")))
136
148
  self.mqttDecodeUpdate(message.topic, message.payload)
137
149
 
150
+ def on_offline(self):
151
+ """ Reconfigures temporary credentials
152
+ """
153
+
154
+ self.logger.debug("AWS IoT offline")
155
+ self.getTemporaryCreds()
156
+ AccessKeyId = self.temporaryCredentials["Credentials"]["AccessKeyId"]
157
+ SecretKey = self.temporaryCredentials["Credentials"]["SecretKey"]
158
+ SessionToken = self.temporaryCredentials["Credentials"]["SessionToken"]
159
+ self.myAWSIoTMQTTClient.configureIAMCredentials(AccessKeyId, SecretKey, SessionToken)
160
+
161
+ def on_online(self):
162
+ """ Logs online state
163
+ """
164
+ self.logger.debug("AWS IoT online")
165
+
138
166
  def updateHWSState(self, id, key, value):
139
167
  """ Updates the specified value for the supplied key in the HWS id specified
140
168
  :param id: ID of the HWS
@@ -201,30 +229,35 @@ class EmeraldHWS():
201
229
  """ Turns the specified HWS on
202
230
  :param id: The UUID of the HWS to turn on
203
231
  """
232
+ self.logger.debug("Sending control message: turn on")
204
233
  self.sendControlMessage(id, {"switch":1})
205
234
 
206
235
  def turnOff(self, id):
207
236
  """ Turns the specified HWS off
208
237
  :param id: The UUID of the HWS to turn off
209
238
  """
239
+ self.logger.debug("Sending control message: turn off")
210
240
  self.sendControlMessage(id, {"switch":0})
211
241
 
212
242
  def setNormalMode(self, id):
213
243
  """ Sets the specified HWS to normal (not Boost or Quiet) mode
214
244
  :param id: The UUID of the HWS to set to normal mode
215
245
  """
246
+ self.logger.debug("Sending control message: normal mode")
216
247
  self.sendControlMessage(id, {"mode":1})
217
248
 
218
249
  def setBoostMode(self, id):
219
250
  """ Sets the specified HWS to boost (high power) mode
220
251
  :param id: The UUID of the HWS to set to boost mode
221
252
  """
253
+ self.logger.debug("Sending control message: boost mode")
222
254
  self.sendControlMessage(id, {"mode":0})
223
255
 
224
256
  def setQuietMode(self, id):
225
257
  """ Sets the specified HWS to quiet (low power) mode
226
258
  :param id: The UUID of the HWS to set to quiet mode
227
259
  """
260
+ self.logger.debug("Sending control message: quiet mode")
228
261
  self.sendControlMessage(id, {"mode":2})
229
262
 
230
263
  def isOn(self, id):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: emerald_hws
3
- Version: 0.0.4
3
+ Version: 0.0.5
4
4
  Summary: A package to manipulate and monitor Emerald Heat Pump Hot Water Systems
5
5
  Author-email: Ross Williamson <ross@inertia.net.nz>
6
6
  Project-URL: Homepage, https://github.com/ross-w/emerald_hws_py
File without changes
File without changes
File without changes