Skip to content

Commit

Permalink
Support HassTurnOn/Off intents for lock domain (#93231)
Browse files Browse the repository at this point in the history
* Support HassTurnOn/Off intents for lock domain

Fix home-assistant/intents#1347

* Added tests

* Linting changes

* Linting
  • Loading branch information
tetele committed Nov 1, 2023
1 parent 4a93465 commit 66dd3b1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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 @@ -89,6 +94,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
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

0 comments on commit 66dd3b1

Please sign in to comment.