Skip to content

Commit 05ea24f

Browse files
authored
Merge pull request #2242 from AppDaemon/template_functions
Template functions
2 parents 472c591 + 7c14aad commit 05ea24f

File tree

1 file changed

+140
-26
lines changed

1 file changed

+140
-26
lines changed

appdaemon/plugins/hass/hassapi.py

Lines changed: 140 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,10 @@ def run_script(
12691269

12701270
@utils.sync_decorator
12711271
async def render_template(self, template: str, namespace: str | None = None) -> Any:
1272-
"""Renders a Home Assistant Template
1272+
"""Renders a Home Assistant Template.
12731273
1274-
https://www.home-assistant.io/docs/configuration/templating
1275-
https://www.home-assistant.io/integrations/template
1274+
See the documentation for the `Template Integration <https://www.home-assistant.io/integrations/template>`__ and
1275+
`Templating Configuration <https://www.home-assistant.io/docs/configuration/templating>`__ for more information.
12761276
12771277
Args:
12781278
template (str): The Home Assistant template to be rendered.
@@ -1303,43 +1303,157 @@ async def render_template(self, template: str, namespace: str | None = None) ->
13031303
except (SyntaxError, ValueError):
13041304
return result
13051305

1306-
# Device IDs
1306+
def _template_command(self, command: str, *args: tuple[str, ...]) -> str | list[str]:
1307+
"""Internal AppDaemon function to format calling a single template command correctly."""
1308+
if len(args) == 0:
1309+
return self.render_template(f'{{{{ {command}() }}}}')
1310+
else:
1311+
assert all(isinstance(i, str) for i in args), f"All inputs must be strings, got {args}"
1312+
arg_str = ', '.join(f"'{i}'" for i in args)
1313+
cmd_str = f'{{{{ {command}({arg_str}) }}}}'
1314+
self.logger.debug("Template command: %s", cmd_str)
1315+
return self.render_template(cmd_str)
13071316

1308-
@utils.sync_decorator
1309-
async def get_device_id(self, entity_id: str) -> str:
1310-
"""Uses the ``device_id`` function in a template to get the device ID"""
1311-
return await self.render_template(f'{{{{device_id("{entity_id}")}}}}')
1317+
# Devices
1318+
# https://www.home-assistant.io/docs/configuration/templating/#devices
13121319

1313-
@utils.sync_decorator
1314-
async def get_device_entities(self, device_id: str) -> list[str]:
1315-
"""Uses the ``device_entities`` function in a template to get entities
1316-
associated with a device.
1320+
def device_entities(self, device_id: str) -> list[str]:
1321+
"""Get a list of entities that are associated with a given device ID.
1322+
1323+
See `device functions <https://www.home-assistant.io/docs/configuration/templating/#devices>`_ for more
1324+
information.
1325+
"""
1326+
return self._template_command('device_entities', device_id)
1327+
1328+
def device_attr(self, device_or_entity_id: str, attr_name: str) -> str:
1329+
"""Get the value of attr_name for the given device or entity ID.
1330+
1331+
See `device functions <https://www.home-assistant.io/docs/configuration/templating/#devices>`_ for more
1332+
information.
1333+
1334+
Attributes vary by device , but some common device attributes include:
1335+
- ``area_id``
1336+
- ``configuration_url``
1337+
- ``manufacturer``
1338+
- ``model``
1339+
- ``name_by_user``
1340+
- ``name``
1341+
- ``sw_version``
1342+
"""
1343+
return self._template_command('device_attr', device_or_entity_id, attr_name)
1344+
1345+
def is_device_attr(self, device_or_entity_id: str, attr_name: str, attr_value: str | int | float) -> bool:
1346+
"""Get returns whether the value of attr_name for the given device or entity ID matches attr_value.
1347+
1348+
See `device functions <https://www.home-assistant.io/docs/configuration/templating/#devices>`_ for more
1349+
information.
1350+
"""
1351+
return self._template_command('is_device_attr', device_or_entity_id, attr_name, attr_value)
1352+
1353+
def device_id(self, entity_id: str) -> str:
1354+
"""Get the device ID for a given entity ID or device name.
1355+
1356+
See `device functions <https://www.home-assistant.io/docs/configuration/templating/#devices>`_ for more
1357+
information.
1358+
"""
1359+
return self._template_command('device_id', entity_id)
1360+
1361+
# Areas
1362+
# https://www.home-assistant.io/docs/configuration/templating/#areas
1363+
1364+
def areas(self) -> list[str]:
1365+
"""Get the full list of area IDs.
1366+
1367+
See `area functions <https://www.home-assistant.io/docs/configuration/templating/#areas>`_ for more information.
1368+
"""
1369+
return self._template_command('areas')
1370+
1371+
def area_id(self, lookup_value: str) -> str:
1372+
"""Get the area ID for a given device ID, entity ID, or area name.
1373+
1374+
See `area functions <https://www.home-assistant.io/docs/configuration/templating/#areas>`_ for more information.
1375+
"""
1376+
return self._template_command('area_id', lookup_value)
1377+
1378+
def area_name(self, lookup_value: str) -> str:
1379+
"""Get the area name for a given device ID, entity ID, or area ID.
1380+
1381+
See `area functions <https://www.home-assistant.io/docs/configuration/templating/#areas>`_ for more information.
1382+
"""
1383+
return self._template_command('area_name', lookup_value)
1384+
1385+
def area_entities(self, area_name_or_id: str) -> list[str]:
1386+
"""Get the list of entity IDs tied to a given area ID or name.
1387+
1388+
See `area functions <https://www.home-assistant.io/docs/configuration/templating/#areas>`_ for more information.
1389+
"""
1390+
return self._template_command('area_entities', area_name_or_id)
1391+
1392+
def area_devices(self, area_name_or_id: str) -> list[str]:
1393+
"""Get the list of device IDs tied to a given area ID or name.
1394+
1395+
See `area functions <https://www.home-assistant.io/docs/configuration/templating/#areas>`_ for more information.
1396+
"""
1397+
return self._template_command('area_devices', area_name_or_id)
1398+
1399+
# Entities for an Integration
1400+
# https://www.home-assistant.io/docs/configuration/templating/#entities-for-an-integration
1401+
1402+
def integration_entities(self, integration: str) -> list[str]:
1403+
"""Get a list of entities that are associated with a given integration, such as ``hue`` or ``zwave_js``.
1404+
1405+
See `entities for an integration
1406+
<https://www.home-assistant.io/docs/configuration/templating/#entities-for-an-integration>`_ for more
1407+
information.
13171408
"""
1318-
return await self.render_template(f'{{{{device_entities("{device_id}")}}}}')
13191409

13201410
# Labels
13211411
# https://www.home-assistant.io/docs/configuration/templating/#labels
13221412

1323-
def _label_command(self, command: str, input: str) -> str | list[str]:
1324-
return self.render_template(f'{{{{ {command}("{input}") }}}}')
1325-
13261413
def labels(self, input: str = None) -> list[str]:
1327-
if input is None:
1328-
return self.render_template('{{ labels() }}')
1329-
else:
1330-
return self._label_command('labels', input)
1414+
"""Get the full list of label IDs, or those for a given area ID, device ID, or entity ID.
1415+
1416+
See `label functions <https://www.home-assistant.io/docs/configuration/templating/#labels>`_ for more
1417+
information.
1418+
"""
1419+
return self._template_command('labels', input)
13311420

13321421
def label_id(self, lookup_value: str) -> str:
1333-
return self._label_command('label_id', lookup_value)
1422+
"""Get the label ID for a given label name.
1423+
1424+
See `label functions <https://www.home-assistant.io/docs/configuration/templating/#labels>`_ for more
1425+
information.
1426+
"""
1427+
return self._template_command('label_id', lookup_value)
13341428

1335-
def label_name(self, lookup_value: str):
1336-
return self._label_command('label_name', lookup_value)
1429+
def label_name(self, lookup_value: str) -> str:
1430+
"""Get the label name for a given label ID.
1431+
1432+
See `label functions <https://www.home-assistant.io/docs/configuration/templating/#labels>`_ for more
1433+
information.
1434+
"""
1435+
return self._template_command('label_name', lookup_value)
13371436

13381437
def label_areas(self, label_name_or_id: str) -> list[str]:
1339-
return self._label_command('label_areas', label_name_or_id)
1438+
"""Get the list of area IDs tied to a given label ID or name.
1439+
1440+
See `label functions <https://www.home-assistant.io/docs/configuration/templating/#labels>`_ for more
1441+
information.
1442+
"""
1443+
return self._template_command('label_areas', label_name_or_id)
13401444

13411445
def label_devices(self, label_name_or_id: str) -> list[str]:
1342-
return self._label_command('label_devices', label_name_or_id)
1446+
"""Get the list of device IDs tied to a given label ID or name.
1447+
1448+
See `label functions <https://www.home-assistant.io/docs/configuration/templating/#labels>`_ for more
1449+
information.
1450+
"""
1451+
return self._template_command('label_devices', label_name_or_id)
13431452

13441453
def label_entities(self, label_name_or_id: str) -> list[str]:
1345-
return self._label_command('label_entities', label_name_or_id)
1454+
"""Get the list of entity IDs tied to a given label ID or name.
1455+
1456+
See `label functions <https://www.home-assistant.io/docs/configuration/templating/#labels>`_ for more
1457+
information.
1458+
"""
1459+
return self._template_command('label_entities', label_name_or_id)

0 commit comments

Comments
 (0)