Repository: home-assistant/core
File: homeassistant/components/analytics/__init__.py (Python)

Code before:

    analytics = Analytics(hass, snapshots_url)

    # Load stored data
    await analytics.load()

    async def _async_handle_labs_update(
        event: Event[labs.EventLabsUpdatedData],
    ) -> None:
        """Handle labs feature toggle."""
        await analytics.save_preferences({ATTR_SNAPSHOTS: event.data["enabled"]})
        await analytics.async_schedule()

    @callback
    def _async_labs_event_filter(event_data: labs.EventLabsUpdatedData) -> bool:
        """Filter labs events for this integration's snapshot feature."""
        return (
            event_data["domain"] == DOMAIN
            and event_data["preview_feature"] == LABS_SNAPSHOT_FEATURE
        )

    async def start_schedule(_event: Event) -> None:
        """Start the send schedule after the started event."""
        await analytics.async_schedule()

        hass.bus.async_listen(
            labs.EVENT_LABS_UPDATED,
            _async_handle_labs_update,
            event_filter=_async_labs_event_filter,
        )

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, start_schedule)

    websocket_api.async_register_command(hass, websocket_analytics)
    websocket_api.async_register_command(hass, websocket_analytics_preferences)

    hass.http.register_view(AnalyticsDevicesView)

    hass.data[DATA_COMPONENT] = analytics
    return True


@callback
@websocket_api.require_admin
@websocket_api.websocket_command({vol.Required("type"): "analytics"})
def websocket_analytics(
    hass: HomeAssistant,
    connection: websocket_api.connection.ActiveConnection,
    msg: dict[str, Any],
) -> None:
    """Return analytics preferences."""

Review comment (refactor):
What do you think about [`labs.async_listen`](https://github.com/home-assistant/core/blob/13b6dfc684823975c5c987170b3cbe464caf2e7a/homeassistant/components/labs/helpers.py#L36) helper? For me, it fits well here.

Code after:

    analytics = Analytics(hass, snapshots_url)

    # Load stored data
    await analytics.load()

    started = False

    async def _async_handle_labs_update(
        event: Event[labs.EventLabsUpdatedData],
    ) -> None:
        """Handle labs feature toggle."""
        await analytics.save_preferences({ATTR_SNAPSHOTS: event.data["enabled"]})
        if started:
            await analytics.async_schedule()

    @callback
    def _async_labs_event_filter(event_data: labs.EventLabsUpdatedData) -> bool:
        """Filter labs events for this integration's snapshot feature."""
        return (
            event_data["domain"] == DOMAIN
            and event_data["preview_feature"] == LABS_SNAPSHOT_FEATURE
        )

    async def start_schedule(_event: Event) -> None:
        """Start the send schedule after the started event."""
        nonlocal started
        started = True
        await analytics.async_schedule()

    hass.bus.async_listen(
        labs.EVENT_LABS_UPDATED,
        _async_handle_labs_update,
        event_filter=_async_labs_event_filter,
    )
    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, start_schedule)

    websocket_api.async_register_command(hass, websocket_analytics)
    websocket_api.async_register_command(hass, websocket_analytics_preferences)

    hass.http.register_view(AnalyticsDevicesView)

    hass.data[DATA_COMPONENT] = analytics
    return True


@callback
@websocket_api.require_admin
@websocket_api.websocket_command({vol.Required("type"): "analytics"})
def websocket_analytics(
    hass: HomeAssistant,