-
Notifications
You must be signed in to change notification settings - Fork 4
Add discovery APIs from supervisor #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
| """Discovery client for supervisor.""" | ||
|
|
||
| from uuid import UUID | ||
|
|
||
| from .client import _SupervisorComponentClient | ||
| from .const import ResponseType | ||
| from .models.discovery import Discovery, DiscoveryConfig, DiscoveryList, SetDiscovery | ||
|
|
||
|
|
||
| class DiscoveryClient(_SupervisorComponentClient): | ||
| """Handles discovery access in supervisor.""" | ||
|
|
||
| async def list(self) -> list[Discovery]: | ||
| """List discovered active services.""" | ||
| result = await self._client.get("discovery") | ||
| return DiscoveryList.from_dict(result.data).discovery | ||
|
|
||
| async def get(self, uuid: UUID) -> Discovery: | ||
| """Get discovery details for a service.""" | ||
| result = await self._client.get(f"discovery/{uuid.hex}") | ||
| return Discovery.from_dict(result.data) | ||
|
|
||
| async def delete(self, uuid: UUID) -> None: | ||
| """Remove discovery for a service.""" | ||
| await self._client.delete(f"discovery/{uuid.hex}") | ||
|
|
||
| async def set(self, config: DiscoveryConfig) -> UUID: | ||
| """Inform supervisor of an available service.""" | ||
| result = await self._client.post( | ||
| "discovery", json=config.to_dict(), response_type=ResponseType.JSON | ||
| ) | ||
| return SetDiscovery.from_dict(result.data).uuid |
This file contains hidden or 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 hidden or 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,39 @@ | ||
| """Models for discovery component.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any | ||
| from uuid import UUID | ||
|
|
||
| from .base import Request, ResponseData | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class DiscoveryConfig(Request): | ||
| """DiscoveryConfig model.""" | ||
|
|
||
| service: str | ||
| config: dict[str, Any] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class Discovery(ResponseData): | ||
| """Discovery model.""" | ||
|
|
||
| addon: str | ||
| service: str | ||
| uuid: UUID | ||
| config: dict[str, Any] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class DiscoveryList(ResponseData): | ||
| """DiscoveryList model.""" | ||
|
|
||
| discovery: list[Discovery] | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class SetDiscovery(ResponseData): | ||
| """SetDiscovery model.""" | ||
|
|
||
| uuid: UUID | ||
This file contains hidden or 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 hidden or 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,16 @@ | ||
| { | ||
| "result": "ok", | ||
| "data": { | ||
| "addon": "core_mosquitto", | ||
| "service": "mqtt", | ||
| "uuid": "889ca604cff84004894e53d181655b3a", | ||
| "config": { | ||
| "host": "core-mosquitto", | ||
| "port": 1883, | ||
| "ssl": false, | ||
| "protocol": "3.1.1", | ||
| "username": "homeassistant", | ||
| "password": "abc123" | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,45 @@ | ||
| { | ||
| "result": "ok", | ||
| "data": { | ||
| "discovery": [ | ||
| { | ||
| "addon": "core_mosquitto", | ||
| "service": "mqtt", | ||
| "uuid": "889ca604cff84004894e53d181655b3a", | ||
| "config": { | ||
| "host": "core-mosquitto", | ||
| "port": 1883, | ||
| "ssl": false, | ||
| "protocol": "3.1.1", | ||
| "username": "homeassistant", | ||
| "password": "abc123" | ||
| } | ||
| } | ||
| ], | ||
| "services": { | ||
| "vlc_telnet": ["core_vlc"], | ||
| "esphome": [ | ||
| "5c53de3b_esphome-beta", | ||
| "5c53de3b_esphome-dev", | ||
| "5c53de3b_esphome" | ||
| ], | ||
| "zwave_js": [ | ||
| "core_zwave_js", | ||
| "a0d7b954_zwavejs2mqtt", | ||
| "77f1785d_zwave_mock_server" | ||
| ], | ||
| "matter": ["core_matter_server"], | ||
| "wyoming": [ | ||
| "core_whisper", | ||
| "core_assist_microphone", | ||
| "core_piper", | ||
| "core_openwakeword" | ||
| ], | ||
| "otbr": ["core_openthread_border_router", "core_silabs_multiprotocol"], | ||
| "mqtt": ["core_mosquitto"], | ||
| "motioneye": ["a0d7b954_motioneye"], | ||
| "deconz": ["core_deconz"], | ||
| "adguard": ["a0d7b954_adguard"] | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,6 @@ | ||
| { | ||
| "result": "ok", | ||
| "data": { | ||
| "uuid": "889ca604cff84004894e53d181655b3a" | ||
| } | ||
| } |
This file contains hidden or 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,73 @@ | ||
| """Test discovery supervisor client.""" | ||
|
|
||
| from uuid import UUID | ||
|
|
||
| from aioresponses import aioresponses | ||
| from yarl import URL | ||
|
|
||
| from aiohasupervisor import SupervisorClient | ||
| from aiohasupervisor.models import DiscoveryConfig | ||
|
|
||
| from . import load_fixture | ||
| from .const import SUPERVISOR_URL | ||
|
|
||
|
|
||
| async def test_discovery_list( | ||
| responses: aioresponses, supervisor_client: SupervisorClient | ||
| ) -> None: | ||
| """Test Discovery list API.""" | ||
| responses.get( | ||
| f"{SUPERVISOR_URL}/discovery", | ||
| status=200, | ||
| body=load_fixture("discovery_list.json"), | ||
| ) | ||
| disc_list = await supervisor_client.discovery.list() | ||
| assert disc_list[0].addon == "core_mosquitto" | ||
| assert disc_list[0].service == "mqtt" | ||
| assert disc_list[0].uuid.hex == "889ca604cff84004894e53d181655b3a" | ||
| assert disc_list[0].config["host"] == "core-mosquitto" | ||
|
|
||
|
|
||
| async def test_get_discovery( | ||
| responses: aioresponses, supervisor_client: SupervisorClient | ||
| ) -> None: | ||
| """Test Discovery get API.""" | ||
| uuid = UUID("889ca604cff84004894e53d181655b3a") | ||
| responses.get( | ||
| f"{SUPERVISOR_URL}/discovery/{uuid.hex}", | ||
| status=200, | ||
| body=load_fixture("discovery_get.json"), | ||
| ) | ||
| discovery = await supervisor_client.discovery.get(uuid) | ||
| assert discovery.addon == "core_mosquitto" | ||
| assert discovery.service == "mqtt" | ||
| assert discovery.uuid == uuid | ||
| assert discovery.config["host"] == "core-mosquitto" | ||
| assert discovery.config["port"] == 1883 | ||
| assert discovery.config["ssl"] is False | ||
|
|
||
|
|
||
| async def test_delete_discovery( | ||
| responses: aioresponses, supervisor_client: SupervisorClient | ||
| ) -> None: | ||
| """Test Discovery delete API.""" | ||
| uuid = UUID("889ca604cff84004894e53d181655b3a") | ||
| responses.delete(f"{SUPERVISOR_URL}/discovery/{uuid.hex}", status=200) | ||
| assert await supervisor_client.discovery.delete(uuid) is None | ||
| assert responses.requests.keys() == { | ||
| ("DELETE", URL(f"{SUPERVISOR_URL}/discovery/{uuid.hex}")) | ||
| } | ||
|
|
||
|
|
||
| async def test_set_discovery( | ||
| responses: aioresponses, supervisor_client: SupervisorClient | ||
| ) -> None: | ||
| """Test Discovery set API.""" | ||
| responses.post( | ||
| f"{SUPERVISOR_URL}/discovery", | ||
| status=200, | ||
| body=load_fixture("discovery_set.json"), | ||
| ) | ||
| assert await supervisor_client.discovery.set( | ||
| DiscoveryConfig(service="mqtt", config={}) | ||
| ) == UUID("889ca604cff84004894e53d181655b3a") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.