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

Change wake word interception to a subscription #125629

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Allow stopping intercepting wake words
  • Loading branch information
synesthesiam committed Sep 11, 2024
commit e27b3f0001cb5dd22375634e4818e73569faf952
11 changes: 10 additions & 1 deletion homeassistant/components/assist_satellite/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ async def async_intercept_wake_word(self) -> str | None:
Returns the detected wake word phrase or None.
"""
if self._wake_word_intercept_future is not None:
raise SatelliteBusyError("Wake word interception already in progress")
# Cancel existing interception
self._wake_word_intercept_future.set_result(None)
self._wake_word_intercept_future = None

# Will cause next wake word to be intercepted in
# async_accept_pipeline_from_satellite
Expand All @@ -118,6 +120,13 @@ async def async_intercept_wake_word(self) -> str | None:
finally:
self._wake_word_intercept_future = None

def async_stop_intercept_wake_word(self) -> None:
"""Stop intercepting wake words."""
if self._wake_word_intercept_future is not None:
# Cancel existing interception
self._wake_word_intercept_future.set_result(None)
self._wake_word_intercept_future = None

async def async_internal_announce(
self,
message: str | None = None,
Expand Down
33 changes: 30 additions & 3 deletions homeassistant/components/assist_satellite/websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
@callback
def async_register_websocket_api(hass: HomeAssistant) -> None:
"""Register the websocket API."""
websocket_api.async_register_command(hass, websocket_intercept_wake_word)
websocket_api.async_register_command(hass, websocket_intercept_wake_word_start)
websocket_api.async_register_command(hass, websocket_intercept_wake_word_stop)


@callback
@websocket_api.websocket_command(
{
vol.Required("type"): "assist_satellite/intercept_wake_word",
vol.Required("type"): "assist_satellite/intercept_wake_word/start",
vol.Required("entity_id"): cv.entity_domain(DOMAIN),
}
)
@websocket_api.require_admin
@websocket_api.async_response
async def websocket_intercept_wake_word(
async def websocket_intercept_wake_word_start(
hass: HomeAssistant,
connection: websocket_api.connection.ActiveConnection,
msg: dict[str, Any],
Expand All @@ -44,3 +45,29 @@ async def websocket_intercept_wake_word(

wake_word_phrase = await satellite.async_intercept_wake_word()
connection.send_result(msg["id"], {"wake_word_phrase": wake_word_phrase})


@callback
@websocket_api.websocket_command(
{
vol.Required("type"): "assist_satellite/intercept_wake_word/stop",
vol.Required("entity_id"): cv.entity_domain(DOMAIN),
}
)
@websocket_api.require_admin
@websocket_api.async_response
async def websocket_intercept_wake_word_stop(
hass: HomeAssistant,
connection: websocket_api.connection.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Intercept the next wake word from a satellite."""
component: EntityComponent[AssistSatelliteEntity] = hass.data[DOMAIN]
satellite = component.get_entity(msg["entity_id"])
if satellite is None:
connection.send_error(
msg["id"], websocket_api.ERR_NOT_FOUND, "Entity not found"
)
return

satellite.async_stop_intercept_wake_word()
55 changes: 42 additions & 13 deletions tests/components/assist_satellite/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def test_intercept_wake_word(

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word",
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": ENTITY_ID,
}
)
Expand Down Expand Up @@ -55,7 +55,7 @@ async def test_intercept_wake_word_requires_on_device_wake_word(

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word",
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": ENTITY_ID,
}
)
Expand Down Expand Up @@ -88,7 +88,7 @@ async def test_intercept_wake_word_requires_wake_word_phrase(

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word",
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": ENTITY_ID,
}
)
Expand Down Expand Up @@ -124,7 +124,7 @@ async def test_intercept_wake_word_require_admin(

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word",
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": ENTITY_ID,
}
)
Expand All @@ -148,7 +148,7 @@ async def test_intercept_wake_word_invalid_satellite(

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word",
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": "assist_satellite.invalid",
}
)
Expand All @@ -167,26 +167,55 @@ async def test_intercept_wake_word_twice(
entity: MockAssistSatellite,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test intercepting a wake word requires admin access."""
"""Test intercepting a wake word twice cancels the previous request."""
ws_client = await hass_ws_client(hass)

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word",
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": ENTITY_ID,
}
)

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word",
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": ENTITY_ID,
}
)
response = await ws_client.receive_json()

assert not response["success"]
assert response["error"] == {
"code": "home_assistant_error",
"message": "Wake word interception already in progress",
}
assert response["success"]
assert response["result"] == {"wake_word_phrase": None}


async def test_intercept_wake_word_stop(
hass: HomeAssistant,
init_components: ConfigEntry,
entity: MockAssistSatellite,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test that we can stop intercepting wake words."""
ws_client = await hass_ws_client(hass)

await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word/start",
"entity_id": ENTITY_ID,
}
)

task = asyncio.create_task(ws_client.receive_json())

async with asyncio.timeout(1):
# Will cancel the open request
await ws_client.send_json_auto_id(
{
"type": "assist_satellite/intercept_wake_word/stop",
"entity_id": ENTITY_ID,
}
)

response = await task
assert response["success"]
assert response["result"] == {"wake_word_phrase": None}