8000 Introduce const file in LaMetric by frenck · Pull Request #66929 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Introduce const file in LaMetric #66929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions homeassistant/components/lametric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Support for LaMetric time."""
import logging

from lmnotify import LaMetricManager
import voluptuous as vol

Expand All @@ -9,12 +7,7 @@
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(
{
Expand All @@ -31,18 +24,18 @@

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

Expand All @@ -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
16 changes: 16 additions & 0 deletions homeassistant/components/lametric/const.py
Original file line number Diff line number Diff line change
@@ -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"
43 changes: 21 additions & 22 deletions homeassistant/components/lametric/notify.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Support for LaMetric notifications."""
from __future__ import annotations

import logging
from typing import Any

from lmnotify import Model, SimpleFrame, Sound
Expand All @@ -20,17 +19,17 @@
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(
{
Expand Down Expand Up @@ -85,7 +84,7 @@ def send_message(self, message: str = "", **kwargs: Any) -> None:

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
Expand All @@ -99,16 +98,16 @@ def send_message(self, message: str = "", **kwargs: Any) -> None:
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,
Expand All @@ -117,13 +116,13 @@ def send_message(self, message: str = "", **kwargs: Any) -> None:
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,
Expand All @@ -138,11 +137,11 @@ def send_message(self, message: str = "", **kwargs: Any) -> None:
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:
Expand All @@ -155,6 +154,6 @@ def send_message(self, message: str = "", **kwargs: Any) -> None:
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"])
0