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 2017-11-13 21:12:15 +01:00 committed by GitHub
parent 6974f2366d
commit 3c135deec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 16 deletions

View File

@ -38,15 +38,16 @@ def setup(hass, config):
conf = config[DOMAIN] conf = config[DOMAIN]
hlmn = HassLaMetricManager(client_id=conf[CONF_CLIENT_ID], hlmn = HassLaMetricManager(client_id=conf[CONF_CLIENT_ID],
client_secret=conf[CONF_CLIENT_SECRET]) client_secret=conf[CONF_CLIENT_SECRET])
devices = hlmn.manager().get_devices() devices = hlmn.manager.get_devices()
if not devices:
_LOGGER.error("No LaMetric devices found")
return False
found = False
hass.data[DOMAIN] = hlmn hass.data[DOMAIN] = hlmn
for dev in devices: for dev in devices:
_LOGGER.debug("Discovered LaMetric device: %s", dev) _LOGGER.debug("Discovered LaMetric device: %s", dev)
found = True
return found return True
class HassLaMetricManager(): class HassLaMetricManager():
@ -63,7 +64,7 @@ class HassLaMetricManager():
from lmnotify import LaMetricManager from lmnotify import LaMetricManager
_LOGGER.debug("Connecting to LaMetric") _LOGGER.debug("Connecting to LaMetric")
self.lmn = LaMetricManager(client_id, client_secret) self.manager = LaMetricManager(client_id, client_secret)
self._client_id = client_id self._client_id = client_id
self._client_secret = client_secret self._client_secret = client_secret
@ -75,9 +76,4 @@ class HassLaMetricManager():
""" """
from lmnotify import LaMetricManager from lmnotify import LaMetricManager
_LOGGER.debug("Reconnecting to LaMetric") _LOGGER.debug("Reconnecting to LaMetric")
self.lmn = LaMetricManager(self._client_id, self.manager = LaMetricManager(self._client_id, self._client_secret)
self._client_secret)
def manager(self):
"""Return the global LaMetricManager instance."""
return self.lmn

View File

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