opened
JurajNyiri
username_0
Opensprinkler sometimes does not initiate properly.

When this is happening, home assistant seems to be taking a long time to load (reason currently unknown).

I did some digging and I think that slow start of HASS causes timeouts and/or race conditions inside pyopensprinkler and this component.

**Component should be able to recover from this and try to set up again and not give up.**

```
Logger: custom_components.opensprinkler
Source: helpers/update_coordinator.py:147
Integration: OpenSprinkler (documentation)
First occurred: October 13, 2020, 11:42:59 PM (1 occurrences)
Last logged: October 13, 2020, 11:42:59 PM

Timeout fetching OpenSprinkler resource status data
```

```
Error while setting up opensprinkler platform for binary_sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 201, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 310, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 371, in _async_add_entity
    device_info = entity.device_info
  File "/config/custom_components/opensprinkler/__init__.py", line 134, in device_info
    model = controller.hardware_version_name or "Unknown"
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 471, in hardware_version_name
    if self.hardware_version == HARDWARE_VERSION_OSPI:
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 466, in hardware_version
    return self._get_option("hwv")
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 274, in _get_option
    return self._get_options()[option]
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 280, in _get_options
    return self._retrieve_state()["options"]
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 268, in _retrieve_state
    raise OpenSprinklerNoStateError("No state. Please refresh")
pyopensprinkler.OpenSprinklerNoStateError: No state. Please refresh
```

```
Error while setting up opensprinkler platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 201, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 310, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 371, in _async_add_entity
    device_info = entity.device_info
  File "/config/custom_components/opensprinkler/__init__.py", line 134, in device_info
    model = controller.hardware_version_name or "Unknown"
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 471, in hardware_version_name
    if self.hardware_version == HARDWARE_VERSION_OSPI:
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 466, in hardware_version
    return self._get_option("hwv")
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 274, in _get_option
    return self._get_options()[option]
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 280, in _get_options
    return self._retrieve_state()["options"]
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 268, in _retrieve_state
    raise OpenSprinklerNoStateError("No state. Please refresh")
pyopensprinkler.OpenSprinklerNoStateError: No state. Please refresh
```

```
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 201, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 310, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 371, in _async_add_entity
    device_info = entity.device_info
  File "/config/custom_components/opensprinkler/__init__.py", line 134, in device_info
    model = controller.hardware_version_name or "Unknown"
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 471, in hardware_version_name
    if self.hardware_version == HARDWARE_VERSION_OSPI:
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 466, in hardware_version
    return self._get_option("hwv")
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 274, in _get_option
    return self._get_options()[option]
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 280, in _get_options
    return self._retrieve_state()["options"]
  File "/usr/local/lib/python3.8/site-packages/pyopensprinkler/__init__.py", line 268, in _retrieve_state
    raise OpenSprinklerNoStateError("No state. Please refresh")
pyopensprinkler.OpenSprinklerNoStateError: No state. Please refresh
```

I think this is caused internally by some race conditions:
- _retrieve_state in pyopensprinkler executes before update is finished, throws exception
- as there was no retry logic, component setup fails

Ideally, issue should be resolved by eliminating the race condition and / or implementing component retry setup logic after some time.

I wrote a (very ugly workaround)[https://github.com/username_0/hass-opensprinkler/blob/fix_timeout_on_start/custom_components/opensprinkler/__init__.py#L61], which works and now boots 100% of the time. 
In my testing, it failed the 1st time, went into exception, recursion and then was successful and continued setup.

I tried restarting multiple times on both rpis I have and it just didn't want to initiate correctly so I finally had some time to debug and find the cause. 
I have encountered this issue before and I remember it also being discussed in Community without a resolution.

I do not know how to replicate this as I do not know the cause. Sometimes it just starts happening and recovers the next day when I restart.
Maybe try adding some sleeps to simulate this and then try to fix?
Component fails to start
issue
created
vinteo
username_1
It would be great if you could open a PR for either pyopensprinkler (to fix the race condition) or with this workaround. Hacktoberfest is still ongoing :)
comment
created
JurajNyiri
username_0
Thank you for the suggestion. 
However, currently I don't have time to fix it properly and I am not proud about that workaround, it's really ugly and should be fixed properly.
comment
closed
vinteo
username_1
issue
created
vinteo
username_1
Please try the latest release v1.1.3
comment
created
JurajNyiri
username_0
Thank you, I have now encountered a similar scenario, it threw an error 
```
Logger: custom_components.opensprinkler
Source: helpers/update_coordinator.py:147
Integration: OpenSprinkler (documentation)
First occurred: 3:11:59 PM (1 occurrences)
Last logged: 3:11:59 PM

Timeout fetching OpenSprinkler resource status data
```

but then recovered. 👍
comment