From b46853f978b46d55232e1938b9887b2f0d6373de Mon Sep 17 00:00:00 2001 From: Brett Rowan <121075405+b-rowan@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:58:11 -0700 Subject: [PATCH] Fix some ruff and linting issues --- custom_components/miner/__init__.py | 1 + custom_components/miner/const.py | 2 +- custom_components/miner/coordinator.py | 3 ++- custom_components/miner/number.py | 1 + custom_components/miner/patch.py | 12 +++++------ custom_components/miner/select.py | 28 +++++++++++++++++++------- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/custom_components/miner/__init__.py b/custom_components/miner/__init__.py index 1cd847e..35119c0 100644 --- a/custom_components/miner/__init__.py +++ b/custom_components/miner/__init__.py @@ -7,6 +7,7 @@ except ImportError: from .patch import install_package from .const import PYASIC_VERSION + install_package(f"pyasic=={PYASIC_VERSION}") import pyasic diff --git a/custom_components/miner/const.py b/custom_components/miner/const.py index fe3230d..bcf9b70 100644 --- a/custom_components/miner/const.py +++ b/custom_components/miner/const.py @@ -17,4 +17,4 @@ JOULES_PER_TERA_HASH = "J/TH" -PYASIC_VERSION = "0.64.1" \ No newline at end of file +PYASIC_VERSION = "0.64.1" diff --git a/custom_components/miner/coordinator.py b/custom_components/miner/coordinator.py index 1ee1e08..43efbac 100644 --- a/custom_components/miner/coordinator.py +++ b/custom_components/miner/coordinator.py @@ -7,6 +7,7 @@ except ImportError: from .patch import install_package from .const import PYASIC_VERSION + install_package(f"pyasic=={PYASIC_VERSION}") import pyasic from homeassistant.config_entries import ConfigEntry @@ -145,6 +146,6 @@ async def _async_update_data(self): "fan_sensors": { idx: {"fan_speed": fan.speed} for idx, fan in enumerate(miner_data.fans) }, - "config": miner_data.config + "config": miner_data.config, } return data diff --git a/custom_components/miner/number.py b/custom_components/miner/number.py index 2d325e3..9cf1df3 100644 --- a/custom_components/miner/number.py +++ b/custom_components/miner/number.py @@ -8,6 +8,7 @@ except ImportError: from .patch import install_package from .const import PYASIC_VERSION + install_package(f"pyasic=={PYASIC_VERSION}") import pyasic diff --git a/custom_components/miner/patch.py b/custom_components/miner/patch.py index 7af23b1..815858e 100644 --- a/custom_components/miner/patch.py +++ b/custom_components/miner/patch.py @@ -1,11 +1,14 @@ +"""Path annoying home assistant dependency handling.""" from __future__ import annotations import os import site import sys -from subprocess import PIPE, Popen +from subprocess import PIPE +from subprocess import Popen -from homeassistant.util.package import is_virtual_env, _LOGGER +from homeassistant.util.package import _LOGGER +from homeassistant.util.package import is_virtual_env _UV_ENV_PYTHON_VARS = ( "UV_SYSTEM_PYTHON", @@ -22,7 +25,6 @@ def install_package( ) -> bool: """Install a package on PyPi. Accepts pip compatible package strings. - Return boolean if install successful. """ _LOGGER.info("Attempting install of %s", package) @@ -64,7 +66,6 @@ def install_package( # https://github.com/astral-sh/uv/issues/2077#issuecomment-2150406001 args += ["--python", sys.executable, "--target", abs_target] - _LOGGER.debug("Running uv pip command: args=%s", args) with Popen( args, @@ -83,5 +84,4 @@ def install_package( ) return False - - return True \ No newline at end of file + return True diff --git a/custom_components/miner/select.py b/custom_components/miner/select.py index 1716ab8..b1b9627 100644 --- a/custom_components/miner/select.py +++ b/custom_components/miner/select.py @@ -1,15 +1,20 @@ +"""A selector for the miner's mining mode.""" from __future__ import annotations from homeassistant.components.select import SelectEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import callback +from homeassistant.core import HomeAssistant from homeassistant.helpers import entity from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from pyasic import MinerConfig -from pyasic.config.mining import MiningModeHPM, MiningModeNormal, MiningModeLPM +from pyasic.config.mining import MiningModeHPM +from pyasic.config.mining import MiningModeLPM +from pyasic.config.mining import MiningModeNormal -from custom_components.miner import MinerCoordinator, DOMAIN +from custom_components.miner import DOMAIN +from custom_components.miner import MinerCoordinator async def async_setup_entry( @@ -27,7 +32,10 @@ def _create_entity(key: str): created.add(key) await coordinator.async_config_entry_first_refresh() - if coordinator.miner.supports_power_modes and not coordinator.miner.supports_autotuning: + if ( + coordinator.miner.supports_power_modes + and not coordinator.miner.supports_autotuning + ): async_add_entities( [ MinerPowerModeSwitch( @@ -38,6 +46,8 @@ def _create_entity(key: str): class MinerPowerModeSwitch(CoordinatorEntity[MinerCoordinator], SelectEntity): + """A selector for the miner's miner mode.""" + def __init__( self, coordinator: MinerCoordinator, @@ -46,7 +56,6 @@ def __init__( super().__init__(coordinator=coordinator) self._attr_unique_id = f"{self.coordinator.data['mac']}-power-mode" - @property def name(self) -> str | None: """Return name of the entity.""" @@ -65,17 +74,22 @@ def device_info(self) -> entity.DeviceInfo: @property def current_option(self) -> str | None: + """The current option selected with the select.""" config: MinerConfig = self.coordinator.data["config"] return str(config.mining_mode.mode) @property def options(self) -> list[str]: + """The allowed options for the selector.""" return ["Normal", "High", "Low"] async def async_select_option(self, option: str) -> None: """Change the selected option.""" - option_map = {"High": MiningModeHPM, "Normal": MiningModeNormal, "Low": MiningModeLPM} + option_map = { + "High": MiningModeHPM, + "Normal": MiningModeNormal, + "Low": MiningModeLPM, + } cfg = await self.coordinator.miner.get_config() cfg.mining_mode = option_map[option]() await self.coordinator.miner.send_config(cfg) -