Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug information for issue with getting hass from the gateway #259

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 63 additions & 11 deletions .github/scripts/update_hacs_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#
# MIT License
#
# Copyright (c) 2021 Bram Gerritsen
# Copyright (c) 2022 Mario DE WEERD
# Copyright (c) 2021 Bram Gerritsen
# Copyright (c) 2022-2024 Mario DE WEERD
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,33 +28,85 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Update the manifest file."""
"""Update the files with new version."""
import json
import os
import re
import sys


def update_manifest():
def update_manifest(path=None, version=None):
"""Update the manifest file."""
version = "0.0.0"
for index, value in enumerate(sys.argv):
if value in ["--version", "-V"]:
version = sys.argv[index + 1]
if path is None:
return

with open(
f"{os.getcwd()}/custom_components/zha_toolkit/manifest.json",
path,
encoding="utf_8",
) as manifestfile:
manifest = json.load(manifestfile)

manifest["version"] = version

with open(
f"{os.getcwd()}/custom_components/zha_toolkit/manifest.json",
path,
"w",
encoding="utf_8",
) as manifestfile:
manifestfile.write(json.dumps(manifest, indent=4, sort_keys=True))


update_manifest()
def replace_version_in_file(path: str, regex: str, version: str):
# Remove any leading 'v' from the provided version
new_version = version.lstrip("v")

# Compile the regex pattern
pattern = re.compile(regex)

# Function to replace the version part in the match
def version_replacer(match):
print("YAS")
# Extract the original version from the match
original_version = match.group("version")

# Determine if the original version started with 'v'
if original_version.startswith("v"):
replacement_version = f"v{new_version}"
else:
replacement_version = new_version

# Replace the version in the matched string
replacement_match = match.group(0).replace(
original_version, replacement_version
)

return replacement_match

# Read the file content
with open(path, encoding="utf_8") as file:
content = file.read()

# Replace the versions in the content
new_content = pattern.sub(version_replacer, content)

# Write the modified content back to the file
with open(path, "w", encoding="utf_8") as file:
file.write(new_content)


newVersion = "0.0.0"
for index, value in enumerate(sys.argv):
if value in ["--version", "-V", "-v"]:
newVersion = sys.argv[index + 1]


filepath = f"{os.getcwd()}/custom_components/zha_toolkit/manifest.json"
update_manifest(filepath, newVersion)

# Example:
# replace_version_in_file('file.txt', r'(?P<version>v?\d+\.\d+\.\d+)', '2.3.4')

# filepath = f"{os.getcwd()}/README"
# Example1: regex = r'toolkit version \[(?P<version>.*?)]'
# Example2: regex= r'(?P<version>v?\d+\.\d+\.\d+)'
# replace_version_in_file(filepath, regex, newVersion)
5 changes: 5 additions & 0 deletions STATS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Badges showing number of downloads per version

- ![badge latest](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/latest/total.svg)
- ![badge v1.1.18](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.18/total.svg)
- ![badge v1.1.17](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.17/total.svg)
- ![badge v1.1.16](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.16/total.svg)
- ![badge v1.1.15](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.15/total.svg)
- ![badge v1.1.14](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.14/total.svg)
- ![badge v1.1.13](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.13/total.svg)
- ![badge v1.1.12](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.12/total.svg)
- ![badge v1.1.11](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v1.1.11/total.svg)
Expand Down
5 changes: 5 additions & 0 deletions custom_components/zha_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,4 +1183,9 @@ def get_hass(gateway: ZHAGateway):
hass = getattr(gateway, "_hass", None)
if hass is None:
hass = getattr(gateway, "hass", None)
if hass is None:
msg = f"Could not get hass from {gateway!r}."
if gateway is not None:
msg += "Attributes available {dir(gateway)}."
raise ValueError(msg)
return hass
Loading