Skip to content

Commit

Permalink
Add support for return values when creating a URL (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy authored Nov 24, 2024
1 parent 0c33b2d commit fe5997e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions custom_components/hass_web_proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ProxiedURL,
ProxyView,
)
from homeassistant.core import callback
from homeassistant.core import ServiceResponse, SupportsResponse, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
Expand Down Expand Up @@ -97,7 +97,7 @@ async def async_setup_entry(
dynamic_proxied_urls={},
)

def create_proxied_url(call: ServiceCall) -> None:
def create_proxied_url(call: ServiceCall) -> ServiceResponse:
"""Create a proxied URL."""
url_id = call.data.get("url_id") or str(uuid.uuid4())
ttl = call.data["ttl"]
Expand All @@ -110,6 +110,7 @@ def create_proxied_url(call: ServiceCall) -> None:
expiration=time.time() + ttl if ttl else 0,
allow_unauthenticated=call.data["allow_unauthenticated"],
)
return {"url_id": url_id}

def delete_proxied_url(call: ServiceCall) -> None:
"""Delete a proxied URL."""
Expand All @@ -130,6 +131,7 @@ def delete_proxied_url(call: ServiceCall) -> None:
SERVICE_CREATE_PROXIED_URL,
create_proxied_url,
CREATE_PROXIED_URL_SCHEMA,
supports_response=SupportsResponse.OPTIONAL,
)
hass.services.async_register(
DOMAIN,
Expand Down
29 changes: 29 additions & 0 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import datetime
import urllib
import uuid
from http import HTTPStatus
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -179,6 +180,34 @@ async def test_proxy_view_dynamic_url_success(
assert resp.status == HTTPStatus.OK


async def test_proxy_view_dynamic_url_return_value(
hass: HomeAssistant,
local_server: Any,
hass_client: Any,
) -> None:
"""Test that a service call to create a URL returns the id."""
config_entry = create_mock_hass_web_proxy_config_entry(hass, TEST_OPTIONS)

await setup_mock_hass_web_proxy_config_entry(hass, config_entry)
await async_proxy_setup_entry(hass, config_entry)

service_response = await hass.services.async_call(
DOMAIN,
SERVICE_CREATE_PROXIED_URL,
{
**TEST_SERVICE_CALL_PARAMS,
CONF_URL_PATTERN: str(local_server),
},
blocking=True,
return_response=True,
)

assert isinstance(service_response.get("url_id"), str)

# Ensure the response is a valid UUID (will raise if not).
uuid.UUID(service_response["url_id"], version=4)


async def test_proxy_view_dynamic_url_delete(
hass: HomeAssistant,
local_server: Any,
Expand Down

0 comments on commit fe5997e

Please sign in to comment.