Debian Codegraph @

hass_nabucasa/instance_api.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 """Manage instance API."""
2
3 from __future__ import annotations
4
5 import logging
6 from typing import Literal, NotRequired, TypedDict
7
8 from aiohttp import hdrs
9
10 from .api import ApiBase, CloudApiError, api_exception_handler
11
12 _LOGGER = logging.getLogger(__name__)
13
14
15 class InstanceApiError(CloudApiError):
16 """Exception raised when handling instance API."""
17
18
19 class InstanceConnectionDetails(TypedDict):
20 """Connection details from instance API."""
21
22 connected_at: str
23 name: str
24 remote_ip_address: str
25 version: str
26
27
28 class InstanceConnectionConnnected(TypedDict):
29 """Connection details from instance API."""
30
31 connected: Literal[True]
32 details: InstanceConnectionDetails
33
34
35 class InstanceConnectionDisconnected(TypedDict):
36 """Connection details from instance API."""
37
38 connected: Literal[False]
39
40
41 type InstanceConnection = InstanceConnectionConnnected | InstanceConnectionDisconnected
42
43
44 class InstanceRegistrationDetails(TypedDict):
45 """Registration details from instance API."""
46
47 alias: NotRequired[list[str]]
48 domain: str
49 email: str
50 server: str
51
52
53 class InstanceSnitunTokenDetails(TypedDict):
54 """Snitun token details from instance API."""
55
56 token: str
57 server: str
58 valid: int
59 throttling: int
60
61
62 class PingResult(TypedDict):
63 """Ping result for a single target."""
64
65 ip: str
66 avg: float
67 max: float
68 min: float
69 reachable: bool
70
71
72 class PingHost(TypedDict):
73 """Host that can be pinged for latency measurement."""
74
75 ip: str
76 location: str
77
78
79 class PingTargetsResponse(TypedDict):
80 """Response from ping targets API."""
81
82 targets: list[PingHost]
83 timeout: int
84 count: int
85
86
87 class InstanceApi(ApiBase):
88 """Class to help communicate with the instance API."""
89
90 @api_exception_handler(InstanceApiError)
91 async def connection(
92 self,
93 *,
94 access_token: str | None = None,
95 skip_token_check: bool = False,
96 ) -> InstanceConnection:
97 """Get the connection details."""
98 _LOGGER.debug("Getting instance connection details")
99 details: InstanceConnection = await self._call_cloud_api(
100 action="instance_connection",
101 headers={
102 hdrs.AUTHORIZATION: access_token or self._cloud.access_token,
103 },
104 skip_token_check=skip_token_check,
105 )
106 return details
107
108 @api_exception_handler(InstanceApiError)
109 async def cleanup_dns_challenge_record(self, *, value: str) -> None:
110 """Remove DNS challenge."""
111 await self._call_cloud_api(
112 method="POST",
113 action="remote_access_dns_challenge_remove",
114 jsondata={"txt": value},
115 )
116
117 @api_exception_handler(InstanceApiError)
118 async def create_dns_challenge_record(self, *, value: str) -> None:
119 """Set DNS challenge."""
120 await self._call_cloud_api(
121 method="POST",
122 action="remote_access_dns_challenge_set",
123 jsondata={"txt": value},
124 )
125
126 @api_exception_handler(InstanceApiError)
127 async def ping_targets(self) -> PingTargetsResponse:
128 """Get ping targets for latency measurement."""
129 details: PingTargetsResponse = await self._call_cloud_api(
130 action="remote_access_ping_targets",
131 )
132 return details
133
134 @api_exception_handler(InstanceApiError)
135 async def register(
136 self,
137 *,
138 ping_result: list[PingResult] | None = None,
139 ) -> InstanceRegistrationDetails:
140 """Register the instance."""
141 details: InstanceRegistrationDetails = await self._call_cloud_api(
142 method="POST",
143 action="remote_access_register",
144 jsondata={"ping_result": ping_result} if ping_result is not None else None,
145 )
146 return details
147
148 @api_exception_handler(InstanceApiError)
149 async def snitun_token(
150 self,
151 *,
152 aes_key: bytes,
153 aes_iv: bytes,
154 ) -> InstanceSnitunTokenDetails:
155 """Create a remote snitun token."""
156 details: InstanceSnitunTokenDetails = await self._call_cloud_api(
157 method="POST",
158 action="remote_access_snitun_token",
159 jsondata={"aes_key": aes_key.hex(), "aes_iv": aes_iv.hex()},
160 )
161 return details