Skip to content

Commit 29222ad

Browse files
author
Khole Jones
committed
Libary Version 0.3.4.3
1 parent fb2363e commit 29222ad

9 files changed

Lines changed: 87 additions & 201 deletions

File tree

pyhiveapi/api/hive_async_api.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class HiveAsync:
2222

2323
def __init__(self, websession: Optional[ClientSession] = None):
2424
"""Hive API initialisation."""
25-
self.log = Logger()
25+
self.logger = Logger()
2626
self.baseUrl = "https://beekeeper.hivehome.com/1.0"
2727
self.urls = {
2828
"properties": "https://sso.hivehome.com/",
@@ -53,7 +53,7 @@ def __init__(self, websession: Optional[ClientSession] = None):
5353
async def request(self, method: str, url: str, **kwargs) -> ClientResponse:
5454
"""Make a request."""
5555
data = kwargs.get("data", None)
56-
await self.log.log(
56+
await self.logger.log(
5757
"No_ID",
5858
"API",
5959
"Request is - {0}:{1} Body is {2}",
@@ -66,7 +66,7 @@ async def request(self, method: str, url: str, **kwargs) -> ClientResponse:
6666
if "sso" in url:
6767
pass
6868
else:
69-
self.log.log("No_ID", "API", "ERROR - NO API TOKEN")
69+
self.logger.log("No_ID", "API", "ERROR - NO API TOKEN")
7070
raise NoApiToken
7171

7272
async with self.websession.request(
@@ -79,16 +79,16 @@ async def request(self, method: str, url: str, **kwargs) -> ClientResponse:
7979
)
8080

8181
if operator.contains(str(resp.status), "20"):
82-
await self.log.log(
82+
await self.logger.log(
8383
"API", "API", "Response is - {0}", info=[str(resp.status)]
8484
)
8585
elif resp.status == HTTP_UNAUTHORIZED:
86-
await self.log.error_check(
87-
"No_ID", "ERROR", "Failed_API", resp=resp["original"]
86+
await self.logger.LOGGER.error(
87+
f"Hive token as expired when calling {url} - "
88+
f"HTTP status is - {resp.status}"
8889
)
89-
9090
else:
91-
self.log.LOGGER.error(
91+
await self.logger.LOGGER.error(
9292
f"Something has gone wrong calling {url} - "
9393
f"HTTP status is - {resp.status}"
9494
)
@@ -265,7 +265,7 @@ async def set_action(self, n_id, data):
265265

266266
async def error(self):
267267
"""An error has occured iteracting wth the Hive API."""
268-
await self.log.log("API_ERROR", "ERROR", "Error attempting API call")
268+
await self.logger.log("API_ERROR", "ERROR", "Error attempting API call")
269269
raise web_exceptions.HTTPError
270270

271271
async def is_file_being_used(self):

pyhiveapi/device_attributes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Attributes:
88
"""Device Attributes Code."""
99

1010
def __init__(self):
11-
self.log = Logger()
11+
self.logger = Logger()
1212
self.type = "Attribute"
1313

1414
async def state_attributes(self, n_id, _type):
@@ -33,7 +33,7 @@ async def online_offline(self, n_id):
3333
data = Data.devices[n_id]
3434
state = data["props"]["online"]
3535
except KeyError as e:
36-
Helper.error(e)
36+
await self.logger.error(e)
3737

3838
return state
3939

@@ -47,7 +47,7 @@ async def get_mode(self, n_id):
4747
state = data["state"]["mode"]
4848
final = Data.HIVETOHA[self.type].get(state, state)
4949
except KeyError as e:
50-
Helper.error(e)
50+
await self.logger.error(e)
5151

5252
return final
5353

@@ -60,8 +60,8 @@ async def battery(self, n_id):
6060
data = Data.devices[n_id]
6161
state = data["props"]["battery"]
6262
final = state
63-
await self.log.error_check(n_id, self.type, state)
63+
await self.logger.error_check(n_id, self.type, state)
6464
except KeyError as e:
65-
Helper.error(e)
65+
await self.logger.error(e)
6666

6767
return final

pyhiveapi/helper/hive_helper.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,31 @@ def getDeviceFromID(self, n_id):
4545

4646
return data
4747

48+
def getDeviceData(self, product):
49+
""""Get device Data."""
50+
device = product
51+
type = product['type']
52+
if type in ('heating', 'hotwater'):
53+
for aDevice in Data.devices:
54+
if (Data.devices[aDevice]["type"]
55+
in Data.HIVE_TYPES["Thermo"]
56+
):
57+
try:
58+
if product["props"]["zone"] == Data.devices[aDevice]['props']['zone']:
59+
device = Data.devices[aDevice]
60+
except KeyError:
61+
pass
62+
elif type == 'trvcontrol':
63+
device = Data.devices[product["props"]["trvs"][0]]
64+
elif type == 'warmwhitelight' and product['props']['model'] == 'SIREN001':
65+
device = Data.devices[product['parent']]
66+
elif type == 'sense':
67+
device = Data.devices[product['parent']]
68+
else:
69+
device = Data.devices[product['id']]
70+
71+
return device
72+
4873
def convertMinutesToTime(self, minutes_to_convert):
4974
"""Convert minutes string to datetime."""
5075
hours_converted, minutes_converted = divmod(minutes_to_convert, 60)

pyhiveapi/helper/logger.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ async def log(self, n_id, l_type, new_message, **kwargs):
6363
else:
6464
pass
6565

66-
async def error(self, e):
66+
async def error(self, e='UNKNOWN'):
6767
"""An unexpected error has occured"""
6868
self.LOGGER.error(
69-
f"An unexpected error has occured whilst {inspect.stack()[1][3]}"
70-
f" with exception {e}"
69+
f"An unexpected error has occured whilst executing {inspect.stack()[1][3]}"
70+
f" with exception {e.__class__} {e}"
7171
)
7272

7373
async def error_check(self, n_id, n_type, error_type, **kwargs):
@@ -89,11 +89,6 @@ async def error_check(self, n_id, n_type, error_type, **kwargs):
8989
if n_id not in Data.errorList:
9090
self.LOGGER.error(message)
9191
Data.errorList.update({n_id: datetime.now()})
92-
elif error_type == "Failed_API":
93-
new_data = str(kwargs.get("resp"))
94-
message = "ERROR - Received {0} response from API."
95-
result = True
96-
self.LOGGER.error(message.format(new_data))
9792

9893
await self.log(n_id, n_type, message, info=[new_data])
9994
return result

pyhiveapi/light.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def get_light(self, device):
2222

2323
if device["deviceData"]["online"]:
2424
self.helper.deviceRecovered(device["device_id"])
25-
data = Data.devices[device["hiveID"]]
25+
data = Data.devices[device["device_id"]]
2626
dev_data = {
2727
"hiveID": device["hiveID"],
2828
"hiveName": device["hiveName"],
@@ -39,7 +39,7 @@ async def get_light(self, device):
3939
"parentDevice": data.get("parent", None),
4040
"custom": device.get("custom", None),
4141
"attributes": await self.attr.state_attributes(
42-
device["hiveID"], device["hiveType"]
42+
device["device_id"], device["hiveType"]
4343
),
4444
}
4545

pyhiveapi/plug.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def get_plug(self, device):
2020

2121
if device["deviceData"]["online"]:
2222
self.helper.deviceRecovered(device["device_id"])
23-
data = Data.devices[device["hiveID"]]
23+
data = Data.devices[device["device_id"]]
2424
dev_data = {
2525
"hiveID": device["hiveID"],
2626
"hiveName": device["hiveName"],
@@ -37,7 +37,7 @@ async def get_plug(self, device):
3737
"parentDevice": data.get("parent", None),
3838
"custom": device.get("custom", None),
3939
"attributes": await self.attr.state_attributes(
40-
device["hiveID"], device["hiveType"]
40+
device["device_id"], device["hiveType"]
4141
),
4242
}
4343

pyhiveapi/sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def get_sensor(self, device):
6868
"deviceData": data.get("props", None),
6969
"parentDevice": data.get("parent", None),
7070
"attributes": await self.attr.state_attributes(
71-
device["hiveID"], device["hiveType"]
71+
device["device_id"], device["hiveType"]
7272
),
7373
}
7474
)

0 commit comments

Comments
 (0)