Google: catch query not supported (#27559)

This commit is contained in:
Paulus Schoutsen 2019-10-12 14:07:01 -07:00 committed by GitHub
parent 6c947f58b8
commit 9e121b785a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -1,15 +1,21 @@
"""Google Report State implementation.""" """Google Report State implementation."""
import logging
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.const import MATCH_ALL from homeassistant.const import MATCH_ALL
from homeassistant.helpers.event import async_call_later from homeassistant.helpers.event import async_call_later
from .helpers import AbstractConfig, GoogleEntity, async_get_entities from .helpers import AbstractConfig, GoogleEntity, async_get_entities
from .error import SmartHomeError
# Time to wait until the homegraph updates # Time to wait until the homegraph updates
# https://github.com/actions-on-google/smart-home-nodejs/issues/196#issuecomment-439156639 # https://github.com/actions-on-google/smart-home-nodejs/issues/196#issuecomment-439156639
INITIAL_REPORT_DELAY = 60 INITIAL_REPORT_DELAY = 60
_LOGGER = logging.getLogger(__name__)
@callback @callback
def async_enable_report_state(hass: HomeAssistant, google_config: AbstractConfig): def async_enable_report_state(hass: HomeAssistant, google_config: AbstractConfig):
"""Enable state reporting.""" """Enable state reporting."""
@ -26,7 +32,11 @@ def async_enable_report_state(hass: HomeAssistant, google_config: AbstractConfig
if not entity.is_supported(): if not entity.is_supported():
return return
entity_data = entity.query_serialize() try:
entity_data = entity.query_serialize()
except SmartHomeError as err:
_LOGGER.debug("Not reporting state for %s: %s", changed_entity, err.code)
return
if old_state: if old_state:
old_entity = GoogleEntity(hass, google_config, old_state) old_entity = GoogleEntity(hass, google_config, old_state)

View File

@ -1,7 +1,7 @@
"""Test Google report state.""" """Test Google report state."""
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components.google_assistant import report_state from homeassistant.components.google_assistant import report_state, error
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from . import BASIC_CONFIG from . import BASIC_CONFIG
@ -10,7 +10,7 @@ from . import BASIC_CONFIG
from tests.common import mock_coro, async_fire_time_changed from tests.common import mock_coro, async_fire_time_changed
async def test_report_state(hass): async def test_report_state(hass, caplog):
"""Test report state works.""" """Test report state works."""
hass.states.async_set("light.ceiling", "off") hass.states.async_set("light.ceiling", "off")
hass.states.async_set("switch.ac", "on") hass.states.async_set("switch.ac", "on")
@ -57,6 +57,19 @@ async def test_report_state(hass):
assert len(mock_report.mock_calls) == 0 assert len(mock_report.mock_calls) == 0
# Test that entities that we can't query don't report a state
with patch.object(
BASIC_CONFIG, "async_report_state", side_effect=mock_coro
) as mock_report, patch(
"homeassistant.components.google_assistant.report_state.GoogleEntity.query_serialize",
side_effect=error.SmartHomeError("mock-error", "mock-msg"),
):
hass.states.async_set("light.kitchen", "off")
await hass.async_block_till_done()
assert "Not reporting state for light.kitchen: mock-error"
assert len(mock_report.mock_calls) == 0
unsub() unsub()
with patch.object( with patch.object(