Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull data from REST API when HA has launched #520

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_components/myskoda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def _async_finish_startup(hass, config, vin) -> None:
"MySkoda has finished starting up. Scheduling post-start tasks for vin %s.",
vin,
)
await self._async_update_data()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need different approach because await can only be used with asynchronous functions

I have tried to fix it using code below, and from logs I saw data has been pulled from all REST endpoints, but sensors still remained unknown until first scheduled polling

            async def _finish_startup(hass: HomeAssistant) -> None:
                """Tasks to execute when we have finished starting up."""
                _LOGGER.debug(
                    "MySkoda has finished starting up. Scheduling post-start tasks for vin %s.",
                    self.vin,
                )
                # Schedule the async update and MQTT connection
                await self._async_update_data()
                try:
                    coord = hass.data[DOMAIN][self.entry.entry_id][COORDINATORS][
                        self.vin
                    ]
                    if not coord.myskoda.mqtt and not coord._mqtt_connecting:
                        hass.async_create_task(coord._mqtt_connect())
                except KeyError:
                    _LOGGER.debug("Could not connect to MQTT. Waiting for regular poll")

            # Schedule `_finish_startup` to run after startup
            async_at_started(hass=self.hass, at_start_cb=_finish_startup)
            return State(vehicle, user, config, operations)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we want to just revert back to pulling everything during initialization (leaving just the MQTT connection to be deferred later). I understand we want to avoid failing the init when just one of the API endpoints is acting up, but maybe this current approach is introducing more issues/complexity than it is solving?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been testing this as well, and my idea from last night is indeed flawed.

We probably need redo this by making a difference between the setup, first fetch and regular fetch. HA has options to do this, and there are plenty of examples present, but it turns out 5am is not my most bright moment 😆

Will work on this today some more.

try:
coord = hass.data[DOMAIN][config.entry_id][COORDINATORS][vin]
if not coord.myskoda.mqtt and not coord._mqtt_connecting:
Expand Down
Loading