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

Conversation

WebSpider
Copy link
Contributor

After PR #447 all the sensors do not have a value after booting HA. This fixes that.

@WebSpider WebSpider added the bugfix This PR contains a bugfix label Jan 5, 2025
@WebSpider WebSpider requested review from dvx76 and prvakt January 5, 2025 02:10
@@ -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.

@WebSpider WebSpider closed this Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This PR contains a bugfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants