2015-03-10 07:08:50 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support for functionality to have conversations with Home Assistant.
|
2015-10-25 14:40:21 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/conversation/
|
2015-03-10 07:08:50 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import re
|
2016-03-29 06:46:19 +00:00
|
|
|
import warnings
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2016-04-13 16:48:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
from homeassistant import core
|
2015-03-10 07:08:50 +00:00
|
|
|
from homeassistant.const import (
|
2016-02-19 05:27:50 +00:00
|
|
|
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON)
|
2016-04-13 16:48:39 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2016-08-22 12:19:19 +00:00
|
|
|
REQUIREMENTS = ['fuzzywuzzy==0.11.1']
|
|
|
|
|
|
|
|
ATTR_TEXT = 'text'
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2016-08-22 12:19:19 +00:00
|
|
|
DOMAIN = 'conversation'
|
|
|
|
|
|
|
|
REGEX_TURN_COMMAND = re.compile(r'turn (?P<name>(?: |\w)+) (?P<command>\w+)')
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2016-08-22 12:19:19 +00:00
|
|
|
SERVICE_PROCESS = 'process'
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2016-04-13 16:48:39 +00:00
|
|
|
SERVICE_PROCESS_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(ATTR_TEXT): vol.All(cv.string, vol.Lower),
|
|
|
|
})
|
|
|
|
|
2015-03-10 07:08:50 +00:00
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Register the process service."""
|
2016-03-29 06:46:19 +00:00
|
|
|
warnings.filterwarnings('ignore', module='fuzzywuzzy')
|
2015-12-15 01:17:43 +00:00
|
|
|
from fuzzywuzzy import process as fuzzyExtract
|
|
|
|
|
2015-03-10 07:08:50 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def process(service):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Parse text into commands."""
|
2016-04-13 16:48:39 +00:00
|
|
|
text = service.data[ATTR_TEXT]
|
2015-03-10 07:08:50 +00:00
|
|
|
match = REGEX_TURN_COMMAND.match(text)
|
|
|
|
|
|
|
|
if not match:
|
|
|
|
logger.error("Unable to process: %s", text)
|
|
|
|
return
|
|
|
|
|
|
|
|
name, command = match.groups()
|
2015-12-15 01:17:43 +00:00
|
|
|
entities = {state.entity_id: state.name for state in hass.states.all()}
|
2016-03-08 16:55:57 +00:00
|
|
|
entity_ids = fuzzyExtract.extractOne(name, entities,
|
2015-12-15 01:17:43 +00:00
|
|
|
score_cutoff=65)[2]
|
2015-03-10 07:08:50 +00:00
|
|
|
|
|
|
|
if not entity_ids:
|
|
|
|
logger.error(
|
|
|
|
"Could not find entity id %s from text %s", name, text)
|
|
|
|
return
|
|
|
|
|
|
|
|
if command == 'on':
|
2015-08-17 03:44:46 +00:00
|
|
|
hass.services.call(core.DOMAIN, SERVICE_TURN_ON, {
|
|
|
|
ATTR_ENTITY_ID: entity_ids,
|
|
|
|
}, blocking=True)
|
2015-03-10 07:08:50 +00:00
|
|
|
|
|
|
|
elif command == 'off':
|
2015-08-17 03:44:46 +00:00
|
|
|
hass.services.call(core.DOMAIN, SERVICE_TURN_OFF, {
|
|
|
|
ATTR_ENTITY_ID: entity_ids,
|
|
|
|
}, blocking=True)
|
2015-03-10 07:08:50 +00:00
|
|
|
|
|
|
|
else:
|
2016-07-02 18:12:48 +00:00
|
|
|
logger.error('Got unsupported command %s from text %s',
|
|
|
|
command, text)
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2016-04-13 16:48:39 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_PROCESS, process,
|
|
|
|
schema=SERVICE_PROCESS_SCHEMA)
|
2015-03-10 07:08:50 +00:00
|
|
|
return True
|