Calling Services
The API provides methods to invoke Home Assistant services.
Basic Service Calls
Use call_service for generic service invocations.
from hassette import App
class NotifyApp(App):
async def on_initialize(self):
await self.api.call_service(
domain="notify",
service="mobile_app_phone",
message="Hello from Hassette!",
priority="high",
)
Convenience Helpers
Common operations like turning entities on/off have dedicated helpers. They save you from specifying the domain and service name separately — turn_on("light.porch") is more readable than call_service("homeassistant", "turn_on", entity_id="light.porch") and less error-prone.
from hassette import App
class HelperApp(App):
async def on_initialize(self):
# Turn on with attributes
await self.api.turn_on("light.kitchen", brightness=255, color_name="blue")
# Turn off
await self.api.turn_off("switch.fan")
# Toggle (reverses current on/off state)
await self.api.toggle_service("light.bedroom")
These methods forward arguments to call_service while providing a cleaner syntax.
Service Responses
Service calls return a response dictionary (if the service provides one).
from hassette import App
class WeatherApp(App):
async def on_initialize(self):
response = await self.api.call_service("weather", "get_forecasts", target={"entity_id": "weather.home"}, return_response=True, type="daily")
print(response)
See Also
- Retrieving Entities & States - Get entity and state data
- Utilities & History - Templates, history, and advanced features
- Bus - Subscribe to service call events