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.
This commit is contained in:
parent
5ec6f25d4e
commit
23f06b0040
1 changed files with 8 additions and 3 deletions
|
@ -6,6 +6,7 @@ https://home-assistant.io/components/notify.lametric/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from requests.exceptions import ConnectionError as RequestsConnectionError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
|
@ -49,6 +50,7 @@ class LaMetricNotificationService(BaseNotificationService):
|
||||||
self._icon = icon
|
self._icon = icon
|
||||||
self._lifetime = lifetime
|
self._lifetime = lifetime
|
||||||
self._cycles = cycles
|
self._cycles = cycles
|
||||||
|
self._devices = []
|
||||||
|
|
||||||
# pylint: disable=broad-except
|
# pylint: disable=broad-except
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
|
@ -86,12 +88,15 @@ class LaMetricNotificationService(BaseNotificationService):
|
||||||
model = Model(frames=frames, cycles=cycles, sound=sound)
|
model = Model(frames=frames, cycles=cycles, sound=sound)
|
||||||
lmn = self.hasslametricmanager.manager
|
lmn = self.hasslametricmanager.manager
|
||||||
try:
|
try:
|
||||||
devices = lmn.get_devices()
|
self._devices = lmn.get_devices()
|
||||||
except TokenExpiredError:
|
except TokenExpiredError:
|
||||||
_LOGGER.debug("Token expired, fetching new token")
|
_LOGGER.debug("Token expired, fetching new token")
|
||||||
lmn.get_token()
|
lmn.get_token()
|
||||||
devices = lmn.get_devices()
|
self._devices = lmn.get_devices()
|
||||||
for dev in 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:
|
if targets is None or dev["name"] in targets:
|
||||||
try:
|
try:
|
||||||
lmn.set_device(dev)
|
lmn.set_device(dev)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue