gcobb321 / icloud3_v3

iCloud3 v3, Development Version - This Repo/Version is used for development and testing of new and updated features. It is not the official release of iCloud3 v3, .
MIT License
143 stars 11 forks source link

'NoneType' object has no attribute 'loop' #294

Open jasonwragg opened 3 months ago

jasonwragg commented 3 months ago

I am receiving the following in my Home Assistant log:

This error originated from a custom integration.

Logger: custom_components.icloud3 Source: helpers/entity.py:1213 integration: iCloud3 v3: iDevice Tracker (documentation, issues) First occurred: April 4, 2024 at 9:06:28 PM (9888 occurrences) Last logged: 12:54:10 PM

'NoneType' object has no attribute 'loop'
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/icloud3/sensor.py", line 1259, in async_update_sensor
    self.schedule_update_ha_state()
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1213, in schedule_update_ha_state
    self.hass.loop.call_soon_threadsafe(self.async_write_ha_state)
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'loop'
gcobb321 commented 3 months ago

This is an error that happened when there was nothing to update but iC3 tried to do it anyway. I added additional error checking to catch this error for the next release.

UlrichThiess commented 5 days ago

I also have this error message:

2024-07-01 18:09:40.738 ERROR (SyncWorker_24) [custom_components.icloud3] Traceback (most recent call last):
  File "/config/custom_components/icloud3/icloud3_main.py", line 926, in process_updated_location_data
    Device.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device.py", line 1606, in write_ha_device_tracker_state
    self.DeviceTracker.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device_tracker.py", line 614, in write_ha_device_tracker_state
    self.schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1231, in schedule_update_ha_state
    self.hass.loop.call_soon_threadsafe(
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'loop'

It started after I got a new iPhone and changed the name of the device.

gcobb321 commented 4 days ago

That message actually comes from HA, not iCloud3. The loop the error refers to is the Event loop which is started after HA starts. Does it happen all the time or only once when starting? Does it happen after you change the device name (iPhone famshr name, not iCloud3 device tracker entity name)? Is the device tracked after everything has started? Are you running v3.0.5.2?

UlrichThiess commented 4 days ago

It occurs twice every 20 seconds. I have registered two iPhones. All data in the iCloud3 configuration has been checked. I am no longer sure since when this has been happening. I am using the latest versions. This morning, I restarted the computer:

2024-07-02 08:58:19.401 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration fontawesome which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.402 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration openweathermaphistory which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.403 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration ingress which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.403 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration mass which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.403 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration ics_calendar which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.403 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration watchman which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.404 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration spook which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.404 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration blitzortung which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.404 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration climate_group which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.404 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration simpleicons which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.404 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration alexa_media which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.405 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration auto_backup which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.405 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration measureit which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.405 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration candy which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.405 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration icloud3 which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.406 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration miele which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.406 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration hvac_group which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.406 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration spook_inverse which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:19.406 WARNING (SyncWorker_1) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-07-02 08:58:22.780 WARNING (SyncWorker_9) [homeassistant.helpers.frame] Detected that custom integration 'ics_calendar' accesses hass.helpers.discovery. This is deprecated and will stop working in Home Assistant 2024.11, it should be updated to import functions used from discovery directly at custom_components/ics_calendar/__init__.py, line 106: hass.helpers.discovery.load_platform(, please report it to the author of the 'ics_calendar' custom integration
2024-07-02 08:58:23.843 ERROR (MainThread) [homeassistant.components.device_tracker] Platform icloud3 does not generate unique IDs. ID icloud3_ulrichs_iphone is already used by device_tracker.ulrichs_iphone - ignoring device_tracker.ulrichs_iphone
2024-07-02 08:58:24.298 WARNING (MainThread) [homeassistant.helpers.frame] Detected that custom integration 'hacs' accesses hass.components.frontend. This is deprecated and will stop working in Home Assistant 2024.9, it should be updated to import functions used from frontend directly at custom_components/hacs/frontend.py, line 68: hass.components.frontend.async_register_built_in_panel(, please create a bug report at https://github.com/hacs/integration/issues
2024-07-02 08:58:24.400 WARNING (MainThread) [homeassistant.components.tankerkoenig.coordinator] Found more than 10 stations to check. This might invalidate your api-key on the long run. Try using a smaller radius
2024-07-02 08:58:25.927 ERROR (SyncWorker_8) [custom_components.icloud3] 'NoneType' object has no attribute 'loop'
Traceback (most recent call last):
  File "/config/custom_components/icloud3/icloud3_main.py", line 926, in process_updated_location_data
    Device.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device.py", line 1606, in write_ha_device_tracker_state
    self.DeviceTracker.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device_tracker.py", line 614, in write_ha_device_tracker_state
    self.schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1231, in schedule_update_ha_state
    self.hass.loop.call_soon_threadsafe(
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'loop'
2024-07-02 08:58:25.930 ERROR (SyncWorker_8) [custom_components.icloud3] Traceback (most recent call last):
  File "/config/custom_components/icloud3/icloud3_main.py", line 926, in process_updated_location_data
    Device.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device.py", line 1606, in write_ha_device_tracker_state
    self.DeviceTracker.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device_tracker.py", line 614, in write_ha_device_tracker_state
    self.schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1231, in schedule_update_ha_state
    self.hass.loop.call_soon_threadsafe(
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'loop'
2024-07-02 08:58:27.353 WARNING (MainThread) [homeassistant.helpers.entity] Updating state for sensor.echo_ug_tx (<class 'homeassistant.components.unifi.sensor.UnifiSensorEntity'>) took 0.412 seconds. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+unifi%22
2024-07-02 08:58:27.901 WARNING (MainThread) [aioesphomeapi.reconnect_logic] Can't connect to ESPHome API for atta-cephalotes-pilz-kamera @ 192.168.0.231: Error connecting to [AddrInfo(family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, proto=6, sockaddr=IPv4Sockaddr(address='192.168.0.231', port=6053))]: [Errno 111] Connect call failed ('192.168.0.231', 6053) (SocketAPIError)
2024-07-02 08:58:30.539 ERROR (SyncWorker_24) [custom_components.icloud3] 'NoneType' object has no attribute 'loop'
Traceback (most recent call last):
  File "/config/custom_components/icloud3/icloud3_main.py", line 926, in process_updated_location_data
    Device.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device.py", line 1606, in write_ha_device_tracker_state
    self.DeviceTracker.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device_tracker.py", line 614, in write_ha_device_tracker_state
    self.schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1231, in schedule_update_ha_state
    self.hass.loop.call_soon_threadsafe(
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'loop'
2024-07-02 08:58:30.547 ERROR (SyncWorker_24) [custom_components.icloud3] Traceback (most recent call last):
  File "/config/custom_components/icloud3/icloud3_main.py", line 926, in process_updated_location_data
    Device.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device.py", line 1606, in write_ha_device_tracker_state
    self.DeviceTracker.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device_tracker.py", line 614, in write_ha_device_tracker_state
    self.schedule_update_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1231, in schedule_update_ha_state
    self.hass.loop.call_soon_threadsafe(
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'loop'
gcobb321 commented 4 days ago

HA guys had me change the way entities are updated a little while back. I’m away so I can not send you an update but, if you want, try this.

In _/icloud3/devicetracker.py, change lines 612-613

from:

        # self.async_write_ha_state()
        self.schedule_update_ha_state()

to

        self.async_write_ha_state()
        # self.schedule_update_ha_state()

Then restart HA. Let me know if that works. what version of HA are you running?

UlrichThiess commented 4 days ago

Now i get:

Traceback (most recent call last):
  File "/config/custom_components/icloud3/icloud3_main.py", line 926, in process_updated_location_data
    Device.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device.py", line 1606, in write_ha_device_tracker_state
    self.DeviceTracker.write_ha_device_tracker_state()
  File "/config/custom_components/icloud3/device_tracker.py", line 613, in write_ha_device_tracker_state
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 999, in async_write_ha_state
    self.hass.verify_event_loop_thread("async_write_ha_state")
  File "/usr/src/homeassistant/homeassistant/core.py", line 447, in verify_event_loop_thread
    frame.report(
  File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 159, in report
    _report_integration(what, integration_frame, level, error_if_integration)
  File "/usr/src/homeassistant/homeassistant/helpers/frame.py", line 197, in _report_integration
    raise RuntimeError(
RuntimeError: Detected that custom integration 'icloud3' calls async_write_ha_state from a thread other than the event loop, which may cause Home Assistant to crash or data to corrupt. For more information, see https://developers.home-assistant.io/docs/asyncio_thread_safety/#async_write_ha_state at custom_components/icloud3/device_tracker.py, line 613: self.async_write_ha_state(). Please report it to the author of the 'icloud3' custom integration.

Maybe it has to do with the fact that my iPhone has already been set up as device_tracker.ulrichs_iphone. That is its DNS name, and the UniFi extension starts first.

I will change that and get back to you later.

gcobb321 commented 4 days ago

Undo the change you made so it is back like it was — the ‘#’ is on the self.async_write_ha_state() and the other one does not have an ‘#’.

The original error is coming from HA. Is there something unique about your setup - virtual environment, docker, etc. No one else has had this error. In running on a RPi with a basic HassIO config. Core v2024.6.4, supervisor v2024.06.2, OS v12.1

UlrichThiess commented 4 days ago

Yes, it was related to the fact that device_tracker.ulrichs_iphone was already created. I renamed it, and now the error is no longer present. Please excuse me for reporting a self-made error and taking up your time. However, now we know the cause of this error. If you check the LOG right at the start of Home Assistant, you can find an error message related to it. Can the case be closed?

gcobb321 commented 3 days ago

When an iCloud3 de device is added on the Configure Settings > Update Devices screen, a check if made to see if the occ device \tracker entity name is already used. So you recall if another component was using the ulrichs_iphone before it was set up by iCloud3 or if iCloud3 had set it up first and then it was also assigned to the other component later? I’m wondering if that other component then loaded before iCloud3 and the other component then owned the name which iCloud3 then tried to do an update for that entity.

What was the name of the other program? Can you email the .storage/core.device_registry and core.entity_registry files to geekstergary@gmail.com. I’d like to see if I can add a check for error recovery during startup to prevent this from occurring

gcobb321 commented 3 days ago

I’ve updated the code in the recorder_prefilter.py file in the iCloud3 GitHub repository. See here for download instructions.