| 1 |
"""Manage cloud cloudhooks.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import asyncio |
| 6 |
from typing import TYPE_CHECKING, Any, TypedDict |
| 7 |
|
| 8 |
from .api import ApiBase, CloudApiError, api_exception_handler |
| 9 |
from .events.types import CloudhookCreatedEvent, CloudhookDeletedEvent |
| 10 |
|
| 11 |
if TYPE_CHECKING: |
| 12 |
from . import Cloud, _ClientT |
| 13 |
|
| 14 |
|
| 15 |
class CloudhookDetails(TypedDict): |
| 16 |
"""Details of a cloudhook.""" |
| 17 |
|
| 18 |
webhook_id: str |
| 19 |
cloudhook_id: str |
| 20 |
cloudhook_url: str |
| 21 |
managed: bool |
| 22 |
|
| 23 |
|
| 24 |
class GeneratedCloudhookDetails(TypedDict): |
| 25 |
"""Details of a generated cloudhook.""" |
| 26 |
|
| 27 |
url: str |
| 28 |
cloudhook_id: str |
| 29 |
|
| 30 |
|
| 31 |
class CloudhookApiError(CloudApiError): |
| 32 |
"""Error raised when a cloudhook API call fails.""" |
| 33 |
|
| 34 |
|
| 35 |
class Cloudhooks(ApiBase): |
| 36 |
"""Class to help manage cloudhooks.""" |
| 37 |
|
| 38 |
def __init__(self, cloud: Cloud[_ClientT]) -> None: |
| 39 |
"""Initialize cloudhooks.""" |
| 40 |
super().__init__(cloud) |
| 41 |
cloud.iot.register_on_connect(self.async_publish_cloudhooks) |
| 42 |
|
| 43 |
async def async_publish_cloudhooks(self) -> None: |
| 44 |
"""Inform the Relayer of the cloudhooks that we support.""" |
| 45 |
if not self._cloud.is_connected: |
| 46 |
return |
| 47 |
|
| 48 |
cloudhooks = self._cloud.client.cloudhooks |
| 49 |
await self._cloud.iot.async_send_message( |
| 50 |
"webhook-register", |
| 51 |
{"cloudhook_ids": [info["cloudhook_id"] for info in cloudhooks.values()]}, |
| 52 |
expect_answer=False, |
| 53 |
) |
| 54 |
|
| 55 |
async def async_create(self, webhook_id: str, managed: bool) -> CloudhookDetails: |
| 56 |
"""Create a cloud webhook.""" |
| 57 |
cloudhooks = self._cloud.client.cloudhooks |
| 58 |
|
| 59 |
if webhook_id in cloudhooks: |
| 60 |
raise ValueError("Hook is already enabled for the cloud.") |
| 61 |
|
| 62 |
if not self._cloud.iot.connected: |
| 63 |
raise ValueError("Cloud is not connected") |
| 64 |
|
| 65 |
# Create cloud hook |
| 66 |
async with asyncio.timeout(10): |
| 67 |
data = await self.generate() |
| 68 |
|
| 69 |
cloudhook_id = data["cloudhook_id"] |
| 70 |
cloudhook_url = data["url"] |
| 71 |
|
| 72 |
# Store hook |
| 73 |
cloudhooks_updated: dict[str, Any] = dict(cloudhooks) |
| 74 |
cloudhook: CloudhookDetails = { |
| 75 |
"webhook_id": webhook_id, |
| 76 |
"cloudhook_id": cloudhook_id, |
| 77 |
"cloudhook_url": cloudhook_url, |
| 78 |
"managed": managed, |
| 79 |
} |
| 80 |
cloudhooks_updated[webhook_id] = cloudhook |
| 81 |
await self._cloud.client.async_cloudhooks_update(cloudhooks_updated) |
| 82 |
|
| 83 |
await self.async_publish_cloudhooks() |
| 84 |
await self._cloud.events.publish(CloudhookCreatedEvent(cloudhook=cloudhook)) |
| 85 |
return cloudhook |
| 86 |
|
| 87 |
async def async_delete(self, webhook_id: str) -> None: |
| 88 |
"""Delete a cloud webhook.""" |
| 89 |
cloudhooks = self._cloud.client.cloudhooks |
| 90 |
|
| 91 |
if webhook_id not in cloudhooks: |
| 92 |
raise ValueError("Hook is not enabled for the cloud.") |
| 93 |
|
| 94 |
# Remove hook |
| 95 |
cloudhooks_updated: dict[str, Any] = dict(cloudhooks) |
| 96 |
cloudhook: CloudhookDetails = cloudhooks_updated.pop(webhook_id) |
| 97 |
await self._cloud.client.async_cloudhooks_update(cloudhooks_updated) |
| 98 |
|
| 99 |
await self._cloud.events.publish(CloudhookDeletedEvent(cloudhook=cloudhook)) |
| 100 |
await self.async_publish_cloudhooks() |
| 101 |
|
| 102 |
@api_exception_handler(CloudhookApiError) |
| 103 |
async def generate(self) -> GeneratedCloudhookDetails: |
| 104 |
"""Get generated cloudhook details.""" |
| 105 |
details: GeneratedCloudhookDetails = await self._call_cloud_api( |
| 106 |
action="webhook_generate", |
| 107 |
) |
| 108 |
return details |