Home Assistant Unofficial Reference 2024.12.1
services.py
Go to the documentation of this file.
1 """Support for LaMetric time services."""
2 
3 from __future__ import annotations
4 
5 from demetriek import (
6  AlarmSound,
7  Chart,
8  Goal,
9  LaMetricError,
10  Model,
11  Notification,
12  NotificationIconType,
13  NotificationPriority,
14  NotificationSound,
15  Simple,
16  Sound,
17 )
18 import voluptuous as vol
19 
20 from homeassistant.const import CONF_DEVICE_ID, CONF_ICON
21 from homeassistant.core import HomeAssistant, ServiceCall, callback
22 from homeassistant.exceptions import HomeAssistantError
23 from homeassistant.helpers import config_validation as cv
24 
25 from .const import (
26  CONF_CYCLES,
27  CONF_DATA,
28  CONF_ICON_TYPE,
29  CONF_MESSAGE,
30  CONF_PRIORITY,
31  CONF_SOUND,
32  DOMAIN,
33  SERVICE_CHART,
34  SERVICE_MESSAGE,
35 )
36 from .coordinator import LaMetricDataUpdateCoordinator
37 from .helpers import async_get_coordinator_by_device_id
38 
39 SERVICE_BASE_SCHEMA = vol.Schema(
40  {
41  vol.Required(CONF_DEVICE_ID): cv.string,
42  vol.Optional(CONF_CYCLES, default=1): cv.positive_int,
43  vol.Optional(CONF_ICON_TYPE, default=NotificationIconType.NONE): vol.Coerce(
44  NotificationIconType
45  ),
46  vol.Optional(CONF_PRIORITY, default=NotificationPriority.INFO): vol.Coerce(
47  NotificationPriority
48  ),
49  vol.Optional(CONF_SOUND): vol.Any(
50  vol.Coerce(AlarmSound), vol.Coerce(NotificationSound)
51  ),
52  }
53 )
54 
55 SERVICE_MESSAGE_SCHEMA = SERVICE_BASE_SCHEMA.extend(
56  {
57  vol.Required(CONF_MESSAGE): cv.string,
58  vol.Optional(CONF_ICON): cv.string,
59  }
60 )
61 
62 SERVICE_CHART_SCHEMA = SERVICE_BASE_SCHEMA.extend(
63  {
64  vol.Required(CONF_DATA): vol.All(cv.ensure_list, [vol.Coerce(int)]),
65  }
66 )
67 
68 
69 @callback
70 def async_setup_services(hass: HomeAssistant) -> None:
71  """Set up services for the LaMetric integration."""
72 
73  async def _async_service_chart(call: ServiceCall) -> None:
74  """Send a chart to a LaMetric device."""
76  hass, call.data[CONF_DEVICE_ID]
77  )
79  coordinator, call, [Chart(data=call.data[CONF_DATA])]
80  )
81 
82  async def _async_service_message(call: ServiceCall) -> None:
83  """Send a message to a LaMetric device."""
85  hass, call.data[CONF_DEVICE_ID]
86  )
88  coordinator,
89  call,
90  [
91  Simple(
92  icon=call.data.get(CONF_ICON),
93  text=call.data[CONF_MESSAGE],
94  )
95  ],
96  )
97 
98  hass.services.async_register(
99  DOMAIN,
100  SERVICE_CHART,
101  _async_service_chart,
102  schema=SERVICE_CHART_SCHEMA,
103  )
104 
105  hass.services.async_register(
106  DOMAIN,
107  SERVICE_MESSAGE,
108  _async_service_message,
109  schema=SERVICE_MESSAGE_SCHEMA,
110  )
111 
112 
114  coordinator: LaMetricDataUpdateCoordinator,
115  call: ServiceCall,
116  frames: list[Chart | Goal | Simple],
117 ) -> None:
118  """Send a notification to an LaMetric device."""
119  sound = None
120  if CONF_SOUND in call.data:
121  sound = Sound(sound=call.data[CONF_SOUND], category=None)
122 
123  notification = Notification(
124  icon_type=NotificationIconType(call.data[CONF_ICON_TYPE]),
125  priority=NotificationPriority(call.data.get(CONF_PRIORITY)),
126  model=Model(
127  frames=frames,
128  cycles=call.data[CONF_CYCLES],
129  sound=sound,
130  ),
131  )
132 
133  try:
134  await coordinator.lametric.notify(notification=notification)
135  except LaMetricError as ex:
136  raise HomeAssistantError("Could not send LaMetric notification") from ex
LaMetricDataUpdateCoordinator async_get_coordinator_by_device_id(HomeAssistant hass, str device_id)
Definition: helpers.py:53
None async_send_notification(LaMetricDataUpdateCoordinator coordinator, ServiceCall call, list[Chart|Goal|Simple] frames)
Definition: services.py:117
None async_setup_services(HomeAssistant hass)
Definition: services.py:70