Skip to content

Commit

Permalink
Remove previously deprecated environment variables (#857)
Browse files Browse the repository at this point in the history
* remove previously deprecated environment variables

* more

* Linting

* linting

* Linting
  • Loading branch information
bachya authored Jan 19, 2024
1 parent 9a7cfc0 commit 9ff6994
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 143 deletions.
55 changes: 5 additions & 50 deletions ecowitt2mqtt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,13 @@
ENV_PRECISION,
ENV_RAW_DATA,
ENV_VERBOSE,
LEGACY_ENV_ENDPOINT,
LEGACY_ENV_HASS_DISCOVERY,
LEGACY_ENV_HASS_DISCOVERY_PREFIX,
LEGACY_ENV_HASS_ENTITY_ID_PREFIX,
LEGACY_ENV_INPUT_UNIT_SYSTEM,
LEGACY_ENV_LOG_LEVEL,
LEGACY_ENV_MQTT_BROKER,
LEGACY_ENV_MQTT_PASSWORD,
LEGACY_ENV_MQTT_PORT,
LEGACY_ENV_MQTT_TOPIC,
LEGACY_ENV_MQTT_USERNAME,
LEGACY_ENV_OUTPUT_UNIT_SYSTEM,
LEGACY_ENV_PORT,
LEGACY_ENV_RAW_DATA,
LOGGER,
UnitSystem,
__version__,
)
from ecowitt2mqtt.core import Ecowitt
from ecowitt2mqtt.helpers.calculator.battery import BatteryStrategy
from ecowitt2mqtt.helpers.server import InputDataFormat

DEPRECATED_ENV_VAR_MAP = {
LEGACY_ENV_ENDPOINT: ENV_ENDPOINT,
LEGACY_ENV_HASS_DISCOVERY: ENV_HASS_DISCOVERY,
LEGACY_ENV_HASS_DISCOVERY_PREFIX: ENV_HASS_DISCOVERY_PREFIX,
LEGACY_ENV_HASS_ENTITY_ID_PREFIX: ENV_HASS_ENTITY_ID_PREFIX,
LEGACY_ENV_INPUT_UNIT_SYSTEM: ENV_INPUT_UNIT_SYSTEM,
LEGACY_ENV_LOG_LEVEL: ENV_VERBOSE,
LEGACY_ENV_MQTT_BROKER: ENV_MQTT_BROKER,
LEGACY_ENV_MQTT_PASSWORD: ENV_MQTT_PASSWORD,
LEGACY_ENV_MQTT_PORT: ENV_MQTT_PORT,
LEGACY_ENV_MQTT_TOPIC: ENV_MQTT_TOPIC,
LEGACY_ENV_MQTT_USERNAME: ENV_MQTT_USERNAME,
LEGACY_ENV_OUTPUT_UNIT_SYSTEM: ENV_OUTPUT_UNIT_SYSTEM,
LEGACY_ENV_PORT: ENV_PORT,
LEGACY_ENV_RAW_DATA: ENV_RAW_DATA,
}

ENV_VAR_TO_CONF_MAP = {
ENV_BATTERY_OVERRIDES: CONF_BATTERY_OVERRIDES,
ENV_CONFIG: CONF_CONFIG,
Expand Down Expand Up @@ -157,24 +125,11 @@ def get_env_vars() -> dict[str, str]:
Returns:
A dictionary of environment variables to config options.
"""
env_vars = {}

for env_var in ENV_VAR_TO_CONF_MAP | DEPRECATED_ENV_VAR_MAP:
if (env_var_value := os.getenv(env_var)) is None:
continue

if (replacement_env_var := DEPRECATED_ENV_VAR_MAP.get(env_var)) is not None:
LOGGER.warning(
"Environment variable %s is deprecated; use %s instead",
env_var,
replacement_env_var,
)
env_var = replacement_env_var

config_option = ENV_VAR_TO_CONF_MAP[env_var]
env_vars[config_option] = env_var_value

return env_vars
return {
config_option: env_var_value
for env_var, config_option in ENV_VAR_TO_CONF_MAP.items()
if (env_var_value := os.getenv(env_var)) is not None
}


def get_cli_arguments(args: list[str]) -> dict[str, Any]:
Expand Down
16 changes: 0 additions & 16 deletions ecowitt2mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,6 @@
ENV_RAW_DATA: Final = "ECOWITT2MQTT_RAW_DATA"
ENV_VERBOSE: Final = "ECOWITT2MQTT_VERBOSE"

# Legacy environment variables that will be deprecated at some point:
LEGACY_ENV_ENDPOINT: Final = "ENDPOINT"
LEGACY_ENV_HASS_DISCOVERY: Final = "HASS_DISCOVERY"
LEGACY_ENV_HASS_DISCOVERY_PREFIX: Final = "HASS_DISCOVERY_PREFIX"
LEGACY_ENV_HASS_ENTITY_ID_PREFIX: Final = "HASS_ENTITY_ID_PREFIX"
LEGACY_ENV_INPUT_UNIT_SYSTEM: Final = "INPUT_UNIT_SYSTEM"
LEGACY_ENV_LOG_LEVEL: Final = "LOG_LEVEL"
LEGACY_ENV_MQTT_BROKER: Final = "MQTT_BROKER"
LEGACY_ENV_MQTT_PASSWORD: Final = "MQTT_PASSWORD"
LEGACY_ENV_MQTT_PORT: Final = "MQTT_PORT"
LEGACY_ENV_MQTT_TOPIC: Final = "MQTT_TOPIC"
LEGACY_ENV_MQTT_USERNAME: Final = "MQTT_USERNAME"
LEGACY_ENV_OUTPUT_UNIT_SYSTEM: Final = "OUTPUT_UNIT_SYSTEM"
LEGACY_ENV_PORT: Final = "PORT"
LEGACY_ENV_RAW_DATA: Final = "RAW_DATA"


# Unit systems:
class UnitSystem(StrEnum):
Expand Down
5 changes: 2 additions & 3 deletions ecowitt2mqtt/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from __future__ import annotations

import logging
import os
import sys
from typing import Any

import colorlog

from ecowitt2mqtt.config import ConfigError, Configs
from ecowitt2mqtt.const import LEGACY_ENV_LOG_LEVEL, LOGGER, __version__
from ecowitt2mqtt.const import LOGGER, __version__
from ecowitt2mqtt.runtime import Runtime


Expand All @@ -19,7 +18,7 @@ def configure_logging(verbose: bool) -> None:
Args:
verbose: Whether verbose logging should be included.
"""
if verbose or os.getenv(LEGACY_ENV_LOG_LEVEL):
if verbose:
log_level = logging.DEBUG
else:
log_level = logging.INFO
Expand Down
75 changes: 1 addition & 74 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,15 @@
from __future__ import annotations

import os
from unittest.mock import Mock, patch

import pytest
from unittest.mock import patch

from ecowitt2mqtt.__main__ import get_cli_arguments, get_env_vars, main
from ecowitt2mqtt.const import (
CONF_MQTT_BROKER,
CONF_MQTT_TOPIC,
CONF_VERBOSE,
ENV_ENDPOINT,
ENV_HASS_DISCOVERY,
ENV_HASS_DISCOVERY_PREFIX,
ENV_HASS_ENTITY_ID_PREFIX,
ENV_INPUT_UNIT_SYSTEM,
ENV_MQTT_BROKER,
ENV_MQTT_PASSWORD,
ENV_MQTT_PORT,
ENV_MQTT_TOPIC,
ENV_MQTT_USERNAME,
ENV_OUTPUT_UNIT_SYSTEM,
ENV_PORT,
ENV_RAW_DATA,
ENV_VERBOSE,
LEGACY_ENV_ENDPOINT,
LEGACY_ENV_HASS_DISCOVERY,
LEGACY_ENV_HASS_DISCOVERY_PREFIX,
LEGACY_ENV_HASS_ENTITY_ID_PREFIX,
LEGACY_ENV_INPUT_UNIT_SYSTEM,
LEGACY_ENV_LOG_LEVEL,
LEGACY_ENV_MQTT_BROKER,
LEGACY_ENV_MQTT_PASSWORD,
LEGACY_ENV_MQTT_PORT,
LEGACY_ENV_MQTT_TOPIC,
LEGACY_ENV_MQTT_USERNAME,
LEGACY_ENV_OUTPUT_UNIT_SYSTEM,
LEGACY_ENV_PORT,
LEGACY_ENV_RAW_DATA,
)


@pytest.mark.parametrize(
"legacy_env_var,new_env_var,value",
[
(LEGACY_ENV_ENDPOINT, ENV_ENDPOINT, "/data/output"),
(LEGACY_ENV_HASS_DISCOVERY, ENV_HASS_DISCOVERY, "True"),
(LEGACY_ENV_HASS_DISCOVERY_PREFIX, ENV_HASS_DISCOVERY_PREFIX, "homeassistant"),
(LEGACY_ENV_HASS_ENTITY_ID_PREFIX, ENV_HASS_ENTITY_ID_PREFIX, "ecowitt"),
(LEGACY_ENV_INPUT_UNIT_SYSTEM, ENV_INPUT_UNIT_SYSTEM, "imperial"),
(LEGACY_ENV_LOG_LEVEL, ENV_VERBOSE, "DEBUG"),
(LEGACY_ENV_MQTT_BROKER, ENV_MQTT_BROKER, "127.0.0.1"),
(LEGACY_ENV_MQTT_PASSWORD, ENV_MQTT_PASSWORD, "password"),
(LEGACY_ENV_MQTT_PORT, ENV_MQTT_PORT, "1883"),
(LEGACY_ENV_MQTT_TOPIC, ENV_MQTT_TOPIC, "topic"),
(LEGACY_ENV_MQTT_USERNAME, ENV_MQTT_USERNAME, "username"),
(LEGACY_ENV_OUTPUT_UNIT_SYSTEM, ENV_OUTPUT_UNIT_SYSTEM, "imperial"),
(LEGACY_ENV_PORT, ENV_PORT, "8080"),
(LEGACY_ENV_RAW_DATA, ENV_RAW_DATA, "True"),
],
)
def test_deprecated_env_var(
caplog: Mock, legacy_env_var: str, new_env_var: str, value: str
) -> None:
"""Test logging the usage of a deprecated environment variable.
Args:
caplog: A mock logging utility.
legacy_env_var: A legacy environment variable name.
new_env_var: The new environment variable name to use.
value: The environment variable's value.
"""
os.environ[legacy_env_var] = value
_ = get_env_vars()
assert any(
m
for m in caplog.messages
if (
f"Environment variable {legacy_env_var} is deprecated; use {new_env_var} "
"instead"
)
in m
)
os.environ.pop(legacy_env_var)


def test_get_cli_arguments() -> None:
Expand Down

0 comments on commit 9ff6994

Please sign in to comment.