Skip to content

Commit

Permalink
Merge pull request #2 from zweckj/feature/webhook
Browse files Browse the repository at this point in the history
Feature/webhook
  • Loading branch information
zweckj authored Jan 10, 2024
2 parents 948cf72 + bbf8672 commit d6c3946
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ async def main():
# client = await TedeeClient.create(personal_token, local_token, ip)
client = TedeeClient(local_ip=ip, local_token=local_token)
bridge = await client.get_local_bridge()
await client.delete_webhook(5)
await client.register_webhook("http://192.168.1.151/events")
await client.get_locks()
await client.sync()
await client.sync()
Expand Down
1 change: 1 addition & 0 deletions pytedee_async/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async def http_request(
HTTPStatus.OK,
HTTPStatus.CREATED,
HTTPStatus.ACCEPTED,
HTTPStatus.NO_CONTENT,
):
return await response.json()
if status_code == HTTPStatus.UNAUTHORIZED:
Expand Down
47 changes: 43 additions & 4 deletions pytedee_async/tedee_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,19 +459,58 @@ def parse_webhook_message(self, message: dict) -> None:

self._locks_dict[lock_id] = lock

async def register_webhook(
async def update_webhooks(
self, webhook_url: str, headers_bridge_sends: list | None = None
) -> None:
"""Register the webhook"""
"""Overrites all webhooks"""
if headers_bridge_sends is None:
headers_bridge_sends = []
_LOGGER.debug("Registering webhook %s", webhook_url)
data = [{"url": webhook_url, "headers": headers_bridge_sends}]
await self._local_api_call("/callback", "PUT", data)
await self._local_api_call("/callback", HTTPMethod.PUT, data)
_LOGGER.debug("Webhook registered successfully.")

async def register_webhook(
self, webhook_url: str, headers_bridge_sends: list | None = None
) -> int:
"""Register a webhook, return the webhook id"""
if headers_bridge_sends is None:
headers_bridge_sends = []
_LOGGER.debug("Registering webhook %s", webhook_url)
data = {"url": webhook_url, "headers": headers_bridge_sends}
try:
success, result = await self._local_api_call("/callback", "POST", data)
except TedeeDataUpdateException as ex:
raise TedeeWebhookException("Unable to register webhook") from ex
if not success:
raise TedeeWebhookException("Unable to register webhook")
_LOGGER.debug("Webhook registered successfully.")
# get the webhook id
try:
success, result = await self._local_api_call("/callback", HTTPMethod.GET)
except TedeeDataUpdateException as ex:
raise TedeeWebhookException("Unable to get webhooks") from ex
if not success or result is None:
raise TedeeWebhookException("Unable to get webhooks")
for webhook in result:
if webhook["url"] == webhook_url:
return webhook["id"]
raise TedeeWebhookException("Webhook id not found")

async def delete_webhooks(self) -> None:
"""Delete all webhooks"""
_LOGGER.debug("Deleting webhooks...")
await self._local_api_call("/callback", "PUT", [])
try:
await self._local_api_call("/callback", "PUT", [])
except TedeeDataUpdateException as ex:
_LOGGER.debug("Unable to delete webhooks: %s", str(ex))
_LOGGER.debug("Webhooks deleted successfully.")

async def delete_webhook(self, webhook_id: int) -> None:
"""Delete a specific webhook"""
_LOGGER.debug("Deleting webhook %s", str(webhook_id))
try:
await self._local_api_call(f"/callback/{webhook_id}", HTTPMethod.DELETE)
except TedeeDataUpdateException as ex:
_LOGGER.debug("Unable to delete webhook: %s", str(ex))
_LOGGER.debug("Webhook deleted successfully.")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pytedee_async",
version="0.2.10",
version="0.2.11",
author="Josef Zweck",
author_email="[email protected]",
description="A Tedee Lock Client package",
Expand Down

0 comments on commit d6c3946

Please sign in to comment.