mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Service validation for conversation component.
This commit is contained in:
parent
003bd24976
commit
7c9729b9c1
@ -8,9 +8,12 @@ import logging
|
|||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import core
|
from homeassistant import core
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON)
|
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON)
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
DOMAIN = "conversation"
|
DOMAIN = "conversation"
|
||||||
|
|
||||||
@ -18,6 +21,10 @@ SERVICE_PROCESS = "process"
|
|||||||
|
|
||||||
ATTR_TEXT = "text"
|
ATTR_TEXT = "text"
|
||||||
|
|
||||||
|
SERVICE_PROCESS_SCHEMA = vol.Schema({
|
||||||
|
vol.Required(ATTR_TEXT): vol.All(cv.string, vol.Lower),
|
||||||
|
})
|
||||||
|
|
||||||
REGEX_TURN_COMMAND = re.compile(r'turn (?P<name>(?: |\w)+) (?P<command>\w+)')
|
REGEX_TURN_COMMAND = re.compile(r'turn (?P<name>(?: |\w)+) (?P<command>\w+)')
|
||||||
|
|
||||||
REQUIREMENTS = ['fuzzywuzzy==0.8.0']
|
REQUIREMENTS = ['fuzzywuzzy==0.8.0']
|
||||||
@ -32,11 +39,7 @@ def setup(hass, config):
|
|||||||
|
|
||||||
def process(service):
|
def process(service):
|
||||||
"""Parse text into commands."""
|
"""Parse text into commands."""
|
||||||
if ATTR_TEXT not in service.data:
|
text = service.data[ATTR_TEXT]
|
||||||
logger.error("Received process service call without a text")
|
|
||||||
return
|
|
||||||
|
|
||||||
text = service.data[ATTR_TEXT].lower()
|
|
||||||
match = REGEX_TURN_COMMAND.match(text)
|
match = REGEX_TURN_COMMAND.match(text)
|
||||||
|
|
||||||
if not match:
|
if not match:
|
||||||
@ -67,6 +70,6 @@ def setup(hass, config):
|
|||||||
logger.error(
|
logger.error(
|
||||||
'Got unsupported command %s from text %s', command, text)
|
'Got unsupported command %s from text %s', command, text)
|
||||||
|
|
||||||
hass.services.register(DOMAIN, SERVICE_PROCESS, process)
|
hass.services.register(DOMAIN, SERVICE_PROCESS, process,
|
||||||
|
schema=SERVICE_PROCESS_SCHEMA)
|
||||||
return True
|
return True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user