Correct actions order in async_setup_entry #356
Merged
+9
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem I was facing for a long time:
Right after LocalTuya start or restart, some random entities may have Unavailable status, until they are updated from corresponding devices.
The reason:
The status of an entity can be updated by LocalTuya means only after the entity has been registered in HASS, i.e. after
LocalTuyaEntity.async_added_to_hass()
method was called, which registers_update_handler()
. Butasync_setup_entry()
first created async tasks to connect to devices, then waited for the first successful connect, and only then calledhass.config_entries.async_forward_entry_setups()
which creates entities instances in HASS.async_connect()
->_make_connection()
may change device's entities statuses, but the changes are not passed as they should.With the recent global changes in device initialization and connection order, Zigbee and BLE sub-devices are connected with a delay mostly enough to propagate status updates, but WiFi low power sensors (e.g. T&H sensor) always have Unavailable status, because at the time of its first
_make_connection()
call, which starts withhass-localtuya/custom_components/localtuya/coordinator.py
Lines 172 to 173 in 284ebb5
didn't lead to
_update_handler()
call, but madeself._status
not empty, preventing calls toself.status_updated(RESTORE_STATES)
during further connection attempts.The solution:
Creating of
async_connect()
tasks is delayed until entities are fully initialized by HASS. There is no a reason to wait for the first successful connection here. So, first, LocalTuya objects are created, then HASS objects are created, and only then startes the connection process.Now it works as expected. This was the last known to me problem with LocalTuya engine 😄