From 0b6e9ea2781cca80f0a9dace658eaeeebb6df29a Mon Sep 17 00:00:00 2001 From: Sander Date: Mon, 14 Nov 2022 19:40:59 +0000 Subject: [PATCH 1/2] Improve formatting and import order --- custom_components/kamstrup_403/__init__.py | 7 +++---- custom_components/kamstrup_403/config_flow.py | 11 +++-------- custom_components/kamstrup_403/kamstrup.py | 3 +-- custom_components/kamstrup_403/manifest.json | 10 +++------- custom_components/kamstrup_403/sensor.py | 7 +++---- tests/const.py | 4 ++-- tests/test_config_flow.py | 8 +++----- tests/test_init.py | 2 -- 8 files changed, 18 insertions(+), 34 deletions(-) diff --git a/custom_components/kamstrup_403/__init__.py b/custom_components/kamstrup_403/__init__.py index 14bd5a8..c702516 100644 --- a/custom_components/kamstrup_403/__init__.py +++ b/custom_components/kamstrup_403/__init__.py @@ -6,9 +6,8 @@ """ import asyncio from datetime import timedelta -from typing import Any, List import logging -import serial +from typing import Any, List from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PORT, CONF_SCAN_INTERVAL @@ -17,8 +16,7 @@ from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed - -from .kamstrup import Kamstrup +import serial from .const import ( DEFAULT_BAUDRATE, @@ -29,6 +27,7 @@ PLATFORMS, VERSION, ) +from .kamstrup import Kamstrup _LOGGER: logging.Logger = logging.getLogger(__package__) diff --git a/custom_components/kamstrup_403/config_flow.py b/custom_components/kamstrup_403/config_flow.py index cf0f51c..653cdc0 100644 --- a/custom_components/kamstrup_403/config_flow.py +++ b/custom_components/kamstrup_403/config_flow.py @@ -2,15 +2,10 @@ from homeassistant import config_entries from homeassistant.const import CONF_PORT, CONF_SCAN_INTERVAL, CONF_TIMEOUT from homeassistant.core import callback -import voluptuous as vol import serial +import voluptuous as vol -from .const import ( - DEFAULT_BAUDRATE, - DEFAULT_SCAN_INTERVAL, - DEFAULT_TIMEOUT, - DOMAIN, -) +from .const import DEFAULT_BAUDRATE, DEFAULT_SCAN_INTERVAL, DEFAULT_TIMEOUT, DOMAIN class KamstrupFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): @@ -79,7 +74,7 @@ class KamstrupOptionsFlowHandler(config_entries.OptionsFlow): """Kamstrup config flow options handler.""" def __init__(self, config_entry): - """Initialize HACS options flow.""" + """Initialize options flow.""" self.config_entry = config_entry self.options = dict(config_entry.options) diff --git a/custom_components/kamstrup_403/kamstrup.py b/custom_components/kamstrup_403/kamstrup.py index fe90bc8..54c54e6 100644 --- a/custom_components/kamstrup_403/kamstrup.py +++ b/custom_components/kamstrup_403/kamstrup.py @@ -13,10 +13,9 @@ # # Modified by Sander Gols to integrate in HA component -from __future__ import print_function +import math import serial -import math ####################################################################### # Units, provided by Erik Jensen diff --git a/custom_components/kamstrup_403/manifest.json b/custom_components/kamstrup_403/manifest.json index 1d22e91..2dd8b1a 100644 --- a/custom_components/kamstrup_403/manifest.json +++ b/custom_components/kamstrup_403/manifest.json @@ -6,10 +6,6 @@ "issue_tracker": "https://github.com/golles/ha-kamstrup_403/issues", "version": "2.0.0", "config_flow": true, - "codeowners": [ - "@golles" - ], - "requirements": [ - "pyserial==3.5" - ] -} \ No newline at end of file + "codeowners": ["@golles"], + "requirements": ["pyserial==3.5"] +} diff --git a/custom_components/kamstrup_403/sensor.py b/custom_components/kamstrup_403/sensor.py index d2fd584..71d8766 100644 --- a/custom_components/kamstrup_403/sensor.py +++ b/custom_components/kamstrup_403/sensor.py @@ -1,8 +1,4 @@ """Sensor platform for kamstrup_403.""" - -from homeassistant.config_entries import ConfigEntry -from homeassistant.const import VOLUME_CUBIC_METERS -from homeassistant.core import HomeAssistant from homeassistant.components.sensor import ( DOMAIN as SENSOR_DOMAIN, SensorDeviceClass, @@ -10,6 +6,9 @@ SensorEntityDescription, SensorStateClass, ) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import VOLUME_CUBIC_METERS +from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType diff --git a/tests/const.py b/tests/const.py index b73eb00..35fdac2 100644 --- a/tests/const.py +++ b/tests/const.py @@ -1,6 +1,6 @@ """Constants for kamstrup_403 tests.""" -from homeassistant.const import CONF_PORT, CONF_SCAN_INTERVAL +from homeassistant.const import CONF_PORT, CONF_SCAN_INTERVAL, CONF_TIMEOUT # Mock config data to be used across multiple tests MOCK_CONFIG = {CONF_PORT: "/dev/ttyUSB0"} -MOCK_UPDATE_CONFIG = {CONF_SCAN_INTERVAL: 120} +MOCK_UPDATE_CONFIG = {CONF_SCAN_INTERVAL: 120, CONF_TIMEOUT: 1.0} diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index a139017..a0a6ef0 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -2,7 +2,7 @@ from unittest.mock import patch from homeassistant import config_entries, data_entry_flow -from homeassistant.const import CONF_PORT, CONF_SCAN_INTERVAL +from homeassistant.const import CONF_PORT import pytest from pytest_homeassistant_custom_component.common import MockConfigEntry @@ -10,16 +10,14 @@ from .const import MOCK_CONFIG, MOCK_UPDATE_CONFIG + # This fixture bypasses the actual setup of the integration # since we only want to test the config flow. We test the # actual functionality of the integration in other test modules. @pytest.fixture(autouse=True) def bypass_setup_fixture(): """Prevent setup.""" - with patch( - "custom_components.kamstrup_403.async_setup", - return_value=True, - ), patch( + with patch("custom_components.kamstrup_403.async_setup", return_value=True,), patch( "custom_components.kamstrup_403.async_setup_entry", return_value=True, ): diff --git a/tests/test_init.py b/tests/test_init.py index 5eab9c4..e29686b 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,6 +1,4 @@ """Test kamstrup_403 setup process.""" -from homeassistant.exceptions import ConfigEntryNotReady -import pytest from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.kamstrup_403 import ( From 16cc1d80aca389686dc24d446b66f958a2ce4043 Mon Sep 17 00:00:00 2001 From: Sander Date: Mon, 14 Nov 2022 20:10:15 +0000 Subject: [PATCH 2/2] Add test that verify version and requirement versions --- tests/test_version.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/test_version.py diff --git a/tests/test_version.py b/tests/test_version.py new file mode 100644 index 0000000..899b6b9 --- /dev/null +++ b/tests/test_version.py @@ -0,0 +1,27 @@ +"""Test for versions.""" +import json + +from custom_components.kamstrup_403.const import VERSION + +with open( + file="custom_components/kamstrup_403/manifest.json", mode="r", encoding="UTF-8" +) as manifest_file: + data = manifest_file.read() + +manifest = json.loads(data) + + +async def test_component_version(): + """Verify that the version in the manifest and const.py are equal""" + assert manifest["version"] == VERSION + + +async def test_component_requirements(): + """Verify that all requirements in the manifest.json are defined as in the requirements files""" + requirements_files = ["requirements_dev.txt", "requirements_test.txt"] + for requirements_file in requirements_files: + with open(file=requirements_file, mode="r", encoding="UTF-8") as file: + lines = [line.rstrip("\n") for line in file] + + for requirement in manifest["requirements"]: + assert requirement in lines