create_device_class_groups.yaml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. blueprint:
  2. name: Create device class groups by room and class itself
  3. description: >-
  4. A script that sends an actionable notification with a confirmation before
  5. running the specified action.
  6. domain: script
  7. input:
  8. areas:
  9. selector:
  10. object:
  11. default: []
  12. device_classes:
  13. selector:
  14. object:
  15. default: []
  16. mode: restart
  17. sequence:
  18. - variables:
  19. areas: !input areas
  20. device_classes: !input device_classes
  21. num_areas: >-
  22. {{ areas|length if areas is not string and areas is iterable else 0 }}
  23. num_device_classes: >-
  24. {{ device_classes|length if areas is not string and device_classes is iterable else 0 }}
  25. - service: system_log.write
  26. data:
  27. level: "warning"
  28. message: >-
  29. Creating groups for:
  30. - {{ device_classes | join("\n\n- ")}}
  31. and areas:
  32. - {{ areas | join("\n\n- ")}}
  33. - repeat:
  34. while: "{{ repeat.index <= num_device_classes }}"
  35. sequence:
  36. - variables:
  37. device_class_name: "{{ device_classes[ repeat.index - 1 ] }}"
  38. devices: >-
  39. {{ states|selectattr('attributes.device_class', 'eq', device_class_name)|map(attribute='entity_id')|list }}
  40. - choose:
  41. conditions: "{{ devices|length > 0 }}"
  42. sequence:
  43. # - service: system_log.write
  44. # data:
  45. # level: "warning"
  46. # message: "Creating «group.{{ device_class_name }}» for {{ devices|length }} entities: {{ devices }}"
  47. - service: group.set
  48. data:
  49. object_id: "{{ device_class_name }}"
  50. add_entities: "{{ devices }}"
  51. - repeat:
  52. while: "{{ repeat.index <= num_areas }}"
  53. sequence:
  54. - variables:
  55. area: "{{ areas[ repeat.index - 1 ] }}"
  56. area_group_name: "{{ area.replace(' ', '_')|lower + '_' + device_class_name }}"
  57. area_entities: >-
  58. {% set entities = namespace(list=[]) %}
  59. {% for entity in expand('group.' + device_class_name) if area_name(entity.entity_id) == area %}
  60. {% set entities.list = entities.list + [ entity.entity_id ] %}
  61. {% endfor %}
  62. {{ entities.list }}
  63. - choose:
  64. conditions: "{{ area_entities|length > 0 }}"
  65. sequence:
  66. # - service: system_log.write
  67. # data:
  68. # level: "warning"
  69. # message: >-
  70. # Creating «group.{{ area_group_name }}» with:
  71. # {{ area_entities | join("\n\n- ")}}
  72. - service: group.set
  73. data:
  74. object_id: "{{ area_group_name }}"
  75. add_entities: "{{ area_entities }}"