Skip to content

Commit

Permalink
Very hacky workaround for pyasic being unable to be installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rowan committed Nov 8, 2024
1 parent 84a6530 commit c1b89ee
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 11 deletions.
10 changes: 9 additions & 1 deletion custom_components/miner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
"""The Miner integration."""
from __future__ import annotations

import pyasic

try:
import pyasic
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
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
Expand Down
11 changes: 9 additions & 2 deletions custom_components/miner/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
"""Config flow for Miner."""
import logging

import pyasic
try:
import pyasic
except ImportError:
from .patch import install_package
from .const import PYASIC_VERSION
install_package(f"pyasic=={PYASIC_VERSION}")
import pyasic
from pyasic import MinerNetwork

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import network
Expand All @@ -10,7 +18,6 @@
from homeassistant.helpers.selector import TextSelector
from homeassistant.helpers.selector import TextSelectorConfig
from homeassistant.helpers.selector import TextSelectorType
from pyasic import MinerNetwork

from .const import CONF_IP
from .const import CONF_RPC_PASSWORD
Expand Down
3 changes: 3 additions & 0 deletions custom_components/miner/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@

TERA_HASH_PER_SECOND = "TH/s"
JOULES_PER_TERA_HASH = "J/TH"


PYASIC_VERSION = "0.62.3"
8 changes: 7 additions & 1 deletion custom_components/miner/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import logging
from datetime import timedelta

import pyasic
try:
import pyasic
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
from homeassistant.core import HomeAssistant
from homeassistant.helpers.debounce import Debouncer
Expand Down
4 changes: 2 additions & 2 deletions custom_components/miner/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"homekit": {},
"iot_class": "local_polling",
"issue_tracker": "https://github.com/Schnitzel/hass-miner/issues",
"requirements": ["betterproto==2.0.0b7", "pyasic==0.62.1"],
"requirements": [],
"ssdp": [],
"version": "1.1.17rc5",
"version": "1.1.17rc6",
"zeroconf": []
}
9 changes: 8 additions & 1 deletion custom_components/miner/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import logging

import pyasic
try:
import pyasic
except ImportError:
from .patch import install_package
from .const import PYASIC_VERSION
install_package(f"pyasic=={PYASIC_VERSION}")
import pyasic

from homeassistant.components.number import NumberEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
Expand Down
87 changes: 87 additions & 0 deletions custom_components/miner/patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from __future__ import annotations

import os
import site
import sys
from subprocess import PIPE, Popen

from homeassistant.util.package import is_virtual_env, _LOGGER

_UV_ENV_PYTHON_VARS = (
"UV_SYSTEM_PYTHON",
"UV_PYTHON",
)

# Copy-paste of home assistant core install, but pre-releases are supported
def install_package(
package: str,
upgrade: bool = True,
target: str | None = None,
constraints: str | None = None,
timeout: int | None = None,
) -> bool:
"""Install a package on PyPi. Accepts pip compatible package strings.
Return boolean if install successful.
"""
_LOGGER.info("Attempting install of %s", package)
env = os.environ.copy()
args = [
sys.executable,
"-m",
"uv",
"pip",
"install",
"--quiet",
package,
# Allow prereleases in sub-packages
"--prerelease=allow",
# We need to use unsafe-first-match for custom components
# which can use a different version of a package than the one
# we have built the wheel for.
"--index-strategy",
"unsafe-first-match",
]
if timeout:
env["HTTP_TIMEOUT"] = str(timeout)
if upgrade:
args.append("--upgrade")
if constraints is not None:
args += ["--constraint", constraints]
if target:
abs_target = os.path.abspath(target)
args += ["--target", abs_target]
elif (
not is_virtual_env()
and not (any(var in env for var in _UV_ENV_PYTHON_VARS))
and (abs_target := site.getusersitepackages())
):
# Pip compatibility
# Uv has currently no support for --user
# See https://github.com/astral-sh/uv/issues/2077
# Using workaround to install to site-packages
# 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,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
env=env,
close_fds=False, # required for posix_spawn
) as process:
_, stderr = process.communicate()
if process.returncode != 0:
_LOGGER.error(
"Unable to install package %s: %s",
package,
stderr.decode("utf-8").lstrip().strip(),
)
return False


return True
7 changes: 3 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
colorlog==6.8.2
homeassistant>=2024.10.0
pip>=21.0,<23.2
ruff==0.7.2
pyasic==0.62.1
betterproto==2.0.0b7
setuptools==75.3.0
ruff==0.6.9
pyasic==0.62.3
setuptools==75.1.0
pre-commit

0 comments on commit c1b89ee

Please sign in to comment.