mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +00:00
Fix Google Calendar caching when offline (#13375)
* Fix Google Calendar caching when offline Events from Google Calendar were not firing under the following circumstances: 1. Start ha as normal with Google Calendar configured as per instructions. 2. ha loses network connectivity to Google 3. ha attempts update of Google Calendar 4. calendar/google component throws uncaught Exception causing update method to not return 5. (cached) Google Calendar event does not fire, remains "Off" Catching the Exception and returning False from the update() method causes the correct behavior (i.e., the calendar component firing the event as scheduled using cached data). * Add requirements * Revert code cleanup * Remove explicit return value from update() * Revert "Remove explicit return value from update()" This reverts commit 7cd77708af658ccea855de47a32ce4ac5262ac30. * Use MockDependency decorator No need to whitelist google-python-api-client for a single unit test at this point.
This commit is contained in:
parent
e1d2d201c8
commit
81cf0dacfe
@ -62,7 +62,14 @@ class GoogleCalendarData(object):
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Get the latest data."""
|
||||
service = self.calendar_service.get()
|
||||
from httplib2 import ServerNotFoundError
|
||||
|
||||
try:
|
||||
service = self.calendar_service.get()
|
||||
except ServerNotFoundError:
|
||||
_LOGGER.warning("Unable to connect to Google, using cached data")
|
||||
return False
|
||||
|
||||
params = dict(DEFAULT_GOOGLE_SEARCH_PARAMS)
|
||||
params['timeMin'] = dt.now().isoformat('T')
|
||||
params['calendarId'] = self.calendar_id
|
||||
|
@ -2,7 +2,7 @@
|
||||
# pylint: disable=protected-access
|
||||
import logging
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
@ -11,7 +11,7 @@ import homeassistant.components.calendar.google as calendar
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.const import CONF_PLATFORM, STATE_OFF, STATE_ON
|
||||
from homeassistant.helpers.template import DATE_STR_FORMAT
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, MockDependency
|
||||
|
||||
TEST_PLATFORM = {calendar_base.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||
|
||||
@ -421,3 +421,16 @@ class TestComponentsGoogleCalendar(unittest.TestCase):
|
||||
'location': event['location'],
|
||||
'description': event['description']
|
||||
})
|
||||
|
||||
@MockDependency("httplib2")
|
||||
def test_update_false(self, mock_httplib2):
|
||||
"""Test that the update returns False upon Error."""
|
||||
mock_service = Mock()
|
||||
mock_service.get = Mock(
|
||||
side_effect=mock_httplib2.ServerNotFoundError("unit test"))
|
||||
|
||||
cal = calendar.GoogleCalendarEventDevice(self.hass, mock_service, None,
|
||||
{'name': "test"})
|
||||
result = cal.data.update()
|
||||
|
||||
self.assertFalse(result)
|
||||
|
Loading…
x
Reference in New Issue
Block a user