Skip to content

Commit

Permalink
bugfix: ensure the google_assistant component respects `allow_unloc…
Browse files Browse the repository at this point in the history
…k` (#18874)

The `Config` object specific to the `google_assistant` component
had a default value for `allow_unlock`. We were not overriding this
default when constructing the Config object during `google_assistant`
component setup, whereas we do when setting up the `cloud` component.

To fix, we thread the `allow_unlock` parameter down through http setup,
and ensure that it's set correctly. Moreover, we also change the
ordering of the `Config` parameters, and remove the default. Future
refactoring should not miss it, as it is now a required parameter.
  • Loading branch information
ahayworth authored and balloob committed Dec 2, 2018
1 parent a62fc7c commit b7e2522
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ def should_expose(entity):

self._gactions_config = ga_h.Config(
should_expose=should_expose,
allow_unlock=self.prefs.google_allow_unlock,
agent_user_id=self.claims['cognito:username'],
entity_config=conf.get(CONF_ENTITY_CONFIG),
allow_unlock=self.prefs.google_allow_unlock,
)

return self._gactions_config
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/google_assistant/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def __init__(self, code, msg):
class Config:
"""Hold the configuration for Google Assistant."""

def __init__(self, should_expose, agent_user_id, entity_config=None,
allow_unlock=False):
def __init__(self, should_expose, allow_unlock, agent_user_id,
entity_config=None):
"""Initialize the configuration."""
self.should_expose = should_expose
self.agent_user_id = agent_user_id
Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/google_assistant/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .const import (
GOOGLE_ASSISTANT_API_ENDPOINT,
CONF_ALLOW_UNLOCK,
CONF_EXPOSE_BY_DEFAULT,
CONF_EXPOSED_DOMAINS,
CONF_ENTITY_CONFIG,
Expand All @@ -32,6 +33,7 @@ def async_register_http(hass, cfg):
expose_by_default = cfg.get(CONF_EXPOSE_BY_DEFAULT)
exposed_domains = cfg.get(CONF_EXPOSED_DOMAINS)
entity_config = cfg.get(CONF_ENTITY_CONFIG) or {}
allow_unlock = cfg.get(CONF_ALLOW_UNLOCK, False)

def is_exposed(entity) -> bool:
"""Determine if an entity should be exposed to Google Assistant."""
Expand All @@ -57,7 +59,7 @@ def is_exposed(entity) -> bool:
return is_default_exposed or explicit_expose

hass.http.register_view(
GoogleAssistantView(is_exposed, entity_config))
GoogleAssistantView(is_exposed, entity_config, allow_unlock))


class GoogleAssistantView(HomeAssistantView):
Expand All @@ -67,15 +69,17 @@ class GoogleAssistantView(HomeAssistantView):
name = 'api:google_assistant'
requires_auth = True

def __init__(self, is_exposed, entity_config):
def __init__(self, is_exposed, entity_config, allow_unlock):
"""Initialize the Google Assistant request handler."""
self.is_exposed = is_exposed
self.entity_config = entity_config
self.allow_unlock = allow_unlock

async def post(self, request: Request) -> Response:
"""Handle Google Assistant requests."""
message = await request.json() # type: dict
config = Config(self.is_exposed,
self.allow_unlock,
request['hass_user'].id,
self.entity_config)
result = await async_handle_message(
Expand Down
2 changes: 2 additions & 0 deletions tests/components/google_assistant/test_smart_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

BASIC_CONFIG = helpers.Config(
should_expose=lambda state: True,
allow_unlock=False,
agent_user_id='test-agent',
)
REQ_ID = 'ff36a3cc-ec34-11e6-b1a0-64510650abcf'
Expand All @@ -35,6 +36,7 @@ async def test_sync_message(hass):

config = helpers.Config(
should_expose=lambda state: state.entity_id != 'light.not_expose',
allow_unlock=False,
agent_user_id='test-agent',
entity_config={
'light.demo_light': {
Expand Down
1 change: 1 addition & 0 deletions tests/components/google_assistant/test_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

BASIC_CONFIG = helpers.Config(
should_expose=lambda state: True,
allow_unlock=False,
agent_user_id='test-agent',
)

Expand Down

0 comments on commit b7e2522

Please sign in to comment.