Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate special web IDs for Web API calls #92

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions custom_components/nuki_ng/nuki.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ async def _update(self):
info_mapping = dict()
device_list = None
web_list = None
def web_id_for_item(item):
as_hex = "{0:x}".format(item["nukiId"])
deviceType = item.get("deviceType", 0)
if deviceType == 2:
as_hex = f"2{as_hex}"
elif deviceType == 3:
as_hex = f"3{as_hex}"
elif deviceType == 4:
as_hex = f"4{as_hex}"
return int(as_hex, 16)
if self.api.can_bridge():
try:
callbacks_list = await self.api.bridge_check_callback(
Expand All @@ -399,20 +409,22 @@ async def _update(self):
for key, item in device_list.items():
dev_id = item["nukiId"]
if self.api.can_web():
web_id = web_id_for_item(item)
item["webId"] = web_id
try:
item["web_auth"] = await self.api.web_list_all_auths(dev_id)
item["web_auth"] = await self.api.web_list_all_auths(web_id)
except ConnectionError:
_LOGGER.warning("Despite being configured, Web API request has failed")
_LOGGER.exception("Error while fetching auth:")
try:
item["last_log"] = await self.api.web_get_last_unlock_log(dev_id)
item["last_log"] = await self.api.web_get_last_unlock_log(web_id)
except ConnectionError:
_LOGGER.warning("Despite being configured, Web API request has failed")
_LOGGER.exception("Error while fetching last log entry")
if web_list:
item["config"] = web_list.get(dev_id, {}).get("config")
item["advancedConfig"] = web_list.get(dev_id, {}).get("advancedConfig")
item["openerAdvancedConfig"] = web_list.get(dev_id, {}).get("openerAdvancedConfig")
item["config"] = web_list.get(web_id, {}).get("config")
item["advancedConfig"] = web_list.get(web_id, {}).get("advancedConfig")
item["openerAdvancedConfig"] = web_list.get(web_id, {}).get("openerAdvancedConfig")
result["devices"][dev_id] = item
result["devices"][dev_id]["bridge_info"] = info_mapping.get(dev_id)
_LOGGER.debug(f"_update: {json.dumps(result)}")
Expand Down Expand Up @@ -451,7 +463,7 @@ async def action(self, dev_id: str, action: str):
await self.async_request_refresh()
_LOGGER.debug(f"bridge action result: {result}, {action}")
elif self.api.can_web():
await self.api.web_lock_action(dev_id, action)
await self.api.web_lock_action(self.web_id(dev_id), action)
await self.async_request_refresh()
_LOGGER.debug(f"web action result: {action}")

Expand All @@ -476,6 +488,9 @@ async def do_delete_callback(self, callback):
def device_data(self, dev_id: str):
return self.data.get("devices", {}).get(dev_id, {})

def web_id(self, dev_id: str):
return self.device_data(dev_id).get("webId", dev_id)

def info_data(self):
return self.data.get("info", {})

Expand All @@ -499,7 +514,7 @@ def info_field(self, dev_id: str, default, *args):
async def update_web_auth(self, dev_id: str, auth: dict, changes: dict):
if "id" not in auth:
raise UpdateFailed("Invalid auth entry")
await self.api.web_update_auth(dev_id, auth["id"], changes)
await self.api.web_update_auth(self.web_id(dev_id), auth["id"], changes)
data = self.data
for key in changes:
data.get(dev_id, {}).get("web_auth", {}).get(auth["id"], {})[key] = changes[
Expand All @@ -513,5 +528,5 @@ async def update_config(self, dev_id: str, name: str, changes: dict):
for key in changes:
obj[key] = changes[key]
_LOGGER.debug(f"Updating config: {name}: {changes} = {obj}")
await self.api.web_update_config(dev_id, name, obj)
await self.api.web_update_config(self.web_id(dev_id), name, obj)
self.async_set_updated_data(data)