Skip to content

Commit d04e78c

Browse files
authored
Add core APIs to client library (#15)
* Add core APIs to client library * Use IPv4Address for ip address * Separate rebuild options and test passing options * Use rebuild options
1 parent 31cbaed commit d04e78c

File tree

8 files changed

+339
-11
lines changed

8 files changed

+339
-11
lines changed

aiohasupervisor/homeassistant.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Home Assistant client for supervisor."""
2+
3+
from .client import _SupervisorComponentClient
4+
from .models.homeassistant import (
5+
HomeAssistantInfo,
6+
HomeAssistantOptions,
7+
HomeAssistantRebuildOptions,
8+
HomeAssistantRestartOptions,
9+
HomeAssistantStats,
10+
HomeAssistantStopOptions,
11+
HomeAssistantUpdateOptions,
12+
)
13+
14+
15+
class HomeAssistantClient(_SupervisorComponentClient):
16+
"""Handles Home Assistant access in supervisor."""
17+
18+
async def info(self) -> HomeAssistantInfo:
19+
"""Get Home Assistant info."""
20+
result = await self._client.get("core/info")
21+
return HomeAssistantInfo.from_dict(result.data)
22+
23+
async def stats(self) -> HomeAssistantStats:
24+
"""Get Home Assistant stats."""
25+
result = await self._client.get("core/stats")
26+
return HomeAssistantStats.from_dict(result.data)
27+
28+
async def options(self, options: HomeAssistantOptions) -> None:
29+
"""Set Home Assistant options."""
30+
await self._client.post("core/options", json=options.to_dict())
31+
32+
async def update(self, options: HomeAssistantUpdateOptions | None = None) -> None:
33+
"""Update Home Assistant."""
34+
await self._client.post(
35+
"core/update", json=options.to_dict() if options else None
36+
)
37+
38+
async def restart(self, options: HomeAssistantRestartOptions | None = None) -> None:
39+
"""Restart Home Assistant."""
40+
await self._client.post(
41+
"core/restart", json=options.to_dict() if options else None
42+
)
43+
44+
async def stop(self, options: HomeAssistantStopOptions | None = None) -> None:
45+
"""Stop Home Assistant."""
46+
await self._client.post(
47+
"core/stop", json=options.to_dict() if options else None
48+
)
49+
50+
async def start(self) -> None:
51+
"""Start Home Assistant."""
52+
await self._client.post("core/start")
53+
54+
async def check_config(self) -> None:
55+
"""Check Home Assistant config."""
56+
await self._client.post("core/check")
57+
58+
async def rebuild(self, options: HomeAssistantRebuildOptions | None = None) -> None:
59+
"""Rebuild Home Assistant."""
60+
await self._client.post(
61+
"core/rebuild", json=options.to_dict() if options else None
62+
)

aiohasupervisor/models/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
StoreInfo,
2424
SupervisorRole,
2525
)
26+
from aiohasupervisor.models.homeassistant import (
27+
HomeAssistantInfo,
28+
HomeAssistantOptions,
29+
HomeAssistantRebuildOptions,
30+
HomeAssistantRestartOptions,
31+
HomeAssistantStats,
32+
HomeAssistantStopOptions,
33+
HomeAssistantUpdateOptions,
34+
)
2635
from aiohasupervisor.models.resolution import (
2736
Check,
2837
CheckOptions,
@@ -96,4 +105,11 @@
96105
"SupervisorOptions",
97106
"SupervisorStats",
98107
"SupervisorUpdateOptions",
108+
"HomeAssistantInfo",
109+
"HomeAssistantOptions",
110+
"HomeAssistantRebuildOptions",
111+
"HomeAssistantRestartOptions",
112+
"HomeAssistantStats",
113+
"HomeAssistantStopOptions",
114+
"HomeAssistantUpdateOptions",
99115
]

aiohasupervisor/models/addons.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from mashumaro import field_options
1010
from mashumaro.config import TO_DICT_ADD_BY_ALIAS_FLAG, BaseConfig
1111

12-
from .base import DEFAULT, Options, Request, RequestConfig, ResponseData
12+
from .base import DEFAULT, ContainerStats, Options, Request, RequestConfig, ResponseData
1313

1414
# --- ENUMS ----
1515

@@ -296,18 +296,9 @@ class AddonsSecurityOptions(Options):
296296

297297

298298
@dataclass(frozen=True, slots=True)
299-
class AddonsStats(ResponseData):
299+
class AddonsStats(ContainerStats):
300300
"""AddonsStats model."""
301301

302-
cpu_percent: float
303-
memory_usage: int
304-
memory_limit: int
305-
memory_percent: float
306-
network_rx: int
307-
network_tx: int
308-
blk_read: int
309-
blk_write: int
310-
311302

312303
@dataclass(frozen=True, slots=True)
313304
class AddonsUninstall(Request):
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""Models for Home Assistant."""
2+
3+
from dataclasses import dataclass
4+
from ipaddress import IPv4Address
5+
6+
from .base import DEFAULT, ContainerStats, Options, Request, ResponseData
7+
8+
# --- OBJECTS ----
9+
10+
11+
@dataclass(frozen=True, slots=True)
12+
class HomeAssistantInfo(ResponseData):
13+
"""HomeAssistantInfo model."""
14+
15+
version: str | None
16+
version_latest: str | None
17+
update_available: bool
18+
machine: str
19+
ip_address: IPv4Address
20+
arch: str
21+
image: str
22+
boot: bool
23+
port: int
24+
ssl: bool
25+
watchdog: bool
26+
audio_input: str
27+
audio_output: str
28+
backups_exclude_database: bool
29+
30+
31+
@dataclass(frozen=True, slots=True)
32+
class HomeAssistantStats(ContainerStats):
33+
"""HomeAssistantStats model."""
34+
35+
36+
@dataclass(frozen=True, slots=True)
37+
class HomeAssistantOptions(Options):
38+
"""HomeAssistantOptions model."""
39+
40+
boot: bool | None = None
41+
image: str | None = DEFAULT # type: ignore[assignment]
42+
port: int | None = None
43+
ssl: bool | None = None
44+
watchdog: bool | None = None
45+
refresh_token: str | None = DEFAULT # type: ignore[assignment]
46+
audio_input: str | None = DEFAULT # type: ignore[assignment]
47+
audio_output: str | None = DEFAULT # type: ignore[assignment]
48+
backups_exclude_database: bool | None = None
49+
50+
51+
@dataclass(frozen=True, slots=True)
52+
class HomeAssistantUpdateOptions(Options):
53+
"""HomeAssistantUpdateOptions model."""
54+
55+
version: str | None = None
56+
backup: bool | None = None
57+
58+
59+
@dataclass(frozen=True, slots=True)
60+
class HomeAssistantRestartOptions(Options):
61+
"""HomeAssistantRestartOptions model."""
62+
63+
safe_mode: bool | None = None
64+
force: bool | None = None
65+
66+
67+
@dataclass(frozen=True, slots=True)
68+
class HomeAssistantRebuildOptions(Options):
69+
"""HomeAssistantRebuildOptions model."""
70+
71+
safe_mode: bool | None = None
72+
force: bool | None = None
73+
74+
75+
@dataclass(frozen=True, slots=True)
76+
class HomeAssistantStopOptions(Request):
77+
"""HomeAssistantStopOptions model."""
78+
79+
force: bool

aiohasupervisor/root.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from .addons import AddonsClient
88
from .client import _SupervisorClient
9+
from .homeassistant import HomeAssistantClient
910
from .models.root import AvailableUpdate, AvailableUpdates, RootInfo
1011
from .resolution import ResolutionClient
1112
from .store import StoreClient
@@ -28,12 +29,18 @@ def __init__(
2829
self._resolution = ResolutionClient(self._client)
2930
self._store = StoreClient(self._client)
3031
self._supervisor = SupervisorManagementClient(self._client)
32+
self._homeassistant = HomeAssistantClient(self._client)
3133

3234
@property
3335
def addons(self) -> AddonsClient:
3436
"""Get addons component client."""
3537
return self._addons
3638

39+
@property
40+
def homeassistant(self) -> HomeAssistantClient:
41+
"""Get Home Assistant component client."""
42+
return self._homeassistant
43+
3744
@property
3845
def resolution(self) -> ResolutionClient:
3946
"""Get resolution center component client."""
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"result": "ok",
3+
"data": {
4+
"version": "2024.9.0",
5+
"version_latest": "2024.9.0",
6+
"update_available": false,
7+
"machine": "odroid-n2",
8+
"ip_address": "172.30.32.1",
9+
"arch": "aarch64",
10+
"image": "ghcr.io/home-assistant/odroid-n2-homeassistant",
11+
"boot": true,
12+
"port": 8123,
13+
"ssl": false,
14+
"watchdog": true,
15+
"audio_input": null,
16+
"audio_output": null,
17+
"backups_exclude_database": false
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"result": "ok",
3+
"data": {
4+
"cpu_percent": 0.01,
5+
"memory_usage": 678883328,
6+
"memory_limit": 3899138048,
7+
"memory_percent": 17.41,
8+
"network_rx": 0,
9+
"network_tx": 0,
10+
"blk_read": 0,
11+
"blk_write": 0
12+
}
13+
}

0 commit comments

Comments
 (0)