frenck / spook

A scary 👻 powerful toolbox 🧰 for Home Assistant 🏡
https://spook.boo
MIT License
381 stars 36 forks source link

AttributeError: 'Template' object has no attribute 'startswith' #17

Closed Mariusthvdb closed 1 year ago

Mariusthvdb commented 1 year ago

error in the HA log:

Deze fout is ontstaan door een aangepaste integratie.

Logger: custom_components.spook
Source: custom_components/spook/repairs/script_unknown_entity_references.py:96 
Integration: Spook (documentation, issues) 
First occurred: 15:37:18 (66 occurrences) 
Last logged: 16:07:46

Unexpected exception from <bound method SpookRepair.async_inspect of <custom_components.spook.repairs.script_unknown_entity_references.SpookRepair object at 0x7f6202b55990>>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/debounce.py", line 109, in _handle_timer_finish
    await task
  File "/config/custom_components/spook/repairs/script_unknown_entity_references.py", line 92, in async_inspect
    referenced_entities = {
  File "/config/custom_components/spook/repairs/script_unknown_entity_references.py", line 96, in <setcomp>
    not entity_id.startswith("device_tracker.")
AttributeError: 'Template' object has no attribute 'startswith'

v 10.111

frenck commented 1 year ago

This is actually interesting, as the codebase from HA doesn't expect that to happen.

This means that the typing in HA is incorrect, but also that the handling in Core itself is incomplete. So, this is nice. We can contribute findings back this way.

I have an idea of how to approach/solve this in Spook itself, if that works as expected, I can go in and improve Core for this (but needs a bit of work).

../Frenck

frenck commented 1 year ago

Could you provide a way to reproduce it for me? Could you provide an example automation/script? I'm failing at reproducing this.

At first glance, it makes sense it fails... but looking deeper, it starts to look odd.

Mariusthvdb commented 1 year ago

tbh, I wouldn't know how... since it mentions device_tracker, Ive checked my automations and scripts using that, but there is no obvious contender.

unless Spook sees things like:

  epson_printer:
    alias: Epson printer
    mode: restart
    sequence:
      - wait_template: >
          {{is_state('device_tracker.printer_epson','home')}}
      - delay: 5
      - service: script.update_epson_cartridge_sensors

and notices the device is not powered, so 'not_home'. but that would be regular use of a device_tracker, so no use to alert. ofc, there are many such automations/scripts, no idea where to start disabling to see what might be the trigger for Spook.

maybe an extra line with the trigger for the error can be added?

unless object at 0x7faa9dac0430 would already be that. In that case I need some help finding that that would be. I ran a search for it in the full config but it was not found

what to think of this though:

Scherm­afbeelding 2023-03-02 om 10 43 15

wait, never mind that. ofc the trackers of the mobile app that are in a zone show the zone, and not only home/not_home... sorry about that:

{{states.device_tracker|rejectattr('state','search','home|not_home')|list}}

clearly reveals that single tracker...

cut it short, I have no idea where the repeating Spook error might originate

Mariusthvdb commented 1 year ago

o wow, I had a brain block...... ofc, I should have been alarmed by the 'startswith'....

I have made this new device alerter a year ago, when the new device_tracker logic was introduced:

  - alias: New device registered config
    id: new_device_registered_config
    mode: queued
    trigger:
      platform: event
      event_type: entity_registry_updated
      event_data:
        action: create
    condition: >
      {{trigger.event.data.entity_id.startswith('device_tracker')}}
    action:
      - variables:
          data: >
            {{trigger.event.data}}
          entity: >
            {{trigger.event.data.entity_id}}
          name: >
            {{state_attr(entity,'friendly_name')}}
          host: >
            {{state_attr(entity,'host_name')}}
          ip: >
            {{state_attr(entity,'ip')}}
          mac: >
            {{state_attr(entity,'mac')}}
          source: >
            {{state_attr(entity,'source_type')}}
          id: >
            {{device_id(entity)}}
          config: >
            - entity: {{entity}},

            - name: {{name}},

            - host: {{host}},

            - ip: {{ip}},

            - mac: {{mac}},

            - source_type: {{source_type}}

            - id: {{id}}
          time: >
            {{now().timestamp()|timestamp_custom('%X')}}
          title: >
            {{time}} - New device registered: {{name}}
          message_body: >
           New device config: {{config}}

      - service: system_log.write
        data:
          message: >
            {{data}}
          level: warning
          logger: homeassistant.components.device_tracker
      - choose:
          - alias: 'Write new device to log?'
            conditions:
              condition: state
              entity_id: input_boolean.write_new_device_to_log
              state: 'on'
            sequence:
              service: system_log.write
              data:
                message: >
                  {{message_body}}
                  Event: {{trigger.event}}
                  Data: {{data}}
                  Data verbose: {{trigger.event.data}}
                level: warning
                logger: homeassistant.components.device_tracker
      - choose:
          - alias: 'Create persistent notification for new device?'
            conditions:
              condition: state
              entity_id: input_boolean.persistent_notification_new_device
              state: 'on'
            sequence:
              service: persistent_notification.create
              data:
                title: >
                  {{title}}
                message: >
                  {{message_body}}
      - choose:
          - alias: 'Notify of new device?'
            conditions:
              condition: state
              entity_id: input_boolean.notify_new_device
              state: 'on'
            sequence:
              service: notify.mobile_app_marijn
              data:
                title: >
                  {{title}}
                message: >
                  {{message_body}}

update

Ive turned these automations off and restarted, but the Spook error persists.

next Ive commented them and Spook still reports the error.??

proof they no longer exist:

Scherm­afbeelding 2023-03-02 om 13 08 56

the legacy automation does not contain the 'startswith', so I've left that one around

btw, the second automation is a shorter version:

  - alias: New device registered
    id: new_device_registered
    mode: queued
    trigger:
      platform: event
      event_type: entity_registry_updated
      event_data:
        action: create
    condition: >
      {{trigger.event.data.entity_id.startswith('device_tracker')}}
    action:
      - variables:
          entity: >
            {{trigger.event.data.entity_id}}
      - service: system_log.write
        data:
          message: >
            New device registrered: {{state_attr(entity,'friendly_name')}}
          level: warning
          logger: homeassistant.components.device_tracker
      - service: persistent_notification.create
        data:
          title: >
            New device registered: {{state_attr(entity,'friendly_name')}}
          message: >
            Entity: {{entity}}
            Host: {{state_attr(entity,'host_name')}}
            Ip: {{state_attr(entity,'ip')}}
            Mac-address: {{state_attr(entity,'mac')}}
            Id: {{device_id(entity)}}
#            Full data: {{trigger.event.data}}
frenck commented 1 year ago

I'm not able to reproduce it with the examples given, nor do I see a possible code path that may cause this (not in Spook and not in HA).

This is the output from the first bigger example you have in the last response:

image

I have tested this on both my prod & dev setups, and both had the same results.

../Frenck

Mariusthvdb commented 1 year ago

yeah, but that is a real 'repair'... I only get the errors in the logs.

I now realize the whole line is created by the script_unknown_entity_references.py itself, where above, I figured the device_tracker mentioning is because of a real missing device_tracker..

how can it error like that, while it should be silent about it in the first place?

Filter out scenes, groups & device_tracker entities.

Ive scrutinized my complete yaml config for device_trackers, and did find 2 being mentioned that are not available in the states. 1 is inside a group:

group:

  device_trackers_media:
    name: Device trackers media
    icon: mdi:theater
    entities:

      - device_tracker.nintendo_switch

and 1 is inside a binary:

template:

  - binary_sensor:

      - unique_id: imac_louise_off
        name: iMac Louise Off
        state: >
          {{is_state('device_tracker.imac_lan','not_home') and
            is_state('device_tracker.imac_wifi','not_home')}}
        delay_on:
          seconds: 30

the _wifi one not existing currently in the states.

I will comment those and restart, maybe that will fix it.

then again, shouldnt this be recognized by Spook, and brought up as 2 repairs?

edit

no, makes no difference. error still being logged

Mariusthvdb commented 1 year ago

not sure if this hold a lead, but the error has changed a bit:

Deze fout is ontstaan door een aangepaste integratie.

Logger: custom_components.spook
Source: custom_components/spook/repairs/script_unknown_entity_references.py:100 
Integration: Spook (documentation, issues) 
First occurred: 20:39:58 (420 occurrences) 
Last logged: 23:28:04

Unexpected exception from <bound method SpookRepair.async_inspect of <custom_components.spook.repairs.script_unknown_entity_references.SpookRepair object at 0x7fdfb5f12950>>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/debounce.py", line 109, in _handle_timer_finish
    await task
  File "/config/custom_components/spook/repairs/script_unknown_entity_references.py", line 96, in async_inspect
    if unknown_entities := {
  File "/config/custom_components/spook/repairs/script_unknown_entity_references.py", line 100, in <setcomp>
    not entity_id.startswith(("device_tracker.", "group.", "scene."))
AttributeError: 'Template' object has no attribute 'startswith'

maybe only because the update changed it's backend code?

Mariusthvdb commented 1 year ago

merely as an update: HA 2023.3.4 and Spook 11.1112 this is still happening:

Logger: custom_components.spook
Source: custom_components/spook/repairs/script_unknown_entity_references.py:100 
Integration: Spook ([documentation](https://github.com/frenck/spook), [issues](https://github.com/frenck/spook/issues)) 
First occurred: 07:54:26 (157 occurrences) 
Last logged: 09:49:46
frenck commented 1 year ago

As expected.

Mariusthvdb commented 1 year ago

yes, well, Ive been trying to delete a any custom resource to see if it could be the source for this, but havent found any yet. So you dont think I was forgetting about it..

frenck commented 1 year ago

As per above, I have no reproduction, this issue is not going to be solved without it.

Mariusthvdb commented 1 year ago

just reinstalled the 10.100 version, figuring to test whether it was Spook being updated or Home Assistant. The 'startswith' error is not logged, indicating its the changes in Spook causing this?

I do see the former other repairs on templates I had ignored before:

Scherm­afbeelding 2023-03-14 om 10 09 28

but these probably are not related (as in: are they the same issue, but worded differently)?

to4ko commented 1 year ago

just installed latest version of spook and get this in log

2023-03-15 17:22:48.247 ERROR (MainThread) [custom_components.spook] Unexpected exception from <bound method SpookRepair.async_inspect of <custom_components.spook.repairs.automation_unknown_entity_references.SpookRepair object at 0x7f6bebf75900>> Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/debounce.py", line 109, in _handle_timer_finish await task File "/config/custom_components/spook/repairs/automation_unknown_entity_references.py", line 95, in async_inspect if unknown_entities := { File "/config/custom_components/spook/repairs/automation_unknown_entity_references.py", line 99, in not entity_id.startswith(("device_tracker.", "group.", "scene.")) AttributeError: 'Template' object has no attribute 'startswith'

only one usage of startswith in my yaml files is https://github.com/to4ko/myconfig/blob/master/my_config/automation/system/updates.yaml#L46

`condition:

upd: automation deleted from config, HA restarted, automation became unavailable in HA and was deleted...error still in log

frenck commented 1 year ago

Thanks, @to4ko!