Introduce const file in LaMetric (#66929)

pull/66931/head
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."""
import logging
from lmnotify import LaMetricManager
import voluptuous as vol
@ -9,12 +7,7 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__)
DOMAIN = "lametric"
LAMETRIC_DEVICES = "LAMETRIC_DEVICES"
from .const import DOMAIN, LOGGER
CONFIG_SCHEMA = vol.Schema(
{
@ -31,18 +24,18 @@ CONFIG_SCHEMA = vol.Schema(
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the LaMetricManager."""
_LOGGER.debug("Setting up LaMetric platform")
LOGGER.debug("Setting up LaMetric platform")
conf = config[DOMAIN]
hlmn = HassLaMetricManager(
client_id=conf[CONF_CLIENT_ID], client_secret=conf[CONF_CLIENT_SECRET]
)
if not (devices := hlmn.manager.get_devices()):
_LOGGER.error("No LaMetric devices found")
LOGGER.error("No LaMetric devices found")
return False
hass.data[DOMAIN] = hlmn
for dev in devices:
_LOGGER.debug("Discovered LaMetric device: %s", dev)
LOGGER.debug("Discovered LaMetric device: %s", dev)
return True
@ -53,7 +46,7 @@ class HassLaMetricManager:
def __init__(self, client_id: str, client_secret: str) -> None:
"""Initialize HassLaMetricManager and connect to LaMetric."""
_LOGGER.debug("Connecting to LaMetric")
LOGGER.debug("Connecting to LaMetric")
self.manager = LaMetricManager(client_id, client_secret)
self._client_id = client_id
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."""
from __future__ import annotations
import logging
from typing import Any
from lmnotify import Model, SimpleFrame, Sound
@ -20,17 +19,17 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN, HassLaMetricManager
_LOGGER = logging.getLogger(__name__)
AVAILABLE_PRIORITIES = ["info", "warning", "critical"]
AVAILABLE_ICON_TYPES = ["none", "info", "alert"]
CONF_CYCLES = "cycles"
CONF_LIFETIME = "lifetime"
CONF_PRIORITY = "priority"
CONF_ICON_TYPE = "icon_type"
from . import HassLaMetricManager
from .const import (
AVAILABLE_ICON_TYPES,
AVAILABLE_PRIORITIES,
CONF_CYCLES,
CONF_ICON_TYPE,
CONF_LIFETIME,
CONF_PRIORITY,
DOMAIN,
LOGGER,
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
@ -85,7 +84,7 @@ class LaMetricNotificationService(BaseNotificationService):
targets = kwargs.get(ATTR_TARGET)
data = kwargs.get(ATTR_DATA)
_LOGGER.debug("Targets/Data: %s/%s", targets, data)
LOGGER.debug("Targets/Data: %s/%s", targets, data)
icon = self._icon
cycles = self._cycles
sound = None
@ -99,16 +98,16 @@ class LaMetricNotificationService(BaseNotificationService):
if "sound" in data:
try:
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:
_LOGGER.error("Sound ID %s unknown, ignoring", data["sound"])
LOGGER.error("Sound ID %s unknown, ignoring", data["sound"])
if "cycles" in data:
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(
LOGGER.warning(
"Priority %s invalid, using default %s",
data["priority"],
priority,
@ -117,13 +116,13 @@ class LaMetricNotificationService(BaseNotificationService):
if data["priority"] in AVAILABLE_PRIORITIES:
priority = data["priority"]
else:
_LOGGER.warning(
LOGGER.warning(
"Priority %s invalid, using default %s",
data["priority"],
priority,
)
text_frame = SimpleFrame(icon, message)
_LOGGER.debug(
LOGGER.debug(
"Icon/Message/Cycles/Lifetime: %s, %s, %d, %d",
icon,
message,
@ -138,11 +137,11 @@ class LaMetricNotificationService(BaseNotificationService):
try:
self._devices = lmn.get_devices()
except TokenExpiredError:
_LOGGER.debug("Token expired, fetching new token")
LOGGER.debug("Token expired, fetching new token")
lmn.get_token()
self._devices = lmn.get_devices()
except RequestsConnectionError:
_LOGGER.warning(
LOGGER.warning(
"Problem connecting to LaMetric, using cached devices instead"
)
for dev in self._devices:
@ -155,6 +154,6 @@ class LaMetricNotificationService(BaseNotificationService):
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:
_LOGGER.warning("Cannot connect to LaMetric %s", dev["name"])
LOGGER.warning("Cannot connect to LaMetric %s", dev["name"])