#:########################################################################################:#
# Home Assistant Summary Package - V2                                                    #
#:########################################################################################:#
#
# TITLE:
#   Home Assistant Summary V2
#
# FILE:
#   packages/ha_summary.yaml
#
# VERSION:
#   V2.1 2026-07-20
#
# VERSION HISTORY:
#   V2.1 2026-07-20
#     - Added structured documentation for refresh behavior and counting rules.
#     - Documented heuristic classifications and their limitations.
#
#   V2.0
#     - Introduced V2 entity IDs to avoid stale entities from earlier versions.
#
# PURPOSE:
#   Provides dashboard summary sensors for Home Assistant entities, devices,
#   areas, cameras, scenes, scripts, automations, add-ons, and updates.
#
#   It also provides device and entity counts for ESPHome, Tasmota, ZHA,
#   Zigbee2MQTT, Matter, and selected cloud integrations.
#
# OUTPUTS:
#   Dashboard entity prefix:
#     sensor.ha_summary_v2_...
#
# REFRESH:
#   Sensors refresh when Home Assistant starts, when the entity, device, or area
#   registries change, when core configuration changes, and every five minutes.
#
# COUNTING NOTES:
#   - Total devices counts distinct device IDs associated with current entities.
#     Devices without entities are not included.
#   - Broken entities are entities whose state is unknown or unavailable.
#   - Add-ons are inferred from Supervisor update entities, excluding the Core,
#     Supervisor, and Operating System update entities.
#   - Zigbee2MQTT devices and entities are identified by searching MQTT device
#     metadata for "zigbee2mqtt".
#   - Matter Wi-Fi and Thread classifications search device metadata for known
#     transport terms. These are heuristics rather than authoritative transport
#     data, and a device can match both categories.
#   - Matter unclassified counts subtract Wi-Fi and Thread counts from totals, so
#     overlapping classifications can affect the result.
#   - Cloud counts use the explicit integration list in the final two sensors.
#     Add or remove integration domains there when maintaining cloud coverage.
#

template:
  - trigger:
      - platform: event
        event_type: homeassistant_started
      - platform: event
        event_type: entity_registry_updated
      - platform: event
        event_type: device_registry_updated
      - platform: event
        event_type: area_registry_updated
      - platform: event
        event_type: core_config_updated
      - platform: time_pattern
        minutes: "/5"

    sensor:
      #:####################################################################################:#
      # Core summary
      #:####################################################################################:#

      - name: HA Summary V2 - devices total
        unique_id: ha_summary_v2_devices_total
        default_entity_id: sensor.ha_summary_v2_devices_total
        unit_of_measurement: "devices"
        state: >-
          {% set ids = namespace(v=[]) %}
          {% for s in states %}
            {% set did = device_id(s.entity_id) %}
            {% if did and did not in ids.v %}
              {% set ids.v = ids.v + [did] %}
            {% endif %}
          {% endfor %}
          {{ ids.v | length }}

      - name: HA Summary V2 - entities total
        unique_id: ha_summary_v2_entities_total
        default_entity_id: sensor.ha_summary_v2_entities_total
        unit_of_measurement: "entities"
        state: "{{ states | list | length }}"

      - name: HA Summary V2 - entities broken
        unique_id: ha_summary_v2_entities_broken
        default_entity_id: sensor.ha_summary_v2_entities_broken
        unit_of_measurement: "entities"
        state: >-
          {{ states | selectattr('state','in',['unavailable','unknown']) | list | length }}

      - name: HA Summary V2 - cameras
        unique_id: ha_summary_v2_cameras
        default_entity_id: sensor.ha_summary_v2_cameras
        unit_of_measurement: "cameras"
        state: "{{ states.camera | list | length }}"

      - name: HA Summary V2 - areas
        unique_id: ha_summary_v2_areas
        default_entity_id: sensor.ha_summary_v2_areas
        unit_of_measurement: "areas"
        state: "{{ areas() | list | length }}"

      - name: HA Summary V2 - scenes
        unique_id: ha_summary_v2_scenes
        default_entity_id: sensor.ha_summary_v2_scenes
        state: "{{ states.scene | list | length }}"

      - name: HA Summary V2 - scripts
        unique_id: ha_summary_v2_scripts
        default_entity_id: sensor.ha_summary_v2_scripts
        state: "{{ states.script | list | length }}"

      - name: HA Summary V2 - automations
        unique_id: ha_summary_v2_automations
        default_entity_id: sensor.ha_summary_v2_automations
        state: "{{ states.automation | list | length }}"

      #:####################################################################################:#
      # Add-ons and updates
      #:####################################################################################:#

      - name: HA Summary V2 - addons
        unique_id: ha_summary_v2_addons
        default_entity_id: sensor.ha_summary_v2_addons
        icon: mdi:puzzle
        state: >-
          {% set hassio_entities = integration_entities('hassio') | list %}
          {% set skip = [
            'update.home_assistant_core_update',
            'update.home_assistant_supervisor_update',
            'update.home_assistant_operating_system_update'
          ] %}
          {% set count = namespace(v=0) %}
          {% for u in states.update %}
            {% if u.entity_id in hassio_entities and u.entity_id not in skip %}
              {% set count.v = count.v + 1 %}
            {% endif %}
          {% endfor %}
          {{ count.v }}

      - name: HA Summary V2 - updates available
        unique_id: ha_summary_v2_updates_available
        default_entity_id: sensor.ha_summary_v2_updates_available
        icon: mdi:download
        state: >-
          {% set count = namespace(v=0) %}
          {% for u in states.update %}
            {% if u.state == 'on' and not (u.attributes.in_progress | default(false)) %}
              {% set count.v = count.v + 1 %}
            {% endif %}
          {% endfor %}
          {{ count.v }}

      #:####################################################################################:#
      # ESPHome
      #:####################################################################################:#

      - name: HA Summary V2 - esphome devices
        unique_id: ha_summary_v2_esphome_devices
        default_entity_id: sensor.ha_summary_v2_esphome_devices
        unit_of_measurement: "devices"
        state: >-
          {% set ids = namespace(v=[]) %}
          {% for e in integration_entities('esphome') %}
            {% set did = device_id(e) %}
            {% if did and did not in ids.v %}
              {% set ids.v = ids.v + [did] %}
            {% endif %}
          {% endfor %}
          {{ ids.v | length }}

      - name: HA Summary V2 - esphome entities
        unique_id: ha_summary_v2_esphome_entities
        default_entity_id: sensor.ha_summary_v2_esphome_entities
        unit_of_measurement: "entities"
        state: "{{ integration_entities('esphome') | list | length }}"

      #:####################################################################################:#
      # Tasmota
      #:####################################################################################:#

      - name: HA Summary V2 - tasmota devices
        unique_id: ha_summary_v2_tasmota_devices
        default_entity_id: sensor.ha_summary_v2_tasmota_devices
        unit_of_measurement: "devices"
        state: >-
          {% set ids = namespace(v=[]) %}
          {% for e in integration_entities('tasmota') %}
            {% set did = device_id(e) %}
            {% if did and did not in ids.v %}
              {% set ids.v = ids.v + [did] %}
            {% endif %}
          {% endfor %}
          {{ ids.v | length }}

      - name: HA Summary V2 - tasmota entities
        unique_id: ha_summary_v2_tasmota_entities
        default_entity_id: sensor.ha_summary_v2_tasmota_entities
        unit_of_measurement: "entities"
        state: "{{ integration_entities('tasmota') | list | length }}"

      #:####################################################################################:#
      # Zigbee via ZHA
      #:####################################################################################:#

      - name: HA Summary V2 - zigbee zha devices
        unique_id: ha_summary_v2_zigbee_zha_devices
        default_entity_id: sensor.ha_summary_v2_zigbee_zha_devices
        unit_of_measurement: "devices"
        state: >-
          {% set ids = namespace(v=[]) %}
          {% for e in integration_entities('zha') | list %}
            {% set did = device_id(e) %}
            {% if did and did not in ids.v %}
              {% set ids.v = ids.v + [did] %}
            {% endif %}
          {% endfor %}
          {{ ids.v | length }}

      - name: HA Summary V2 - zigbee zha entities
        unique_id: ha_summary_v2_zigbee_zha_entities
        default_entity_id: sensor.ha_summary_v2_zigbee_zha_entities
        unit_of_measurement: "entities"
        state: "{{ integration_entities('zha') | list | length }}"

      #:####################################################################################:#
      # Zigbee via Zigbee2MQTT
      #:####################################################################################:#

      - name: HA Summary V2 - zigbee z2m devices
        unique_id: ha_summary_v2_zigbee_z2m_devices
        default_entity_id: sensor.ha_summary_v2_zigbee_z2m_devices
        unit_of_measurement: "devices"
        state: >-
          {% set dids = namespace(v=[]) %}
          {% for e in integration_entities('mqtt') | list %}
            {% set did = device_id(e) %}
            {% if did and did not in dids.v %}
              {% set txt = (
                ((device_attr(did, 'identifiers') or []) | string) ~ ' ' ~
                ((device_attr(did, 'connections') or []) | string) ~ ' ' ~
                ((device_attr(did, 'name') or '') | string) ~ ' ' ~
                ((device_attr(did, 'model') or '') | string) ~ ' ' ~
                ((device_attr(did, 'manufacturer') or '') | string)
              ) | lower %}
              {% if 'zigbee2mqtt' in txt %}
                {% set dids.v = dids.v + [did] %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ dids.v | length }}

      - name: HA Summary V2 - zigbee z2m entities
        unique_id: ha_summary_v2_zigbee_z2m_entities
        default_entity_id: sensor.ha_summary_v2_zigbee_z2m_entities
        unit_of_measurement: "entities"
        state: >-
          {% set ent = namespace(v=[]) %}
          {% for e in integration_entities('mqtt') | list %}
            {% set did = device_id(e) %}
            {% if did %}
              {% set txt = (
                ((device_attr(did, 'identifiers') or []) | string) ~ ' ' ~
                ((device_attr(did, 'connections') or []) | string) ~ ' ' ~
                ((device_attr(did, 'name') or '') | string) ~ ' ' ~
                ((device_attr(did, 'model') or '') | string) ~ ' ' ~
                ((device_attr(did, 'manufacturer') or '') | string)
              ) | lower %}
              {% if 'zigbee2mqtt' in txt %}
                {% set ent.v = ent.v + [e] %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ ent.v | unique | list | length }}

      #:####################################################################################:#
      # Matter
      #:####################################################################################:#

      - name: HA Summary V2 - matter devices
        unique_id: ha_summary_v2_matter_devices
        default_entity_id: sensor.ha_summary_v2_matter_devices
        unit_of_measurement: "devices"
        state: >-
          {% set ids = namespace(v=[]) %}
          {% for e in integration_entities('matter') %}
            {% set did = device_id(e) %}
            {% if did and did not in ids.v %}
              {% set ids.v = ids.v + [did] %}
            {% endif %}
          {% endfor %}
          {{ ids.v | length }}

      - name: HA Summary V2 - matter entities
        unique_id: ha_summary_v2_matter_entities
        default_entity_id: sensor.ha_summary_v2_matter_entities
        unit_of_measurement: "entities"
        state: "{{ integration_entities('matter') | list | length }}"

      - name: HA Summary V2 - matter wifi devices
        unique_id: ha_summary_v2_matter_wifi_devices
        default_entity_id: sensor.ha_summary_v2_matter_wifi_devices
        unit_of_measurement: "devices"
        state: >-
          {% set dids = namespace(v=[]) %}
          {% for e in integration_entities('matter') %}
            {% set did = device_id(e) %}
            {% if did and did not in dids.v %}
              {% set txt = (
                ((device_attr(did, 'identifiers') or []) | string) ~ ' ' ~
                ((device_attr(did, 'connections') or []) | string) ~ ' ' ~
                ((device_attr(did, 'name') or '') | string) ~ ' ' ~
                ((device_attr(did, 'model') or '') | string) ~ ' ' ~
                ((device_attr(did, 'manufacturer') or '') | string)
              ) | lower %}
              {% if 'wifi' in txt or 'wi-fi' in txt or 'wlan' in txt or 'ethernet' in txt %}
                {% set dids.v = dids.v + [did] %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ dids.v | length }}

      - name: HA Summary V2 - matter wifi entities
        unique_id: ha_summary_v2_matter_wifi_entities
        default_entity_id: sensor.ha_summary_v2_matter_wifi_entities
        unit_of_measurement: "entities"
        state: >-
          {% set ent = namespace(v=[]) %}
          {% for e in integration_entities('matter') %}
            {% set did = device_id(e) %}
            {% if did %}
              {% set txt = (
                ((device_attr(did, 'identifiers') or []) | string) ~ ' ' ~
                ((device_attr(did, 'connections') or []) | string) ~ ' ' ~
                ((device_attr(did, 'name') or '') | string) ~ ' ' ~
                ((device_attr(did, 'model') or '') | string) ~ ' ' ~
                ((device_attr(did, 'manufacturer') or '') | string)
              ) | lower %}
              {% if 'wifi' in txt or 'wi-fi' in txt or 'wlan' in txt or 'ethernet' in txt %}
                {% set ent.v = ent.v + [e] %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ ent.v | unique | list | length }}

      - name: HA Summary V2 - matter thread devices
        unique_id: ha_summary_v2_matter_thread_devices
        default_entity_id: sensor.ha_summary_v2_matter_thread_devices
        unit_of_measurement: "devices"
        state: >-
          {% set dids = namespace(v=[]) %}
          {% for e in integration_entities('matter') %}
            {% set did = device_id(e) %}
            {% if did and did not in dids.v %}
              {% set txt = (
                ((device_attr(did, 'identifiers') or []) | string) ~ ' ' ~
                ((device_attr(did, 'connections') or []) | string) ~ ' ' ~
                ((device_attr(did, 'name') or '') | string) ~ ' ' ~
                ((device_attr(did, 'model') or '') | string) ~ ' ' ~
                ((device_attr(did, 'manufacturer') or '') | string)
              ) | lower %}
              {% if 'thread' in txt or 'openthread' in txt or 'otbr' in txt or 'border router' in txt or 'border-router' in txt or '802.15.4' in txt %}
                {% set dids.v = dids.v + [did] %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ dids.v | length }}

      - name: HA Summary V2 - matter thread entities
        unique_id: ha_summary_v2_matter_thread_entities
        default_entity_id: sensor.ha_summary_v2_matter_thread_entities
        unit_of_measurement: "entities"
        state: >-
          {% set ent = namespace(v=[]) %}
          {% for e in integration_entities('matter') %}
            {% set did = device_id(e) %}
            {% if did %}
              {% set txt = (
                ((device_attr(did, 'identifiers') or []) | string) ~ ' ' ~
                ((device_attr(did, 'connections') or []) | string) ~ ' ' ~
                ((device_attr(did, 'name') or '') | string) ~ ' ' ~
                ((device_attr(did, 'model') or '') | string) ~ ' ' ~
                ((device_attr(did, 'manufacturer') or '') | string)
              ) | lower %}
              {% if 'thread' in txt or 'openthread' in txt or 'otbr' in txt or 'border router' in txt or 'border-router' in txt or '802.15.4' in txt %}
                {% set ent.v = ent.v + [e] %}
              {% endif %}
            {% endif %}
          {% endfor %}
          {{ ent.v | unique | list | length }}

      - name: HA Summary V2 - matter unclassified devices
        unique_id: ha_summary_v2_matter_unclassified_devices
        default_entity_id: sensor.ha_summary_v2_matter_unclassified_devices
        unit_of_measurement: "devices"
        state: >-
          {% set total = states('sensor.ha_summary_v2_matter_devices') | int(0) %}
          {% set wifi = states('sensor.ha_summary_v2_matter_wifi_devices') | int(0) %}
          {% set thread = states('sensor.ha_summary_v2_matter_thread_devices') | int(0) %}
          {{ [total - wifi - thread, 0] | max }}

      - name: HA Summary V2 - matter unclassified entities
        unique_id: ha_summary_v2_matter_unclassified_entities
        default_entity_id: sensor.ha_summary_v2_matter_unclassified_entities
        unit_of_measurement: "entities"
        state: >-
          {% set total = states('sensor.ha_summary_v2_matter_entities') | int(0) %}
          {% set wifi = states('sensor.ha_summary_v2_matter_wifi_entities') | int(0) %}
          {% set thread = states('sensor.ha_summary_v2_matter_thread_entities') | int(0) %}
          {{ [total - wifi - thread, 0] | max }}

      #:####################################################################################:#
      # Cloud devices/entities
      #:####################################################################################:#

      - name: HA Summary V2 - cloud devices
        unique_id: ha_summary_v2_cloud_devices
        default_entity_id: sensor.ha_summary_v2_cloud_devices
        unit_of_measurement: "devices"
        state: >-
          {% set dids = namespace(v=[]) %}
          {% for e in (
             integration_entities('tuya') +
             integration_entities('ewelink') +
             integration_entities('meross_cloud') +
             integration_entities('tapo') +
             integration_entities('tplink') +
             integration_entities('nest') +
             integration_entities('ring') +
             integration_entities('arlo') +
             integration_entities('wyze') +
             integration_entities('myq') +
             integration_entities('nuki') +
             integration_entities('august') +
             integration_entities('switchbot') +
             integration_entities('icloud')
          ) | unique %}
            {% set did = device_id(e) %}
            {% if did and did not in dids.v %}
              {% set dids.v = dids.v + [did] %}
            {% endif %}
          {% endfor %}
          {{ dids.v | length }}

      - name: HA Summary V2 - cloud entities
        unique_id: ha_summary_v2_cloud_entities
        default_entity_id: sensor.ha_summary_v2_cloud_entities
        unit_of_measurement: "entities"
        state: >-
          {% set list_all =
             integration_entities('tuya') +
             integration_entities('ewelink') +
             integration_entities('meross_cloud') +
             integration_entities('tapo') +
             integration_entities('tplink') +
             integration_entities('nest') +
             integration_entities('ring') +
             integration_entities('arlo') +
             integration_entities('wyze') +
             integration_entities('myq') +
             integration_entities('nuki') +
             integration_entities('august') +
             integration_entities('switchbot') +
             integration_entities('icloud')
          %}
          {{ list_all | unique | list | length }}
