Skip to content

Commit

Permalink
Google: Report all states on activating report state (#27312)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Oct 8, 2019
1 parent 4322310 commit c214d7a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
5 changes: 5 additions & 0 deletions homeassistant/components/google_assistant/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ def traits(self):
]
return self._traits

@callback
def should_expose(self):
"""If entity should be exposed."""
return self.config.should_expose(self.state)

@callback
def is_supported(self) -> bool:
"""Return if the entity is supported by Google."""
Expand Down
24 changes: 23 additions & 1 deletion homeassistant/components/google_assistant/report_state.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""Google Report State implementation."""
from homeassistant.core import HomeAssistant, callback
from homeassistant.const import MATCH_ALL
from homeassistant.helpers.event import async_call_later

from .helpers import AbstractConfig, GoogleEntity
from .helpers import AbstractConfig, GoogleEntity, async_get_entities

# Time to wait until the homegraph updates
# https://github.com/actions-on-google/smart-home-nodejs/issues/196#issuecomment-439156639
INITIAL_REPORT_DELAY = 60


@callback
Expand Down Expand Up @@ -34,6 +39,23 @@ async def async_entity_state_listener(changed_entity, old_state, new_state):
{"devices": {"states": {changed_entity: entity_data}}}
)

async_call_later(
hass, INITIAL_REPORT_DELAY, _async_report_all_states(hass, google_config)
)

return hass.helpers.event.async_track_state_change(
MATCH_ALL, async_entity_state_listener
)


async def _async_report_all_states(hass: HomeAssistant, google_config: AbstractConfig):
"""Report all states."""
entities = {}

for entity in async_get_entities(hass, google_config):
if not entity.should_expose():
continue

entities[entity.entity_id] = entity.query_serialize()

await google_config.async_report_state({"devices": {"states": entities}})
31 changes: 26 additions & 5 deletions tests/components/google_assistant/test_report_state.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
"""Test Google report state."""
from unittest.mock import patch

from homeassistant.components.google_assistant.report_state import (
async_enable_report_state,
)
from homeassistant.components.google_assistant import report_state
from homeassistant.util.dt import utcnow

from . import BASIC_CONFIG

from tests.common import mock_coro

from tests.common import mock_coro, async_fire_time_changed


async def test_report_state(hass):
"""Test report state works."""
unsub = async_enable_report_state(hass, BASIC_CONFIG)
hass.states.async_set("light.ceiling", "off")
hass.states.async_set("switch.ac", "on")

with patch.object(
BASIC_CONFIG, "async_report_state", side_effect=mock_coro
) as mock_report, patch.object(report_state, "INITIAL_REPORT_DELAY", 0):
unsub = report_state.async_enable_report_state(hass, BASIC_CONFIG)

async_fire_time_changed(hass, utcnow())
await hass.async_block_till_done()

# Test that enabling report state does a report on all entities
assert len(mock_report.mock_calls) == 1
assert mock_report.mock_calls[0][1][0] == {
"devices": {
"states": {
"light.ceiling": {"on": False, "online": True},
"switch.ac": {"on": True, "online": True},
}
}
}

with patch.object(
BASIC_CONFIG, "async_report_state", side_effect=mock_coro
Expand Down

0 comments on commit c214d7a

Please sign in to comment.