Add icon_type as configuration variable (#36594)

This commit is contained in:
Tony Phan 2020-06-11 17:11:46 +10:00 committed by GitHub
parent 4df186787a
commit afe4647896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,10 +20,12 @@ from . import DOMAIN as LAMETRIC_DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
AVAILABLE_PRIORITIES = ["info", "warning", "critical"] AVAILABLE_PRIORITIES = ["info", "warning", "critical"]
AVAILABLE_ICON_TYPES = ["none", "info", "alert"]
CONF_CYCLES = "cycles" CONF_CYCLES = "cycles"
CONF_LIFETIME = "lifetime" CONF_LIFETIME = "lifetime"
CONF_PRIORITY = "priority" CONF_PRIORITY = "priority"
CONF_ICON_TYPE = "icon_type"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
@ -31,6 +33,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
vol.Optional(CONF_LIFETIME, default=10): cv.positive_int, vol.Optional(CONF_LIFETIME, default=10): cv.positive_int,
vol.Optional(CONF_CYCLES, default=1): cv.positive_int, vol.Optional(CONF_CYCLES, default=1): cv.positive_int,
vol.Optional(CONF_PRIORITY, default="warning"): vol.In(AVAILABLE_PRIORITIES), vol.Optional(CONF_PRIORITY, default="warning"): vol.In(AVAILABLE_PRIORITIES),
vol.Optional(CONF_ICON_TYPE, default="info"): vol.In(AVAILABLE_ICON_TYPES),
} }
) )
@ -44,19 +47,23 @@ def get_service(hass, config, discovery_info=None):
config[CONF_LIFETIME] * 1000, config[CONF_LIFETIME] * 1000,
config[CONF_CYCLES], config[CONF_CYCLES],
config[CONF_PRIORITY], config[CONF_PRIORITY],
config[CONF_ICON_TYPE],
) )
class LaMetricNotificationService(BaseNotificationService): class LaMetricNotificationService(BaseNotificationService):
"""Implement the notification service for LaMetric.""" """Implement the notification service for LaMetric."""
def __init__(self, hasslametricmanager, icon, lifetime, cycles, priority): def __init__(
self, hasslametricmanager, icon, lifetime, cycles, priority, icon_type
):
"""Initialize the service.""" """Initialize the service."""
self.hasslametricmanager = hasslametricmanager self.hasslametricmanager = hasslametricmanager
self._icon = icon self._icon = icon
self._lifetime = lifetime self._lifetime = lifetime
self._cycles = cycles self._cycles = cycles
self._priority = priority self._priority = priority
self._icon_type = icon_type
self._devices = [] self._devices = []
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
@ -69,6 +76,7 @@ class LaMetricNotificationService(BaseNotificationService):
cycles = self._cycles cycles = self._cycles
sound = None sound = None
priority = self._priority priority = self._priority
icon_type = self._icon_type
# Additional data? # Additional data?
if data is not None: if data is not None:
@ -82,6 +90,15 @@ class LaMetricNotificationService(BaseNotificationService):
_LOGGER.error("Sound ID %s unknown, ignoring", data["sound"]) _LOGGER.error("Sound ID %s unknown, ignoring", data["sound"])
if "cycles" in data: if "cycles" in data:
cycles = int(data["cycles"]) cycles = int(data["cycles"])
if "icon_type" in data:
if data["icon_type"] in AVAILABLE_ICON_TYPES:
icon_type = data["icon_type"]
else:
_LOGGER.warning(
"Priority %s invalid, using default %s",
data["priority"],
priority,
)
if "priority" in data: if "priority" in data:
if data["priority"] in AVAILABLE_PRIORITIES: if data["priority"] in AVAILABLE_PRIORITIES:
priority = data["priority"] priority = data["priority"]
@ -91,7 +108,6 @@ class LaMetricNotificationService(BaseNotificationService):
data["priority"], data["priority"],
priority, priority,
) )
text_frame = SimpleFrame(icon, message) text_frame = SimpleFrame(icon, message)
_LOGGER.debug( _LOGGER.debug(
"Icon/Message/Cycles/Lifetime: %s, %s, %d, %d", "Icon/Message/Cycles/Lifetime: %s, %s, %d, %d",
@ -120,7 +136,10 @@ class LaMetricNotificationService(BaseNotificationService):
try: try:
lmn.set_device(dev) lmn.set_device(dev)
lmn.send_notification( lmn.send_notification(
model, lifetime=self._lifetime, priority=priority model,
lifetime=self._lifetime,
priority=priority,
icon_type=icon_type,
) )
_LOGGER.debug("Sent notification to LaMetric %s", dev["name"]) _LOGGER.debug("Sent notification to LaMetric %s", dev["name"])
except OSError: except OSError: