Introduce const file in LaMetric (#66929)

This commit is contained in:
Franck Nijhof 2022-02-20 11:47:36 +01:00 committed by GitHub
parent 94a0f1c7b3
commit ddedaf6f70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 34 deletions

View file

@ -1,6 +1,4 @@
"""Support for LaMetric time.""" """Support for LaMetric time."""
import logging
from lmnotify import LaMetricManager from lmnotify import LaMetricManager
import voluptuous as vol import voluptuous as vol
@ -9,12 +7,7 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__) from .const import DOMAIN, LOGGER
DOMAIN = "lametric"
LAMETRIC_DEVICES = "LAMETRIC_DEVICES"
CONFIG_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema(
{ {
@ -31,18 +24,18 @@ CONFIG_SCHEMA = vol.Schema(
def setup(hass: HomeAssistant, config: ConfigType) -> bool: def setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the LaMetricManager.""" """Set up the LaMetricManager."""
_LOGGER.debug("Setting up LaMetric platform") LOGGER.debug("Setting up LaMetric platform")
conf = config[DOMAIN] conf = config[DOMAIN]
hlmn = HassLaMetricManager( hlmn = HassLaMetricManager(
client_id=conf[CONF_CLIENT_ID], client_secret=conf[CONF_CLIENT_SECRET] client_id=conf[CONF_CLIENT_ID], client_secret=conf[CONF_CLIENT_SECRET]
) )
if not (devices := hlmn.manager.get_devices()): if not (devices := hlmn.manager.get_devices()):
_LOGGER.error("No LaMetric devices found") LOGGER.error("No LaMetric devices found")
return False return 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)
return True return True
@ -53,7 +46,7 @@ class HassLaMetricManager:
def __init__(self, client_id: str, client_secret: str) -> None: def __init__(self, client_id: str, client_secret: str) -> None:
"""Initialize HassLaMetricManager and connect to LaMetric.""" """Initialize HassLaMetricManager and connect to LaMetric."""
_LOGGER.debug("Connecting to LaMetric") LOGGER.debug("Connecting to LaMetric")
self.manager = 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

View file

@ -0,0 +1,16 @@
"""Constants for the LaMetric integration."""
import logging
from typing import Final
DOMAIN: Final = "lametric"
LOGGER = logging.getLogger(__package__)
AVAILABLE_PRIORITIES: Final = ["info", "warning", "critical"]
AVAILABLE_ICON_TYPES: Final = ["none", "info", "alert"]
CONF_CYCLES: Final = "cycles"
CONF_LIFETIME: Final = "lifetime"
CONF_PRIORITY: Final = "priority"
CONF_ICON_TYPE: Final = "icon_type"

View file

@ -1,7 +1,6 @@
"""Support for LaMetric notifications.""" """Support for LaMetric notifications."""
from __future__ import annotations from __future__ import annotations
import logging
from typing import Any from typing import Any
from lmnotify import Model, SimpleFrame, Sound from lmnotify import Model, SimpleFrame, Sound
@ -20,17 +19,17 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN, HassLaMetricManager from . import HassLaMetricManager
from .const import (
_LOGGER = logging.getLogger(__name__) AVAILABLE_ICON_TYPES,
AVAILABLE_PRIORITIES,
AVAILABLE_PRIORITIES = ["info", "warning", "critical"] CONF_CYCLES,
AVAILABLE_ICON_TYPES = ["none", "info", "alert"] CONF_ICON_TYPE,
CONF_LIFETIME,
CONF_CYCLES = "cycles" CONF_PRIORITY,
CONF_LIFETIME = "lifetime" DOMAIN,
CONF_PRIORITY = "priority" LOGGER,
CONF_ICON_TYPE = "icon_type" )
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
@ -85,7 +84,7 @@ class LaMetricNotificationService(BaseNotificationService):
targets = kwargs.get(ATTR_TARGET) targets = kwargs.get(ATTR_TARGET)
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 cycles = self._cycles
sound = None sound = None
@ -99,16 +98,16 @@ class LaMetricNotificationService(BaseNotificationService):
if "sound" in data: if "sound" in data:
try: try:
sound = Sound(category="notifications", sound_id=data["sound"]) sound = Sound(category="notifications", sound_id=data["sound"])
_LOGGER.debug("Adding notification sound %s", data["sound"]) LOGGER.debug("Adding notification sound %s", data["sound"])
except AssertionError: except AssertionError:
_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 "icon_type" in data:
if data["icon_type"] in AVAILABLE_ICON_TYPES: if data["icon_type"] in AVAILABLE_ICON_TYPES:
icon_type = data["icon_type"] icon_type = data["icon_type"]
else: else:
_LOGGER.warning( LOGGER.warning(
"Priority %s invalid, using default %s", "Priority %s invalid, using default %s",
data["priority"], data["priority"],
priority, priority,
@ -117,13 +116,13 @@ class LaMetricNotificationService(BaseNotificationService):
if data["priority"] in AVAILABLE_PRIORITIES: if data["priority"] in AVAILABLE_PRIORITIES:
priority = data["priority"] priority = data["priority"]
else: else:
_LOGGER.warning( LOGGER.warning(
"Priority %s invalid, using default %s", "Priority %s invalid, using default %s",
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",
icon, icon,
message, message,
@ -138,11 +137,11 @@ class LaMetricNotificationService(BaseNotificationService):
try: try:
self._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()
self._devices = lmn.get_devices() self._devices = lmn.get_devices()
except RequestsConnectionError: except RequestsConnectionError:
_LOGGER.warning( LOGGER.warning(
"Problem connecting to LaMetric, using cached devices instead" "Problem connecting to LaMetric, using cached devices instead"
) )
for dev in self._devices: for dev in self._devices:
@ -155,6 +154,6 @@ class LaMetricNotificationService(BaseNotificationService):
priority=priority, priority=priority,
icon_type=icon_type, 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:
_LOGGER.warning("Cannot connect to LaMetric %s", dev["name"]) LOGGER.warning("Cannot connect to LaMetric %s", dev["name"])