emerald-hws 0.0.4__py3-none-any.whl → 0.0.6__py3-none-any.whl

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.
emerald_hws/emeraldhws.py CHANGED
@@ -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,7 @@ class EmeraldHWS():
30
30
  self.password = password
31
31
  self.token = ""
32
32
  self.properties = {}
33
+ self.logger = logging.getLogger()
33
34
 
34
35
  def getLoginToken(self):
35
36
  """ Performs an API request to get a token from the API
@@ -71,31 +72,42 @@ class EmeraldHWS():
71
72
  post_response_json = post_response.json()
72
73
 
73
74
  if post_response_json.get("code") == 200:
75
+ self.logger.debug("Successfully logged into Emerald API")
74
76
  self.properties = post_response_json.get("info").get("property")
75
77
  else:
76
78
  raise Exception("Unable to fetch properties from Emerald API")
77
79
 
78
- def connectMQTT(self):
79
- """ Establishes a connection to Amazon IOT core's MQTT service
80
+ def getTemporaryCreds(self):
81
+ """ Returns temporary credentials for IoT Core
80
82
  """
81
83
 
82
- cert_path = os.path.join(os.path.dirname(__file__), '__assets__', 'SFSRootCAG2.pem')
83
-
84
- # Cognito auth
85
84
  identityPoolID = self.COGNITO_IDENTITY_POOL_ID
86
85
  region = self.MQTT_HOST.split('.')[2]
87
86
  cognitoIdentityClient = boto3.client('cognito-identity', region_name=region)
88
87
 
89
88
  temporaryIdentityId = cognitoIdentityClient.get_id(IdentityPoolId=identityPoolID)
90
89
  identityID = temporaryIdentityId["IdentityId"]
90
+ self.logger.debug("AWS IoT IdentityID: {}".format(identityID))
91
91
 
92
92
  temporaryCredentials = cognitoIdentityClient.get_credentials_for_identity(IdentityId=identityID)
93
- AccessKeyId = temporaryCredentials["Credentials"]["AccessKeyId"]
94
- SecretKey = temporaryCredentials["Credentials"]["SecretKey"]
95
- SessionToken = temporaryCredentials["Credentials"]["SessionToken"]
93
+ self.logger.debug("Got new temporary credentials for AWS")
94
+ self.identityID = identityID
95
+ self.temporaryCredentials = temporaryCredentials
96
+
97
+
98
+ def connectMQTT(self):
99
+ """ Establishes a connection to Amazon IOT core's MQTT service
100
+ """
101
+
102
+ cert_path = os.path.join(os.path.dirname(__file__), '__assets__', 'SFSRootCAG2.pem')
103
+ self.getTemporaryCreds()
104
+
105
+ AccessKeyId = self.temporaryCredentials["Credentials"]["AccessKeyId"]
106
+ SecretKey = self.temporaryCredentials["Credentials"]["SecretKey"]
107
+ SessionToken = self.temporaryCredentials["Credentials"]["SessionToken"]
96
108
 
97
109
  # Init AWSIoTMQTTClient
98
- myAWSIoTMQTTClient = AWSIoTMQTTClient(identityID, useWebsocket=True)
110
+ myAWSIoTMQTTClient = AWSIoTMQTTClient(self.identityID, useWebsocket=True)
99
111
 
100
112
  # AWSIoTMQTTClient configuration
101
113
  myAWSIoTMQTTClient.configureEndpoint(self.MQTT_HOST, 443)
@@ -106,9 +118,10 @@ class EmeraldHWS():
106
118
  myAWSIoTMQTTClient.configureDrainingFrequency(2) # Draining: 2 Hz
107
119
  myAWSIoTMQTTClient.configureConnectDisconnectTimeout(10) # 10 sec
108
120
  myAWSIoTMQTTClient.configureMQTTOperationTimeout(10) # 10 sec
109
-
121
+ myAWSIoTMQTTClient.onOffline = self.on_offline
122
+ myAWSIoTMQTTClient.onOnline = self.on_online
110
123
  # Connect and subscribe to AWS IoT
111
- myAWSIoTMQTTClient.connect()
124
+ myAWSIoTMQTTClient.connect(keepAliveIntervalSecond=60)
112
125
 
113
126
  self.myAWSIoTMQTTClient = myAWSIoTMQTTClient
114
127
 
@@ -127,14 +140,28 @@ class EmeraldHWS():
127
140
  self.updateHWSState(hws_id, key, json_payload[1][key])
128
141
 
129
142
  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")
143
+ """ Calls decode update for received message
144
+ """
135
145
 
146
+ self.logger.debug("Received message from MQTT topic {}: {}".format(message.topic,message.payload.decode("utf-8")))
136
147
  self.mqttDecodeUpdate(message.topic, message.payload)
137
148
 
149
+ def on_offline(self):
150
+ """ Reconfigures temporary credentials
151
+ """
152
+
153
+ self.logger.debug("AWS IoT offline")
154
+ self.getTemporaryCreds()
155
+ AccessKeyId = self.temporaryCredentials["Credentials"]["AccessKeyId"]
156
+ SecretKey = self.temporaryCredentials["Credentials"]["SecretKey"]
157
+ SessionToken = self.temporaryCredentials["Credentials"]["SessionToken"]
158
+ self.myAWSIoTMQTTClient.configureIAMCredentials(AccessKeyId, SecretKey, SessionToken)
159
+
160
+ def on_online(self):
161
+ """ Logs online state
162
+ """
163
+ self.logger.debug("AWS IoT online")
164
+
138
165
  def updateHWSState(self, id, key, value):
139
166
  """ Updates the specified value for the supplied key in the HWS id specified
140
167
  :param id: ID of the HWS
@@ -201,30 +228,35 @@ class EmeraldHWS():
201
228
  """ Turns the specified HWS on
202
229
  :param id: The UUID of the HWS to turn on
203
230
  """
231
+ self.logger.debug("Sending control message: turn on")
204
232
  self.sendControlMessage(id, {"switch":1})
205
233
 
206
234
  def turnOff(self, id):
207
235
  """ Turns the specified HWS off
208
236
  :param id: The UUID of the HWS to turn off
209
237
  """
238
+ self.logger.debug("Sending control message: turn off")
210
239
  self.sendControlMessage(id, {"switch":0})
211
240
 
212
241
  def setNormalMode(self, id):
213
242
  """ Sets the specified HWS to normal (not Boost or Quiet) mode
214
243
  :param id: The UUID of the HWS to set to normal mode
215
244
  """
245
+ self.logger.debug("Sending control message: normal mode")
216
246
  self.sendControlMessage(id, {"mode":1})
217
247
 
218
248
  def setBoostMode(self, id):
219
249
  """ Sets the specified HWS to boost (high power) mode
220
250
  :param id: The UUID of the HWS to set to boost mode
221
251
  """
252
+ self.logger.debug("Sending control message: boost mode")
222
253
  self.sendControlMessage(id, {"mode":0})
223
254
 
224
255
  def setQuietMode(self, id):
225
256
  """ Sets the specified HWS to quiet (low power) mode
226
257
  :param id: The UUID of the HWS to set to quiet mode
227
258
  """
259
+ self.logger.debug("Sending control message: quiet mode")
228
260
  self.sendControlMessage(id, {"mode":2})
229
261
 
230
262
  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.6
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
@@ -0,0 +1,8 @@
1
+ emerald_hws/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
2
+ emerald_hws/emeraldhws.py,sha256=w7iSSo9hcsk8CoQsYD89Ar-PWyzVkjnpbxd8YEXrujc,12051
3
+ emerald_hws/__assets__/SFSRootCAG2.pem,sha256=hw9W0AnYrrlbcWsOewAgIl1ULEsoO57Ylu35dCjWcS4,1424
4
+ emerald_hws-0.0.6.dist-info/LICENSE,sha256=zzMi56JX7OO-epbXNfe_oFW6sfZHSlO7Yxm0Oh0V014,1072
5
+ emerald_hws-0.0.6.dist-info/METADATA,sha256=jHPA5zbpaLofBPi47Pkx0U7Jrpf9xTurvIAMuC-bh10,694
6
+ emerald_hws-0.0.6.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
7
+ emerald_hws-0.0.6.dist-info/top_level.txt,sha256=ZCiUmnBkDr2n4QVkTet1s_AKiGJjuz3heuCR5w5ZqLY,12
8
+ emerald_hws-0.0.6.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.42.0)
2
+ Generator: bdist_wheel (0.43.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,8 +0,0 @@
1
- emerald_hws/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
2
- emerald_hws/emeraldhws.py,sha256=z6TETw5ZyFA7t-deiOnOIRgqKNgK_0ZRXSbDSD0DP8A,10525
3
- emerald_hws/__assets__/SFSRootCAG2.pem,sha256=hw9W0AnYrrlbcWsOewAgIl1ULEsoO57Ylu35dCjWcS4,1424
4
- emerald_hws-0.0.4.dist-info/LICENSE,sha256=zzMi56JX7OO-epbXNfe_oFW6sfZHSlO7Yxm0Oh0V014,1072
5
- emerald_hws-0.0.4.dist-info/METADATA,sha256=ZCvber0QPgtZpQKlUF2ET7eAMdz7ooPfyvgQ8NIHpEU,694
6
- emerald_hws-0.0.4.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
7
- emerald_hws-0.0.4.dist-info/top_level.txt,sha256=ZCiUmnBkDr2n4QVkTet1s_AKiGJjuz3heuCR5w5ZqLY,12
8
- emerald_hws-0.0.4.dist-info/RECORD,,