Skip to content

Commit

Permalink
Fix some ruff and linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rowan committed Nov 22, 2024
1 parent c7342b5 commit b46853f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions custom_components/miner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
except ImportError:
from .patch import install_package
from .const import PYASIC_VERSION

install_package(f"pyasic=={PYASIC_VERSION}")
import pyasic

Expand Down
2 changes: 1 addition & 1 deletion custom_components/miner/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
JOULES_PER_TERA_HASH = "J/TH"


PYASIC_VERSION = "0.64.1"
PYASIC_VERSION = "0.64.1"
3 changes: 2 additions & 1 deletion custom_components/miner/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions custom_components/miner/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
except ImportError:
from .patch import install_package
from .const import PYASIC_VERSION

install_package(f"pyasic=={PYASIC_VERSION}")
import pyasic

Expand Down
12 changes: 6 additions & 6 deletions custom_components/miner/patch.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -83,5 +84,4 @@ def install_package(
)
return False


return True
return True
28 changes: 21 additions & 7 deletions custom_components/miner/select.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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."""
Expand All @@ -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)

0 comments on commit b46853f

Please sign in to comment.