-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
8 changed files
with
339 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Home Assistant client for supervisor.""" | ||
|
||
from .client import _SupervisorComponentClient | ||
from .models.homeassistant import ( | ||
HomeAssistantInfo, | ||
HomeAssistantOptions, | ||
HomeAssistantRebuildOptions, | ||
HomeAssistantRestartOptions, | ||
HomeAssistantStats, | ||
HomeAssistantStopOptions, | ||
HomeAssistantUpdateOptions, | ||
) | ||
|
||
|
||
class HomeAssistantClient(_SupervisorComponentClient): | ||
"""Handles Home Assistant access in supervisor.""" | ||
|
||
async def info(self) -> HomeAssistantInfo: | ||
"""Get Home Assistant info.""" | ||
result = await self._client.get("core/info") | ||
return HomeAssistantInfo.from_dict(result.data) | ||
|
||
async def stats(self) -> HomeAssistantStats: | ||
"""Get Home Assistant stats.""" | ||
result = await self._client.get("core/stats") | ||
return HomeAssistantStats.from_dict(result.data) | ||
|
||
async def options(self, options: HomeAssistantOptions) -> None: | ||
"""Set Home Assistant options.""" | ||
await self._client.post("core/options", json=options.to_dict()) | ||
|
||
async def update(self, options: HomeAssistantUpdateOptions | None = None) -> None: | ||
"""Update Home Assistant.""" | ||
await self._client.post( | ||
"core/update", json=options.to_dict() if options else None | ||
) | ||
|
||
async def restart(self, options: HomeAssistantRestartOptions | None = None) -> None: | ||
"""Restart Home Assistant.""" | ||
await self._client.post( | ||
"core/restart", json=options.to_dict() if options else None | ||
) | ||
|
||
async def stop(self, options: HomeAssistantStopOptions | None = None) -> None: | ||
"""Stop Home Assistant.""" | ||
await self._client.post( | ||
"core/stop", json=options.to_dict() if options else None | ||
) | ||
|
||
async def start(self) -> None: | ||
"""Start Home Assistant.""" | ||
await self._client.post("core/start") | ||
|
||
async def check_config(self) -> None: | ||
"""Check Home Assistant config.""" | ||
await self._client.post("core/check") | ||
|
||
async def rebuild(self, options: HomeAssistantRebuildOptions | None = None) -> None: | ||
"""Rebuild Home Assistant.""" | ||
await self._client.post( | ||
"core/rebuild", json=options.to_dict() if options else None | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""Models for Home Assistant.""" | ||
|
||
from dataclasses import dataclass | ||
from ipaddress import IPv4Address | ||
|
||
from .base import DEFAULT, ContainerStats, Options, Request, ResponseData | ||
|
||
# --- OBJECTS ---- | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class HomeAssistantInfo(ResponseData): | ||
"""HomeAssistantInfo model.""" | ||
|
||
version: str | None | ||
version_latest: str | None | ||
update_available: bool | ||
machine: str | ||
ip_address: IPv4Address | ||
arch: str | ||
image: str | ||
boot: bool | ||
port: int | ||
ssl: bool | ||
watchdog: bool | ||
audio_input: str | ||
audio_output: str | ||
backups_exclude_database: bool | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class HomeAssistantStats(ContainerStats): | ||
"""HomeAssistantStats model.""" | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class HomeAssistantOptions(Options): | ||
"""HomeAssistantOptions model.""" | ||
|
||
boot: bool | None = None | ||
image: str | None = DEFAULT # type: ignore[assignment] | ||
port: int | None = None | ||
ssl: bool | None = None | ||
watchdog: bool | None = None | ||
refresh_token: str | None = DEFAULT # type: ignore[assignment] | ||
audio_input: str | None = DEFAULT # type: ignore[assignment] | ||
audio_output: str | None = DEFAULT # type: ignore[assignment] | ||
backups_exclude_database: bool | None = None | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class HomeAssistantUpdateOptions(Options): | ||
"""HomeAssistantUpdateOptions model.""" | ||
|
||
version: str | None = None | ||
backup: bool | None = None | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class HomeAssistantRestartOptions(Options): | ||
"""HomeAssistantRestartOptions model.""" | ||
|
||
safe_mode: bool | None = None | ||
force: bool | None = None | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class HomeAssistantRebuildOptions(Options): | ||
"""HomeAssistantRebuildOptions model.""" | ||
|
||
safe_mode: bool | None = None | ||
force: bool | None = None | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class HomeAssistantStopOptions(Request): | ||
"""HomeAssistantStopOptions model.""" | ||
|
||
force: bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"result": "ok", | ||
"data": { | ||
"version": "2024.9.0", | ||
"version_latest": "2024.9.0", | ||
"update_available": false, | ||
"machine": "odroid-n2", | ||
"ip_address": "172.30.32.1", | ||
"arch": "aarch64", | ||
"image": "ghcr.io/home-assistant/odroid-n2-homeassistant", | ||
"boot": true, | ||
"port": 8123, | ||
"ssl": false, | ||
"watchdog": true, | ||
"audio_input": null, | ||
"audio_output": null, | ||
"backups_exclude_database": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"result": "ok", | ||
"data": { | ||
"cpu_percent": 0.01, | ||
"memory_usage": 678883328, | ||
"memory_limit": 3899138048, | ||
"memory_percent": 17.41, | ||
"network_rx": 0, | ||
"network_tx": 0, | ||
"blk_read": 0, | ||
"blk_write": 0 | ||
} | ||
} |
Oops, something went wrong.