Added HassOpenCover and HassCloseCover intents (#13372)
* Added intents to cover * Added test for cover intents * Style fixes * Reverted reversions * Async fixes * Woof * Added conditional loading * Added conditional loading * Added conditional loading * Moved tests, fixed logic * Moved tests, fixed logic * Pylint * Pylint * Refactored componenet registration * Refactored componenet registration * Lint
This commit is contained in:
1 parent
bf58945680
commit
bf44dc422c
5 files changed
+185
-83
No files matched your search
@@ -13,10 +13,14 @@ from homeassistant import core
|
||||
from homeassistant.components import http
|
||||
from homeassistant.components.http.data_validator import (
|
||||
RequestDataValidator)
|
||||
from homeassistant.components.cover import (INTENT_OPEN_COVER,
|
||||
INTENT_CLOSE_COVER)
|
||||
from homeassistant.const import EVENT_COMPONENT_LOADED
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers import intent
|
||||
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.setup import (ATTR_COMPONENT)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -28,6 +32,13 @@ DOMAIN = 'conversation'
|
||||
REGEX_TURN_COMMAND = re.compile(r'turn (?P<name>(?: |\w)+) (?P<command>\w+)')
|
||||
REGEX_TYPE = type(re.compile(''))
|
||||
|
||||
UTTERANCES = {
|
||||
'cover': {
|
||||
INTENT_OPEN_COVER: ['Open [the] [a] [an] {name}[s]'],
|
||||
INTENT_CLOSE_COVER: ['Close [the] [a] [an] {name}[s]']
|
||||
}
|
||||
}
|
||||
|
||||
SERVICE_PROCESS = 'process'
|
||||
|
||||
SERVICE_PROCESS_SCHEMA = vol.Schema({
|
||||
@@ -112,6 +123,25 @@ async def async_setup(hass, config):
|
||||
'[the] [a] [an] {name}[s] toggle',
|
||||
])
|
||||
|
||||
@callback
|
||||
def register_utterances(component):
|
||||
"""Register utterances for a component."""
|
||||
if component not in UTTERANCES:
|
||||
return
|
||||
for intent_type, sentences in UTTERANCES[component].items():
|
||||
async_register(hass, intent_type, sentences)
|
||||
|
||||
@callback
|
||||
def component_loaded(event):
|
||||
"""Handle a new component loaded."""
|
||||
register_utterances(event.data[ATTR_COMPONENT])
|
||||
|
||||
hass.bus.async_listen(EVENT_COMPONENT_LOADED, component_loaded)
|
||||
|
||||
# Check already loaded components.
|
||||
for component in hass.config.components:
|
||||
register_utterances(component)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components import group
|
||||
from homeassistant.helpers import intent
|
||||
from homeassistant.const import (
|
||||
SERVICE_OPEN_COVER, SERVICE_CLOSE_COVER, SERVICE_SET_COVER_POSITION,
|
||||
SERVICE_STOP_COVER, SERVICE_OPEN_COVER_TILT, SERVICE_CLOSE_COVER_TILT,
|
||||
@@ -55,6 +56,9 @@ ATTR_CURRENT_TILT_POSITION = 'current_tilt_position'
|
||||
ATTR_POSITION = 'position'
|
||||
ATTR_TILT_POSITION = 'tilt_position'
|
||||
|
||||
INTENT_OPEN_COVER = 'HassOpenCover'
|
||||
INTENT_CLOSE_COVER = 'HassCloseCover'
|
||||
|
||||
COVER_SERVICE_SCHEMA = vol.Schema({
|
||||
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||
})
|
||||
@@ -181,6 +185,12 @@ async def async_setup(hass, config):
|
||||
hass.services.async_register(
|
||||
DOMAIN, service_name, async_handle_cover_service,
|
||||
schema=schema)
|
||||
hass.helpers.intent.async_register(intent.ServiceIntentHandler(
|
||||
INTENT_OPEN_COVER, DOMAIN, SERVICE_OPEN_COVER,
|
||||
"Opened {}"))
|
||||
hass.helpers.intent.async_register(intent.ServiceIntentHandler(
|
||||
INTENT_CLOSE_COVER, DOMAIN, SERVICE_CLOSE_COVER,
|
||||
"Closed {}"))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
Reference in new issue
Block a user