Home Assistant Unofficial Reference 2024.12.1
resources.py
Go to the documentation of this file.
1 """Alexa Resources and Assets."""
2 
3 from typing import Any
4 
5 
7  """The Global Alexa catalog.
8 
9  https://developer.amazon.com/docs/device-apis/resources-and-assets.html#global-alexa-catalog
10 
11  You can use the global Alexa catalog for pre-defined names of devices, settings,
12  values, and units.
13 
14  This catalog is localized into all the languages that Alexa supports.
15  You can reference the following catalog of pre-defined friendly names.
16 
17  Each item in the following list is an asset identifier followed by its
18  supported friendly names. The first friendly name for each identifier is
19  the one displayed in the Alexa mobile app.
20  """
21 
22  # Air Purifier, Air Cleaner,Clean Air Machine
23  DEVICE_NAME_AIR_PURIFIER = "Alexa.DeviceName.AirPurifier"
24 
25  # Fan, Blower
26  DEVICE_NAME_FAN = "Alexa.DeviceName.Fan"
27 
28  # Router, Internet Router, Network Router, Wifi Router, Net Router
29  DEVICE_NAME_ROUTER = "Alexa.DeviceName.Router"
30 
31  # Shade, Blind, Curtain, Roller, Shutter, Drape, Awning,
32  # Window shade, Interior blind
33  DEVICE_NAME_SHADE = "Alexa.DeviceName.Shade"
34 
35  # Shower
36  DEVICE_NAME_SHOWER = "Alexa.DeviceName.Shower"
37 
38  # Space Heater, Portable Heater
39  DEVICE_NAME_SPACE_HEATER = "Alexa.DeviceName.SpaceHeater"
40 
41  # Washer, Washing Machine
42  DEVICE_NAME_WASHER = "Alexa.DeviceName.Washer"
43 
44  # 2.4G Guest Wi-Fi, 2.4G Guest Network, Guest Network 2.4G, 2G Guest Wifi
45  SETTING_2G_GUEST_WIFI = "Alexa.Setting.2GGuestWiFi"
46 
47  # 5G Guest Wi-Fi, 5G Guest Network, Guest Network 5G, 5G Guest Wifi
48  SETTING_5G_GUEST_WIFI = "Alexa.Setting.5GGuestWiFi"
49 
50  # Auto, Automatic, Automatic Mode, Auto Mode
51  SETTING_AUTO = "Alexa.Setting.Auto"
52 
53  # Direction
54  SETTING_DIRECTION = "Alexa.Setting.Direction"
55 
56  # Dry Cycle, Dry Preset, Dry Setting, Dryer Cycle, Dryer Preset, Dryer Setting
57  SETTING_DRY_CYCLE = "Alexa.Setting.DryCycle"
58 
59  # Fan Speed, Airflow speed, Wind Speed, Air speed, Air velocity
60  SETTING_FAN_SPEED = "Alexa.Setting.FanSpeed"
61 
62  # Guest Wi-fi, Guest Network, Guest Net
63  SETTING_GUEST_WIFI = "Alexa.Setting.GuestWiFi"
64 
65  # Heat
66  SETTING_HEAT = "Alexa.Setting.Heat"
67 
68  # Mode
69  SETTING_MODE = "Alexa.Setting.Mode"
70 
71  # Night, Night Mode
72  SETTING_NIGHT = "Alexa.Setting.Night"
73 
74  # Opening, Height, Lift, Width
75  SETTING_OPENING = "Alexa.Setting.Opening"
76 
77  # Oscillate, Swivel, Oscillation, Spin, Back and forth
78  SETTING_OSCILLATE = "Alexa.Setting.Oscillate"
79 
80  # Preset, Setting
81  SETTING_PRESET = "Alexa.Setting.Preset"
82 
83  # Quiet, Quiet Mode, Noiseless, Silent
84  SETTING_QUIET = "Alexa.Setting.Quiet"
85 
86  # Temperature, Temp
87  SETTING_TEMPERATURE = "Alexa.Setting.Temperature"
88 
89  # Wash Cycle, Wash Preset, Wash setting
90  SETTING_WASH_CYCLE = "Alexa.Setting.WashCycle"
91 
92  # Water Temperature, Water Temp, Water Heat
93  SETTING_WATER_TEMPERATURE = "Alexa.Setting.WaterTemperature"
94 
95  # Handheld Shower, Shower Wand, Hand Shower
96  SHOWER_HAND_HELD = "Alexa.Shower.HandHeld"
97 
98  # Rain Head, Overhead shower, Rain Shower, Rain Spout, Rain Faucet
99  SHOWER_RAIN_HEAD = "Alexa.Shower.RainHead"
100 
101  # Degrees, Degree
102  UNIT_ANGLE_DEGREES = "Alexa.Unit.Angle.Degrees"
103 
104  # Radians, Radian
105  UNIT_ANGLE_RADIANS = "Alexa.Unit.Angle.Radians"
106 
107  # Feet, Foot
108  UNIT_DISTANCE_FEET = "Alexa.Unit.Distance.Feet"
109 
110  # Inches, Inch
111  UNIT_DISTANCE_INCHES = "Alexa.Unit.Distance.Inches"
112 
113  # Kilometers
114  UNIT_DISTANCE_KILOMETERS = "Alexa.Unit.Distance.Kilometers"
115 
116  # Meters, Meter, m
117  UNIT_DISTANCE_METERS = "Alexa.Unit.Distance.Meters"
118 
119  # Miles, Mile
120  UNIT_DISTANCE_MILES = "Alexa.Unit.Distance.Miles"
121 
122  # Yards, Yard
123  UNIT_DISTANCE_YARDS = "Alexa.Unit.Distance.Yards"
124 
125  # Grams, Gram, g
126  UNIT_MASS_GRAMS = "Alexa.Unit.Mass.Grams"
127 
128  # Kilograms, Kilogram, kg
129  UNIT_MASS_KILOGRAMS = "Alexa.Unit.Mass.Kilograms"
130 
131  # Percent
132  UNIT_PERCENT = "Alexa.Unit.Percent"
133 
134  # Celsius, Degrees Celsius, Degrees, C, Centigrade, Degrees Centigrade
135  UNIT_TEMPERATURE_CELSIUS = "Alexa.Unit.Temperature.Celsius"
136 
137  # Degrees, Degree
138  UNIT_TEMPERATURE_DEGREES = "Alexa.Unit.Temperature.Degrees"
139 
140  # Fahrenheit, Degrees Fahrenheit, Degrees F, Degrees, F
141  UNIT_TEMPERATURE_FAHRENHEIT = "Alexa.Unit.Temperature.Fahrenheit"
142 
143  # Kelvin, Degrees Kelvin, Degrees K, Degrees, K
144  UNIT_TEMPERATURE_KELVIN = "Alexa.Unit.Temperature.Kelvin"
145 
146  # Cubic Feet, Cubic Foot
147  UNIT_VOLUME_CUBIC_FEET = "Alexa.Unit.Volume.CubicFeet"
148 
149  # Cubic Meters, Cubic Meter, Meters Cubed
150  UNIT_VOLUME_CUBIC_METERS = "Alexa.Unit.Volume.CubicMeters"
151 
152  # Gallons, Gallon
153  UNIT_VOLUME_GALLONS = "Alexa.Unit.Volume.Gallons"
154 
155  # Liters, Liter, L
156  UNIT_VOLUME_LITERS = "Alexa.Unit.Volume.Liters"
157 
158  # Pints, Pint
159  UNIT_VOLUME_PINTS = "Alexa.Unit.Volume.Pints"
160 
161  # Quarts, Quart
162  UNIT_VOLUME_QUARTS = "Alexa.Unit.Volume.Quarts"
163 
164  # Ounces, Ounce, oz
165  UNIT_WEIGHT_OUNCES = "Alexa.Unit.Weight.Ounces"
166 
167  # Pounds, Pound, lbs
168  UNIT_WEIGHT_POUNDS = "Alexa.Unit.Weight.Pounds"
169 
170  # Close
171  VALUE_CLOSE = "Alexa.Value.Close"
172 
173  # Delicates, Delicate
174  VALUE_DELICATE = "Alexa.Value.Delicate"
175 
176  # High
177  VALUE_HIGH = "Alexa.Value.High"
178 
179  # Low
180  VALUE_LOW = "Alexa.Value.Low"
181 
182  # Maximum, Max
183  VALUE_MAXIMUM = "Alexa.Value.Maximum"
184 
185  # Medium, Mid
186  VALUE_MEDIUM = "Alexa.Value.Medium"
187 
188  # Minimum, Min
189  VALUE_MINIMUM = "Alexa.Value.Minimum"
190 
191  # Open
192  VALUE_OPEN = "Alexa.Value.Open"
193 
194  # Quick Wash, Fast Wash, Wash Quickly, Speed Wash
195  VALUE_QUICK_WASH = "Alexa.Value.QuickWash"
196 
197 
199  """Base class for Alexa capabilityResources, modeResources, and presetResources.
200 
201  Resources objects labels must be unique across all modeResources and
202  presetResources within the same device. To provide support for all
203  supported locales, include one label from the AlexaGlobalCatalog in the
204  labels array.
205 
206  You cannot use any words from the following list as friendly names:
207  https://developer.amazon.com/docs/alexa/device-apis/resources-and-assets.html#names-you-cannot-use
208 
209  https://developer.amazon.com/docs/device-apis/resources-and-assets.html#capability-resources
210  """
211 
212  def __init__(self, labels: list[str]) -> None:
213  """Initialize an Alexa resource."""
214  self._resource_labels_resource_labels = []
215  for label in labels:
216  self._resource_labels_resource_labels.append(label)
217 
218  def serialize_capability_resources(self) -> dict[str, list[dict[str, Any]]]:
219  """Return capabilityResources object serialized for an API response."""
220  return self.serialize_labelsserialize_labels(self._resource_labels_resource_labels)
221 
222  def serialize_configuration(self) -> dict[str, Any]:
223  """Return serialized configuration for an API response.
224 
225  Return ModeResources, PresetResources friendlyNames serialized.
226  """
227  raise NotImplementedError
228 
229  def serialize_labels(self, resources: list[str]) -> dict[str, list[dict[str, Any]]]:
230  """Return serialized labels for an API response.
231 
232  Returns resource label objects for friendlyNames serialized.
233  """
234  labels: list[dict[str, Any]] = []
235  label_dict: dict[str, Any]
236  for label in resources:
237  if label in AlexaGlobalCatalog.__dict__.values():
238  label_dict = {"@type": "asset", "value": {"assetId": label}}
239  else:
240  label_dict = {
241  "@type": "text",
242  "value": {"text": label, "locale": "en-US"},
243  }
244 
245  labels.append(label_dict)
246 
247  return {"friendlyNames": labels}
248 
249 
251  """Implements Alexa ModeResources.
252 
253  https://developer.amazon.com/docs/device-apis/resources-and-assets.html#capability-resources
254  """
255 
256  def __init__(self, labels: list[str], ordered: bool = False) -> None:
257  """Initialize an Alexa modeResource."""
258  super().__init__(labels)
259  self._supported_modes: list[dict[str, Any]] = []
260  self._mode_ordered: bool = ordered
261 
262  def add_mode(self, value: str, labels: list[str]) -> None:
263  """Add mode to the supportedModes object."""
264  self._supported_modes.append({"value": value, "labels": labels})
265 
266  def serialize_configuration(self) -> dict[str, Any]:
267  """Return serialized configuration for an API response.
268 
269  Returns configuration for ModeResources friendlyNames serialized.
270  """
271  mode_resources: list[dict[str, Any]] = []
272  for mode in self._supported_modes:
273  result = {
274  "value": mode["value"],
275  "modeResources": self.serialize_labelsserialize_labels(mode["labels"]),
276  }
277  mode_resources.append(result)
278 
279  return {"ordered": self._mode_ordered, "supportedModes": mode_resources}
280 
281 
283  """Implements Alexa PresetResources.
284 
285  Use presetResources with RangeController to provide a set of
286  friendlyNames for each RangeController preset.
287 
288  https://developer.amazon.com/docs/device-apis/resources-and-assets.html#presetresources
289  """
290 
291  def __init__(
292  self,
293  labels: list[str],
294  min_value: float,
295  max_value: float,
296  precision: float,
297  unit: str | None = None,
298  ) -> None:
299  """Initialize an Alexa presetResource."""
300  super().__init__(labels)
301  self._presets: list[dict[str, Any]] = []
302  self._minimum_value_minimum_value = min_value
303  self._maximum_value_maximum_value = max_value
304  self._precision_precision = precision
305  self._unit_of_measure_unit_of_measure = None
306  if unit in AlexaGlobalCatalog.__dict__.values():
307  self._unit_of_measure_unit_of_measure = unit
308 
309  def add_preset(self, value: float, labels: list[str]) -> None:
310  """Add preset to configuration presets array."""
311  self._presets.append({"value": value, "labels": labels})
312 
313  def serialize_configuration(self) -> dict[str, Any]:
314  """Return serialized configuration for an API response.
315 
316  Returns configuration for PresetResources friendlyNames serialized.
317  """
318  configuration: dict[str, Any] = {
319  "supportedRange": {
320  "minimumValue": self._minimum_value_minimum_value,
321  "maximumValue": self._maximum_value_maximum_value,
322  "precision": self._precision_precision,
323  }
324  }
325 
326  if self._unit_of_measure_unit_of_measure:
327  configuration["unitOfMeasure"] = self._unit_of_measure_unit_of_measure
328 
329  if self._presets:
330  preset_resources = [
331  {
332  "rangeValue": preset["value"],
333  "presetResources": self.serialize_labelsserialize_labels(preset["labels"]),
334  }
335  for preset in self._presets
336  ]
337  configuration["presets"] = preset_resources
338 
339  return configuration
340 
341 
343  """Class for Alexa Semantics Object.
344 
345  You can optionally enable additional utterances by using semantics. When
346  you use semantics, you manually map the phrases "open", "close", "raise",
347  and "lower" to directives.
348 
349  Semantics is supported for the following interfaces only: ModeController,
350  RangeController, and ToggleController.
351 
352  Semantics stateMappings are only supported for one interface of the same
353  type on the same device. If a device has multiple RangeControllers only
354  one interface may use stateMappings otherwise discovery will fail.
355 
356  You can support semantics actionMappings on different controllers for the
357  same device, however each controller must support different phrases.
358  For example, you can support "raise" on a RangeController, and "open"
359  on a ModeController, but you can't support "open" on both RangeController
360  and ModeController. Semantics stateMappings are only supported for one
361  interface on the same device.
362 
363  https://developer.amazon.com/docs/device-apis/alexa-discovery.html#semantics-object
364  """
365 
366  MAPPINGS_ACTION = "actionMappings"
367  MAPPINGS_STATE = "stateMappings"
368 
369  ACTIONS_TO_DIRECTIVE = "ActionsToDirective"
370  STATES_TO_VALUE = "StatesToValue"
371  STATES_TO_RANGE = "StatesToRange"
372 
373  ACTION_CLOSE = "Alexa.Actions.Close"
374  ACTION_LOWER = "Alexa.Actions.Lower"
375  ACTION_OPEN = "Alexa.Actions.Open"
376  ACTION_RAISE = "Alexa.Actions.Raise"
377 
378  STATES_OPEN = "Alexa.States.Open"
379  STATES_CLOSED = "Alexa.States.Closed"
380 
381  DIRECTIVE_RANGE_SET_VALUE = "SetRangeValue"
382  DIRECTIVE_RANGE_ADJUST_VALUE = "AdjustRangeValue"
383  DIRECTIVE_TOGGLE_TURN_ON = "TurnOn"
384  DIRECTIVE_TOGGLE_TURN_OFF = "TurnOff"
385  DIRECTIVE_MODE_SET_MODE = "SetMode"
386  DIRECTIVE_MODE_ADJUST_MODE = "AdjustMode"
387 
388  def __init__(self) -> None:
389  """Initialize an Alexa modeResource."""
390  self._action_mappings: list[dict[str, Any]] = []
391  self._state_mappings: list[dict[str, Any]] = []
392 
393  def _add_action_mapping(self, semantics: dict[str, Any]) -> None:
394  """Add action mapping between actions and interface directives."""
395  self._action_mappings.append(semantics)
396 
397  def _add_state_mapping(self, semantics: dict[str, Any]) -> None:
398  """Add state mapping between states and interface directives."""
399  self._state_mappings.append(semantics)
400 
401  def add_states_to_value(self, states: list[str], value: Any) -> None:
402  """Add StatesToValue stateMappings."""
403  self._add_state_mapping_add_state_mapping(
404  {"@type": self.STATES_TO_VALUESTATES_TO_VALUE, "states": states, "value": value}
405  )
406 
408  self, states: list[str], min_value: float, max_value: float
409  ) -> None:
410  """Add StatesToRange stateMappings."""
411  self._add_state_mapping_add_state_mapping(
412  {
413  "@type": self.STATES_TO_RANGESTATES_TO_RANGE,
414  "states": states,
415  "range": {"minimumValue": min_value, "maximumValue": max_value},
416  }
417  )
418 
420  self, actions: list[str], directive: str, payload: dict[str, Any]
421  ) -> None:
422  """Add ActionsToDirective actionMappings."""
423  self._add_action_mapping_add_action_mapping(
424  {
425  "@type": self.ACTIONS_TO_DIRECTIVEACTIONS_TO_DIRECTIVE,
426  "actions": actions,
427  "directive": {"name": directive, "payload": payload},
428  }
429  )
430 
431  def serialize_semantics(self) -> dict[str, Any]:
432  """Return semantics object serialized for an API response."""
433  semantics: dict[str, Any] = {}
434  if self._action_mappings:
435  semantics[self.MAPPINGS_ACTIONMAPPINGS_ACTION] = self._action_mappings
436  if self._state_mappings:
437  semantics[self.MAPPINGS_STATEMAPPINGS_STATE] = self._state_mappings
438 
439  return semantics
dict[str, list[dict[str, Any]]] serialize_capability_resources(self)
Definition: resources.py:218
dict[str, list[dict[str, Any]]] serialize_labels(self, list[str] resources)
Definition: resources.py:229
None add_mode(self, str value, list[str] labels)
Definition: resources.py:262
None __init__(self, list[str] labels, bool ordered=False)
Definition: resources.py:256
None __init__(self, list[str] labels, float min_value, float max_value, float precision, str|None unit=None)
Definition: resources.py:298
None add_preset(self, float value, list[str] labels)
Definition: resources.py:309
None _add_action_mapping(self, dict[str, Any] semantics)
Definition: resources.py:393
None add_states_to_value(self, list[str] states, Any value)
Definition: resources.py:401
None add_action_to_directive(self, list[str] actions, str directive, dict[str, Any] payload)
Definition: resources.py:421
None add_states_to_range(self, list[str] states, float min_value, float max_value)
Definition: resources.py:409
None _add_state_mapping(self, dict[str, Any] semantics)
Definition: resources.py:397