Fix and clean lametric (#10391)

* Fix and clean lametric

* Add missing DEPENDENCIES in notify platform.
* Remove not needed method in component manager class.
* Don't overwrite notify DOMAIN.
* Return consistently depending on found devices in setup of component.

* Get new token if token expired

* Add debug log for getting new token

* Clean up
This commit is contained in:
Martin Hjelmare authored and GitHub committed 2017-11-13 21:12:15 +01:00
1 parent 6974f2366d
commit 3c135deec8
2 files changed
+19 -16

No files matched your search

+12 -5
View File
@@ -13,9 +13,10 @@ from homeassistant.components.notify import (
from homeassistant.const import CONF_ICON
import homeassistant.helpers.config_validation as cv
from homeassistant.components.lametric import DOMAIN
from homeassistant.components.lametric import DOMAIN as LAMETRIC_DOMAIN
REQUIREMENTS = ['lmnotify==0.0.4']
DEPENDENCIES = ['lametric']
_LOGGER = logging.getLogger(__name__)
@@ -30,7 +31,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
# pylint: disable=unused-variable
def get_service(hass, config, discovery_info=None):
"""Get the Slack notification service."""
hlmn = hass.data.get(DOMAIN)
hlmn = hass.data.get(LAMETRIC_DOMAIN)
return LaMetricNotificationService(hlmn,
config[CONF_ICON],
config[CONF_DISPLAY_TIME] * 1000)
@@ -49,6 +50,7 @@ class LaMetricNotificationService(BaseNotificationService):
def send_message(self, message="", **kwargs):
"""Send a message to some LaMetric deviced."""
from lmnotify import SimpleFrame, Sound, Model
from oauthlib.oauth2 import TokenExpiredError
targets = kwargs.get(ATTR_TARGET)
data = kwargs.get(ATTR_DATA)
@@ -82,10 +84,15 @@ class LaMetricNotificationService(BaseNotificationService):
_LOGGER.debug(frames)
model = Model(frames=frames)
lmn = self.hasslametricmanager.manager()
devices = lmn.get_devices()
lmn = self.hasslametricmanager.manager
try:
devices = lmn.get_devices()
except TokenExpiredError:
_LOGGER.debug("Token expired, fetching new token")
lmn.get_token()
devices = lmn.get_devices()
for dev in devices:
if (targets is None) or (dev["name"] in targets):
if targets is None or dev["name"] in targets:
lmn.set_device(dev)
lmn.send_notification(model, lifetime=self._display_time)
_LOGGER.debug("Sent notification to LaMetric %s", dev["name"])