Added cycles config option to LaMetric notifications (#10656)

* Added cycles config option to lametric.py

Added cycles config option, changed display_time to lifetime, code cleanup

* Update lametric.py

* Update lametric.py
This commit is contained in:
frittes 2017-11-18 21:12:16 +01:00 committed by Martin Hjelmare
parent 92fe9aadc8
commit 6ad62a2ccb

View File

@ -20,35 +20,39 @@ DEPENDENCIES = ['lametric']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_DISPLAY_TIME = "display_time" CONF_LIFETIME = "lifetime"
CONF_CYCLES = "cycles"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_ICON, default="i555"): cv.string, vol.Optional(CONF_ICON, default="i555"): cv.string,
vol.Optional(CONF_DISPLAY_TIME, default=10): cv.positive_int, vol.Optional(CONF_LIFETIME, default=10): cv.positive_int,
vol.Optional(CONF_CYCLES, default=1): cv.positive_int,
}) })
# 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 LaMetric notification service."""
hlmn = hass.data.get(LAMETRIC_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_LIFETIME] * 1000,
config[CONF_CYCLES])
class LaMetricNotificationService(BaseNotificationService): class LaMetricNotificationService(BaseNotificationService):
"""Implement the notification service for LaMetric.""" """Implement the notification service for LaMetric."""
def __init__(self, hasslametricmanager, icon, display_time): def __init__(self, hasslametricmanager, icon, lifetime, cycles):
"""Initialize the service.""" """Initialize the service."""
self.hasslametricmanager = hasslametricmanager self.hasslametricmanager = hasslametricmanager
self._icon = icon self._icon = icon
self._display_time = display_time self._lifetime = lifetime
self._cycles = cycles
# pylint: disable=broad-except # pylint: disable=broad-except
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 device."""
from lmnotify import SimpleFrame, Sound, Model from lmnotify import SimpleFrame, Sound, Model
from oauthlib.oauth2 import TokenExpiredError from oauthlib.oauth2 import TokenExpiredError
@ -56,9 +60,10 @@ class LaMetricNotificationService(BaseNotificationService):
data = kwargs.get(ATTR_DATA) data = kwargs.get(ATTR_DATA)
_LOGGER.debug("Targets/Data: %s/%s", targets, data) _LOGGER.debug("Targets/Data: %s/%s", targets, data)
icon = self._icon icon = self._icon
cycles = self._cycles
sound = None sound = None
# User-defined icon? # Additional data?
if data is not None: if data is not None:
if "icon" in data: if "icon" in data:
icon = data["icon"] icon = data["icon"]
@ -73,12 +78,12 @@ class LaMetricNotificationService(BaseNotificationService):
data["sound"]) data["sound"])
text_frame = SimpleFrame(icon, message) text_frame = SimpleFrame(icon, message)
_LOGGER.debug("Icon/Message/Duration: %s, %s, %d", _LOGGER.debug("Icon/Message/Cycles/Lifetime: %s, %s, %d, %d",
icon, message, self._display_time) icon, message, self._cycles, self._lifetime)
frames = [text_frame] frames = [text_frame]
model = Model(frames=frames, sound=sound) model = Model(frames=frames, cycles=cycles, sound=sound)
lmn = self.hasslametricmanager.manager lmn = self.hasslametricmanager.manager
try: try:
devices = lmn.get_devices() devices = lmn.get_devices()
@ -89,5 +94,5 @@ class LaMetricNotificationService(BaseNotificationService):
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._lifetime)
_LOGGER.debug("Sent notification to LaMetric %s", dev["name"]) _LOGGER.debug("Sent notification to LaMetric %s", dev["name"])