Skip to content

Commit fa8b70b

Browse files
authored
Add missing __call__ methods to Service classes (#244)
* Add missing __call__methods to Service and AsyncService * Bump version to 6.0.1 * Lint * Clean up * Remove extra space * Add trailing comma
1 parent d6314de commit fa8b70b

5 files changed

Lines changed: 427 additions & 1 deletion

File tree

homeassistant_api/models/domains.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,18 @@ def trigger(
630630
**service_data,
631631
)
632632

633+
def __call__(
634+
self,
635+
**service_data: Any,
636+
) -> (
637+
tuple[State, ...]
638+
| tuple[tuple[State, ...], dict[str, Any]]
639+
| dict[str, Any]
640+
| None
641+
):
642+
"""Calls the service associated with this object."""
643+
return self.trigger(**service_data)
644+
633645

634646
class AsyncService(BaseService):
635647
"""Async service with async trigger method."""
@@ -658,3 +670,15 @@ async def trigger(
658670
self.service_id,
659671
**service_data,
660672
)
673+
674+
async def __call__(
675+
self,
676+
**service_data: Any,
677+
) -> (
678+
tuple[State, ...]
679+
| tuple[tuple[State, ...], dict[str, Any]]
680+
| dict[str, Any]
681+
| None
682+
):
683+
"""Calls the service associated with this object."""
684+
return await self.trigger(**service_data)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ requires = ["hatchling"]
44

55
[project]
66
name = "HomeAssistant-API"
7-
version = "6.0.0"
7+
version = "6.0.1"
88
description = "Python Wrapper for Homeassistant's REST API"
99
readme = "README.md"
1010
license = "GPL-3.0-or-later"

tests/cassettes/test_endpoints/test_async_call_service.json

Lines changed: 190 additions & 0 deletions
Large diffs are not rendered by default.

tests/cassettes/test_endpoints/test_call_service.json

Lines changed: 190 additions & 0 deletions
Large diffs are not rendered by default.

tests/test_endpoints.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,17 @@ def test_trigger_service(cached_client: Client) -> None:
499499
logger.info(resp)
500500
assert isinstance(resp, tuple)
501501

502+
def test_call_service(cached_client: Client) -> None:
503+
"""Tests the `POST /api/services/<domain>/<service>` endpoint."""
504+
notify = cached_client.get_domain("notify")
505+
assert notify is not None
506+
resp = notify.persistent_notification(
507+
message="Your API Test Suite just said hello!",
508+
title="Test Suite Notifcation",
509+
)
510+
logger.info(resp)
511+
assert isinstance(resp, tuple)
512+
502513

503514
async def test_async_trigger_service(async_cached_client: AsyncClient) -> None:
504515
"""Tests the `POST /api/services/<domain>/<service>` endpoint."""
@@ -511,6 +522,17 @@ async def test_async_trigger_service(async_cached_client: AsyncClient) -> None:
511522
assert isinstance(resp, tuple)
512523

513524

525+
async def test_async_call_service(async_cached_client: AsyncClient) -> None:
526+
"""Tests the `POST /api/services/<domain>/<service>` endpoint."""
527+
notify = await async_cached_client.get_domain("notify")
528+
assert notify is not None
529+
resp = await notify.persistent_notification(
530+
message="Your API Test Suite just said hello!",
531+
title="Test Suite Notifcation (Async)",
532+
)
533+
assert isinstance(resp, tuple)
534+
535+
514536
def test_websocket_trigger_service(websocket_client: WebsocketClient) -> None:
515537
"""Tests the `"type": "trigger_service"` websocket command."""
516538
notify = websocket_client.get_domain("notify")

0 commit comments

Comments
 (0)