Home Assistant Unofficial Reference 2024.12.1
services.py
Go to the documentation of this file.
1 """Services for the Tado integration."""
2 
3 import logging
4 
5 import voluptuous as vol
6 
7 from homeassistant.core import HomeAssistant, ServiceCall, callback
8 from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
9 from homeassistant.helpers import selector
10 
11 from .const import (
12  ATTR_MESSAGE,
13  CONF_CONFIG_ENTRY,
14  CONF_READING,
15  DOMAIN,
16  SERVICE_ADD_METER_READING,
17 )
18 
19 _LOGGER = logging.getLogger(__name__)
20 SCHEMA_ADD_METER_READING = vol.Schema(
21  {
22  vol.Required(CONF_CONFIG_ENTRY): selector.ConfigEntrySelector(
23  {
24  "integration": DOMAIN,
25  }
26  ),
27  vol.Required(CONF_READING): vol.Coerce(int),
28  }
29 )
30 
31 
32 @callback
33 def setup_services(hass: HomeAssistant) -> None:
34  """Set up the services for the Tado integration."""
35 
36  async def add_meter_reading(call: ServiceCall) -> None:
37  """Send meter reading to Tado."""
38  entry_id: str = call.data[CONF_CONFIG_ENTRY]
39  reading: int = call.data[CONF_READING]
40  _LOGGER.debug("Add meter reading %s", reading)
41 
42  entry = hass.config_entries.async_get_entry(entry_id)
43  if entry is None:
44  raise ServiceValidationError("Config entry not found")
45 
46  tadoconnector = entry.runtime_data
47 
48  response: dict = await hass.async_add_executor_job(
49  tadoconnector.set_meter_reading, call.data[CONF_READING]
50  )
51 
52  if ATTR_MESSAGE in response:
53  raise HomeAssistantError(response[ATTR_MESSAGE])
54 
55  hass.services.async_register(
56  DOMAIN, SERVICE_ADD_METER_READING, add_meter_reading, SCHEMA_ADD_METER_READING
57  )
None setup_services(HomeAssistant hass)
Definition: services.py:33