Skip to content

Commit

Permalink
Fix support Hass 2023.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 15, 2023
1 parent 0c238f9 commit e5bdef6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 58 deletions.
68 changes: 23 additions & 45 deletions hass_win/__main__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
"""
Run Home Assistant natively in Windows. Tested with:
Version 1:
- Windows 10 x64 (19044.1466)
- Python 3.9.10 x64
- Home Assistant 2022.2.0 (default_config)
Version 2:
- Windows 7 x64
- WinPython v3.8.9 x32
- Home Assistant 2021.12.10 (default_config)
"""
import logging
import mimetypes
import os
import platform
import socket
import subprocess
import sys
from logging import FileHandler
from logging.handlers import BaseRotatingHandler
from types import ModuleType

# noinspection PyPackageRequirements
from atomicwrites import AtomicWriter
from colorlog import ColoredFormatter
from homeassistant import __main__, const, setup
from homeassistant.helpers import frame, signal
from homeassistant.loader import Integration
Expand All @@ -40,6 +20,9 @@

sys.argv.append("--runner")

# https://docs.python.org/3/library/os.html#utf8-mode
os.environ["PYTHONUTF8"] = "1"

while True:
try:
subprocess.check_call(sys.argv)
Expand All @@ -54,6 +37,8 @@
# runner arg supported only on old Hass versions
sys.argv.remove("--runner")

assert sys.flags.utf8_mode, "env PYTHONUTF8=1 should be set"


def wrap_log(func):
def wrap(*args, **kwargs):
Expand All @@ -63,21 +48,11 @@ def wrap(*args, **kwargs):
return wrap


def wrap_utf8(func):
def wrap(*args, **kwargs):
if len(args) == 5:
return func(args[0], args[1], args[2], args[3] or "utf-8", args[4])
kwargs["encoding"] = "utf-8"
return func(*args, **kwargs)

return wrap


def wrap_before_pip(func):
def wrap(self, hass, pkg_path, file_path, manifest):
if manifest["domain"] == "cast":
if (const.MAJOR_VERSION, const.MINOR_VERSION) >= (2022, 12):
manifest["requirements"] = ["pychromecast==12.1.4"]
# if manifest["domain"] == "cast":
# if (const.MAJOR_VERSION, const.MINOR_VERSION) >= (2022, 12):
# manifest["requirements"] = ["pychromecast==12.1.4"]
return func(self, hass, pkg_path, file_path, manifest)

return wrap
Expand All @@ -95,9 +70,7 @@ async def wrapper(hass, domain, config):
elif domain == "ffmpeg":
try:
binary = config.get(domain).get(domain + "_bin", domain)
subprocess.Popen(
[binary, "-version"], stdout=subprocess.DEVNULL
)
subprocess.Popen([binary, "-version"], stdout=subprocess.DEVNULL)
except Exception:
logging.getLogger(__name__).info("FFmpeg DISABLED!")
return True
Expand All @@ -113,9 +86,10 @@ def fix_requirements(requirements: list):
if req == "pyturbojpeg":
# fix PyTurboJPEG for camera and stream
from turbojpeg import DEFAULT_LIB_PATHS

# downloaded from: https://pypi.org/project/PyTurboJPEG/
DEFAULT_LIB_PATHS["Windows"].append(
f"{os.path.dirname(__file__)}\\turbojpeg-{ARCH}.dll"
os.path.dirname(__file__) + f"\\turbojpeg-{ARCH}.dll"
)

elif req == "pyserial":
Expand Down Expand Up @@ -148,14 +122,6 @@ def wrapper(integration: Integration):
# noinspection PyFinal
const.REQUIRED_NEXT_PYTHON_HA_RELEASE = None

# adds msec to logger
ColoredFormatter.__init__ = wrap_log(ColoredFormatter.__init__)

# fix Windows encoding
AtomicWriter.__init__ = wrap_utf8(AtomicWriter.__init__)
FileHandler.__init__ = wrap_utf8(FileHandler.__init__)
BaseRotatingHandler.__init__ = wrap_utf8(BaseRotatingHandler.__init__)

# fix mimetypes for borked Windows machines
# https://github.com/home-assistant/core/commit/64bcd6097457b0c56528425a6a6ce00a2bce791c
mimetypes.add_type("text/css", ".css")
Expand Down Expand Up @@ -187,5 +153,17 @@ def wrapper(integration: Integration):
# fix Chromecast warning in logs (async_setup_platforms)
frame.report = lambda *args, **kwargs: None

# fix opus library for VOIP integration
os.environ["PATH"] += ";" + os.path.dirname(__file__)

if __name__ == "__main__":
try:
# optional color logs
from colorlog import ColoredFormatter

# adds msec to logger
ColoredFormatter.__init__ = wrap_log(ColoredFormatter.__init__)
except ModuleNotFoundError:
pass

sys.exit(__main__.main())
Binary file added hass_win/opus.dll
Binary file not shown.
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

setup(
name="hass_win",
version="2023.3.6",
version="2023.5.3",
description="Windows runner for Home Assistant",
author="AlexxIT",
install_requires=["homeassistant", "colorlog"],
install_requires=[
"homeassistant",
"colorlog", # optional color log
"chacha20poly1305-reuseable==0.0.4", # fix HomeKit support on after 2023.5
],
packages=["hass_win"],
package_data={"hass_win": ["*.dll"]},
)
11 changes: 0 additions & 11 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
from hass_win import __main__


def test_utf8():
from logging import FileHandler
from logging.handlers import RotatingFileHandler
# noinspection PyPackageRequirements
from atomicwrites import AtomicWriter

assert AtomicWriter(__file__, overwrite=False)
assert FileHandler(__file__, 'r', None, False).encoding == "utf-8"
assert RotatingFileHandler(__file__, 'r').encoding == "utf-8"


def test_validate_os():
from homeassistant import __main__
assert __main__.validate_python() is None
Expand Down

0 comments on commit e5bdef6

Please sign in to comment.