From 23f06b0040649e41dc4ff3fcbf509455b90fa7e8 Mon Sep 17 00:00:00 2001 From: Philip Rosenberg-Watt Date: Fri, 23 Mar 2018 11:10:52 -0600 Subject: [PATCH] Cache LaMetric devices for offline use (#13379) If the connection to the LaMetric server fails, we should still be able to send notifications to known and reachable devices. --- homeassistant/components/notify/lametric.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/notify/lametric.py b/homeassistant/components/notify/lametric.py index f4c9c391408..895ffd9db10 100644 --- a/homeassistant/components/notify/lametric.py +++ b/homeassistant/components/notify/lametric.py @@ -6,6 +6,7 @@ https://home-assistant.io/components/notify.lametric/ """ import logging +from requests.exceptions import ConnectionError as RequestsConnectionError import voluptuous as vol from homeassistant.components.notify import ( @@ -49,6 +50,7 @@ class LaMetricNotificationService(BaseNotificationService): self._icon = icon self._lifetime = lifetime self._cycles = cycles + self._devices = [] # pylint: disable=broad-except def send_message(self, message="", **kwargs): @@ -86,12 +88,15 @@ class LaMetricNotificationService(BaseNotificationService): model = Model(frames=frames, cycles=cycles, sound=sound) lmn = self.hasslametricmanager.manager try: - devices = lmn.get_devices() + self._devices = lmn.get_devices() except TokenExpiredError: _LOGGER.debug("Token expired, fetching new token") lmn.get_token() - devices = lmn.get_devices() - for dev in devices: + self._devices = lmn.get_devices() + except RequestsConnectionError: + _LOGGER.warning("Problem connecting to LaMetric, " + "using cached devices instead") + for dev in self._devices: if targets is None or dev["name"] in targets: try: lmn.set_device(dev)