Skip to content

Snips sounds#13746

Merged
balloob merged 8 commits intohome-assistant:devfrom
todschmidt:snips-sounds
Apr 9, 2018
Merged

Snips sounds#13746
balloob merged 8 commits intohome-assistant:devfrom
todschmidt:snips-sounds

Conversation

@todschmidt
Copy link
Copy Markdown
Contributor

@todschmidt todschmidt commented Apr 7, 2018

Description:

Added configuration to enable feedback sounds.
Added probability threshold

Related issue (if applicable): fixes #N/A

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5133

Example entry for configuration.yaml (if applicable):

snips:
  probability_threshold: 0.40
  feedback_sounds: true
  site_ids:
    - default
    - remote

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

Comment thread tests/components/test_snips.py Outdated
assert result
await hass.async_block_till_done()

assert len(events) == 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

for site_id in config[DOMAIN].get(CONF_SITE_IDS):
payload = json.dumps({'siteId': site_id})
hass.components.mqtt.async_publish(
FEEDBACK_ON_TOPIC, None, qos=0, retain=False)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you doing here? Is this to clear the retained message?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Comment thread homeassistant/components/snips.py Outdated
site_ids = ([call.data.get(ATTR_SITE_ID)]
if call.data.get(ATTR_SITE_ID)
else config[DOMAIN].get(CONF_SITE_IDS))
for site_id in site_ids:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this into helper methods please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roger

Comment thread homeassistant/components/snips.py Outdated
hass.components.mqtt.async_publish(
FEEDBACK_ON_TOPIC, payload, qos=1, retain=True)
else:
for site_id in config[DOMAIN].get(CONF_SITE_IDS):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert to helper method.

Comment thread homeassistant/components/snips.py Outdated
CONFIG_SCHEMA = vol.Schema({
DOMAIN: {}
DOMAIN: vol.Schema({
vol.Optional(CONF_FEEDBACK, default=False): cv.boolean,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be configurable per site id?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured that was enough of an edge case it wasn't worth complicating the config for. They can turn on or off a site via the service but I can't imagine there are many cases where anyone wants to do that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to limit this functionality to just the service? You don't need to change it all the time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, it's pretty minor to add it and I think a lot of users will like to set it here, especially considering snips doesn't have the option yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially for something like hassio where you reboot and it's nice to have it back on if you want it.

for site_id in site_ids:
payload = json.dumps({'siteId': site_id})
hass.components.mqtt.async_publish(
FEEDBACK_ON_TOPIC, None, qos=0, retain=False)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you clear the retained message on the off topic if setting the on topic?

Copy link
Copy Markdown
Contributor Author

@todschmidt todschmidt Apr 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off isn't set to be retained since that is the default if snips restarts Edit: Doh, I see what you mean I didn't have it set before, fixed.

Comment thread homeassistant/components/snips.py Outdated
"""Activate Snips component."""
@asyncio.coroutine
def message_received(topic, payload, qos):
def set_feedback(site_ids, state):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is called from inside async context, plase add @callback decorator and prefix name with async_

Comment thread homeassistant/components/snips.py Outdated
hass.components.mqtt.async_publish(
topic, payload, qos=1, retain=True)

set_feedback(None, config[DOMAIN].get(CONF_FEEDBACK))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should have a default. If no value is given, we should not send any messages. That way it can be controlled by the outside.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, was back and forth there and could see it either way but ok.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, there has to be a better way to do this. I think it should be if nothing is configured we don't send anything, if false is configured we do. But this seems hacky but how to test for not set?

    if not config[DOMAIN].get(CONF_FEEDBACK, 'no_conf') == 'no_conf':
        async_set_feedback(None, config[DOMAIN].get(CONF_FEEDBACK))

return

if (request['intent']['probability']
< config[DOMAIN].get(CONF_PROBABILITY)):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default for probability is 0. So if none is configured, nothing will ever pass? Better to not have a probability default and just check if probability is None or intentProb < probability

Copy link
Copy Markdown
Contributor Author

@todschmidt todschmidt Apr 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probability (editL was: default) is checked for greater than, so 0 matches all. It's a lower limit not upper.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config option defaults to 0.
Snips probability is between 0 and 1.

If not configured, probability defaults to 0.

So you're checking here things like if 0.8 < 0. Which will always be false?

Also, if you know a key exists, prefer to use square brackets over get

Copy link
Copy Markdown
Contributor Author

@todschmidt todschmidt Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.8 < 0 is false yes, so we accept the intent. If I set the default to 1 then we would by default not accept any intents.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see it now. My bad.

Comment thread homeassistant/components/snips.py Outdated
hass.components.mqtt.async_publish(
topic, payload, qos=int(state), retain=state)

if not config[DOMAIN].get(CONF_FEEDBACK, 'no_conf') == 'no_conf':
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is weird… instead try this

if CONF_FEEDBACK in config[DOMAIN]:
    async_set_feedback(None, config[DOMAIN][CONF_FEEDBACK])

Comment thread tests/components/test_snips.py Outdated
events = []

@callback
def record_event(event):
Copy link
Copy Markdown
Member

@balloob balloob Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a helper for this, it's called async_mock_service and can be imported from tests.common

@balloob balloob merged commit e593117 into home-assistant:dev Apr 9, 2018
@balloob balloob mentioned this pull request Apr 27, 2018
Adminiuga pushed a commit to Adminiuga/home-assistant that referenced this pull request Jun 25, 2018
* Added feedback sound configuration

* Added feedback sound configuration

* Cleaned up feedback off

* Cleaned up whitespace

* Moved feedback pus to helper funx

* Async

* Used async_mock_service for tests

* Lint
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants