@@ -1269,10 +1269,10 @@ def run_script(
1269
1269
1270
1270
@utils .sync_decorator
1271
1271
async def render_template (self , template : str , namespace : str | None = None ) -> Any :
1272
- """Renders a Home Assistant Template
1272
+ """Renders a Home Assistant Template.
1273
1273
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.
1276
1276
1277
1277
Args:
1278
1278
template (str): The Home Assistant template to be rendered.
@@ -1303,43 +1303,157 @@ async def render_template(self, template: str, namespace: str | None = None) ->
1303
1303
except (SyntaxError , ValueError ):
1304
1304
return result
1305
1305
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 )
1307
1316
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
1312
1319
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.
1317
1408
"""
1318
- return await self .render_template (f'{{{{device_entities("{ device_id } ")}}}}' )
1319
1409
1320
1410
# Labels
1321
1411
# https://www.home-assistant.io/docs/configuration/templating/#labels
1322
1412
1323
- def _label_command (self , command : str , input : str ) -> str | list [str ]:
1324
- return self .render_template (f'{{{{ { command } ("{ input } ") }}}}' )
1325
-
1326
1413
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 )
1331
1420
1332
1421
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 )
1334
1428
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 )
1337
1436
1338
1437
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 )
1340
1444
1341
1445
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 )
1343
1452
1344
1453
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