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

Support HassTurnOn/Off intents for lock domain #93231

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions homeassistant/components/intent/__init__.py
Expand Up @@ -10,6 +10,11 @@
SERVICE_OPEN_COVER,
)
from homeassistant.components.http.data_validator import RequestDataValidator
from homeassistant.components.lock import (
DOMAIN as LOCK_DOMAIN,
SERVICE_LOCK,
SERVICE_UNLOCK,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TOGGLE,
Expand Down Expand Up @@ -85,6 +90,24 @@ async def async_call_service(self, intent_obj: intent.Intent, state: State) -> N
)
return

if state.domain == LOCK_DOMAIN:
# on = lock
# off = unlock
await self._run_then_background(
hass.async_create_task(
hass.services.async_call(
LOCK_DOMAIN,
SERVICE_LOCK
if self.service == SERVICE_TURN_ON
Copy link
Member

Choose a reason for hiding this comment

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

Ternary operator expressions that span more than one line are really hard to read. I suggest defining a local variable above first and use that here.

else SERVICE_UNLOCK,
{ATTR_ENTITY_ID: state.entity_id},
context=intent_obj.context,
blocking=True,
)
)
)
return

if not hass.services.has_service(state.domain, self.service):
raise intent.IntentHandleError(
f"Service {self.service} does not support entity {state.entity_id}"
Expand Down
39 changes: 39 additions & 0 deletions tests/components/intent/test_init.py
Expand Up @@ -2,6 +2,7 @@
import pytest

from homeassistant.components.cover import SERVICE_OPEN_COVER
from homeassistant.components.lock import SERVICE_LOCK
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_FRIENDLY_NAME,
Expand Down Expand Up @@ -118,6 +119,44 @@ async def test_turn_on_intent(hass: HomeAssistant) -> None:
assert call.data == {"entity_id": ["light.test_light"]}


async def test_translated_turn_on_intent(hass: HomeAssistant) -> None:
"""Test HassTurnOn intent on domains which don't have the intent."""
result = await async_setup_component(hass, "homeassistant", {})
result = await async_setup_component(hass, "intent", {})
await hass.async_block_till_done()
assert result

entity_registry = er.async_get(hass)

cover = entity_registry.async_get_or_create("cover", "test", "cover_uid")
lock = entity_registry.async_get_or_create("lock", "test", "lock_uid")

hass.states.async_set(cover.entity_id, "closed")
hass.states.async_set(lock.entity_id, "unlocked")
cover_service_calls = async_mock_service(hass, "cover", SERVICE_OPEN_COVER)
lock_service_calls = async_mock_service(hass, "lock", SERVICE_LOCK)

await intent.async_handle(
hass, "test", "HassTurnOn", {"name": {"value": cover.entity_id}}
)
await intent.async_handle(
hass, "test", "HassTurnOn", {"name": {"value": lock.entity_id}}
)
await hass.async_block_till_done()

assert len(cover_service_calls) == 1
call = cover_service_calls[0]
assert call.domain == "cover"
assert call.service == "open_cover"
assert call.data == {"entity_id": cover.entity_id}

assert len(lock_service_calls) == 1
call = lock_service_calls[0]
assert call.domain == "lock"
assert call.service == "lock"
assert call.data == {"entity_id": lock.entity_id}


async def test_turn_off_intent(hass: HomeAssistant) -> None:
"""Test HassTurnOff intent."""
result = await async_setup_component(hass, "homeassistant", {})
Expand Down