Skip to content

Commit

Permalink
Use config entry for background tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed May 26, 2024
1 parent 4c75059 commit dd859c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/wyoming/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _make_satellite(
device_id=device.id,
)

return WyomingSatellite(hass, service, satellite_device)
return WyomingSatellite(hass, config_entry, service, satellite_device)


async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
Expand Down
42 changes: 27 additions & 15 deletions homeassistant/components/wyoming/satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from homeassistant.components import assist_pipeline, intent, stt, tts
from homeassistant.components.assist_pipeline import select as pipeline_select
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Context, HomeAssistant, callback

from .const import DOMAIN
Expand Down Expand Up @@ -51,10 +52,15 @@ class WyomingSatellite:
"""Remove voice satellite running the Wyoming protocol."""

def __init__(
self, hass: HomeAssistant, service: WyomingService, device: SatelliteDevice
self,
hass: HomeAssistant,
config_entry: ConfigEntry,
service: WyomingService,
device: SatelliteDevice,
) -> None:
"""Initialize satellite."""
self.hass = hass
self.config_entry = config_entry
self.service = service
self.device = device
self.is_running = True
Expand Down Expand Up @@ -150,7 +156,8 @@ async def on_stopped(self) -> None:
def _send_pause(self) -> None:
"""Send a pause message to satellite."""
if self._client is not None:
self.hass.async_create_background_task(
self.config_entry.async_create_background_task(
self.hass,
self._client.write_event(PauseSatellite().event()),
"pause satellite",
)
Expand Down Expand Up @@ -215,11 +222,11 @@ async def _run_pipeline_loop(self) -> None:
send_ping = True

# Read events and check for pipeline end in parallel
pipeline_ended_task = self.hass.async_create_background_task(
self._pipeline_ended_event.wait(), "satellite pipeline ended"
pipeline_ended_task = self.config_entry.async_create_background_task(
self.hass, self._pipeline_ended_event.wait(), "satellite pipeline ended"
)
client_event_task = self.hass.async_create_background_task(
self._client.read_event(), "satellite event read"
client_event_task = self.config_entry.async_create_background_task(
self.hass, self._client.read_event(), "satellite event read"
)
pending = {pipeline_ended_task, client_event_task}

Expand All @@ -230,8 +237,8 @@ async def _run_pipeline_loop(self) -> None:
if send_ping:
# Ensure satellite is still connected
send_ping = False
self.hass.async_create_background_task(
self._send_delayed_ping(), "ping satellite"
self.config_entry.async_create_background_task(
self.hass, self._send_delayed_ping(), "ping satellite"
)

async with asyncio.timeout(_PING_TIMEOUT):
Expand All @@ -242,8 +249,12 @@ async def _run_pipeline_loop(self) -> None:
# Pipeline run end event was received
_LOGGER.debug("Pipeline finished")
self._pipeline_ended_event.clear()
pipeline_ended_task = self.hass.async_create_background_task(
self._pipeline_ended_event.wait(), "satellite pipeline ended"
pipeline_ended_task = (
self.config_entry.async_create_background_task(
self.hass,
self._pipeline_ended_event.wait(),
"satellite pipeline ended",
)
)
pending.add(pipeline_ended_task)

Expand Down Expand Up @@ -315,8 +326,8 @@ async def _run_pipeline_loop(self) -> None:
_LOGGER.debug("Unexpected event from satellite: %s", client_event)

# Next event
client_event_task = self.hass.async_create_background_task(
self._client.read_event(), "satellite event read"
client_event_task = self.config_entry.async_create_background_task(
self.hass, self._client.read_event(), "satellite event read"
)
pending.add(client_event_task)

Expand Down Expand Up @@ -356,7 +367,8 @@ def _run_pipeline_once(
)
self._is_pipeline_running = True
self._pipeline_ended_event.clear()
self.hass.async_create_background_task(
self.config_entry.async_create_background_task(
self.hass,
assist_pipeline.async_pipeline_from_audio_stream(
self.hass,
context=Context(),
Expand Down Expand Up @@ -584,6 +596,6 @@ def _handle_timer(

if event is not None:
# Send timer event to satellite
self.hass.async_create_background_task(
self._client.write_event(event), "timer event"
self.config_entry.async_create_background_task(
self.hass, self._client.write_event(event), "wyoming timer event"
)

0 comments on commit dd859c5

Please sign in to comment.