Ejemplo n.º 1
0
async def async_setup(hass, config):
    """Set up the Hangouts bot component."""
    from homeassistant.components.conversation import create_matcher

    config = config.get(DOMAIN)
    if config is None:
        hass.data[DOMAIN] = {
            CONF_INTENTS: {},
            CONF_ERROR_SUPPRESSED_CONVERSATIONS: [],
        }
        return True

    hass.data[DOMAIN] = {
        CONF_INTENTS: config[CONF_INTENTS],
        CONF_ERROR_SUPPRESSED_CONVERSATIONS:
            config[CONF_ERROR_SUPPRESSED_CONVERSATIONS],
    }

    for data in hass.data[DOMAIN][CONF_INTENTS].values():
        matchers = []
        for sentence in data[CONF_SENTENCES]:
            matchers.append(create_matcher(sentence))

        data[CONF_MATCHERS] = matchers

    hass.async_create_task(hass.config_entries.flow.async_init(
        DOMAIN, context={'source': config_entries.SOURCE_IMPORT}
    ))

    return True
Ejemplo n.º 2
0
async def async_setup(hass, config):
    """Set up the Hangouts bot component."""
    from homeassistant.components.conversation import create_matcher

    config = config.get(DOMAIN)
    if config is None:
        hass.data[DOMAIN] = {
            CONF_INTENTS: {},
            CONF_ERROR_SUPPRESSED_CONVERSATIONS: [],
        }
        return True

    hass.data[DOMAIN] = {
        CONF_INTENTS:
        config[CONF_INTENTS],
        CONF_ERROR_SUPPRESSED_CONVERSATIONS:
        config[CONF_ERROR_SUPPRESSED_CONVERSATIONS],
    }

    for data in hass.data[DOMAIN][CONF_INTENTS].values():
        matchers = []
        for sentence in data[CONF_SENTENCES]:
            matchers.append(create_matcher(sentence))

        data[CONF_MATCHERS] = matchers

    hass.async_create_task(
        hass.config_entries.flow.async_init(
            DOMAIN, context={'source': config_entries.SOURCE_IMPORT}))

    return True
Ejemplo n.º 3
0
def test_create_matcher():
    """Test the create matcher method."""
    # Basic sentence
    pattern = conversation.create_matcher('Hello world')
    assert pattern.match('Hello world') is not None

    # Match a part
    pattern = conversation.create_matcher('Hello {name}')
    match = pattern.match('hello world')
    assert match is not None
    assert match.groupdict()['name'] == 'world'
    no_match = pattern.match('Hello world, how are you?')
    assert no_match is None

    # Optional and matching part
    pattern = conversation.create_matcher('Turn on [the] {name}')
    match = pattern.match('turn on the kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn on kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn off kitchen lights')
    assert match is None

    # Two different optional parts, 1 matching part
    pattern = conversation.create_matcher('Turn on [the] [a] {name}')
    match = pattern.match('turn on the kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn on kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn on a kitchen light')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen light'

    # Strip plural
    pattern = conversation.create_matcher('Turn {name}[s] on')
    match = pattern.match('turn kitchen lights on')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen light'

    # Optional 2 words
    pattern = conversation.create_matcher('Turn [the great] {name} on')
    match = pattern.match('turn the great kitchen lights on')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn kitchen lights on')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
Ejemplo n.º 4
0
def test_create_matcher():
    """Test the create matcher method."""
    # Basic sentence
    pattern = conversation.create_matcher('Hello world')
    assert pattern.match('Hello world') is not None

    # Match a part
    pattern = conversation.create_matcher('Hello {name}')
    match = pattern.match('hello world')
    assert match is not None
    assert match.groupdict()['name'] == 'world'
    no_match = pattern.match('Hello world, how are you?')
    assert no_match is None

    # Optional and matching part
    pattern = conversation.create_matcher('Turn on [the] {name}')
    match = pattern.match('turn on the kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn on kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn off kitchen lights')
    assert match is None

    # Two different optional parts, 1 matching part
    pattern = conversation.create_matcher('Turn on [the] [a] {name}')
    match = pattern.match('turn on the kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn on kitchen lights')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn on a kitchen light')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen light'

    # Strip plural
    pattern = conversation.create_matcher('Turn {name}[s] on')
    match = pattern.match('turn kitchen lights on')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen light'

    # Optional 2 words
    pattern = conversation.create_matcher('Turn [the great] {name} on')
    match = pattern.match('turn the great kitchen lights on')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
    match = pattern.match('turn kitchen lights on')
    assert match is not None
    assert match.groupdict()['name'] == 'kitchen lights'
Ejemplo n.º 5
0
def test_create_matcher():
    """Test the create matcher method."""
    # Basic sentence
    pattern = conversation.create_matcher("Hello world")
    assert pattern.match("Hello world") is not None

    # Match a part
    pattern = conversation.create_matcher("Hello {name}")
    match = pattern.match("hello world")
    assert match is not None
    assert match.groupdict()["name"] == "world"
    no_match = pattern.match("Hello world, how are you?")
    assert no_match is None

    # Optional and matching part
    pattern = conversation.create_matcher("Turn on [the] {name}")
    match = pattern.match("turn on the kitchen lights")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen lights"
    match = pattern.match("turn on kitchen lights")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen lights"
    match = pattern.match("turn off kitchen lights")
    assert match is None

    # Two different optional parts, 1 matching part
    pattern = conversation.create_matcher("Turn on [the] [a] {name}")
    match = pattern.match("turn on the kitchen lights")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen lights"
    match = pattern.match("turn on kitchen lights")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen lights"
    match = pattern.match("turn on a kitchen light")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen light"

    # Strip plural
    pattern = conversation.create_matcher("Turn {name}[s] on")
    match = pattern.match("turn kitchen lights on")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen light"

    # Optional 2 words
    pattern = conversation.create_matcher("Turn [the great] {name} on")
    match = pattern.match("turn the great kitchen lights on")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen lights"
    match = pattern.match("turn kitchen lights on")
    assert match is not None
    assert match.groupdict()["name"] == "kitchen lights"