chore: linting and formating python files

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-08-07 00:48:44 +02:00
1 parent dd2626a15c
commit 92d3e9b835
4 files changed
+48 -31

No files matched your search

+20 -12
View File
@@ -54,9 +54,13 @@ def validate_protocol_timeout(value):
ms = value.total_milliseconds
if ms < 5000:
raise cv.Invalid("Protocol timeout must be at least 5 seconds for reliable communication")
raise cv.Invalid(
"Protocol timeout must be at least 5 seconds for reliable communication"
)
if ms > 60000:
raise cv.Invalid("Protocol timeout must be at most 60 seconds to avoid blocking")
raise cv.Invalid(
"Protocol timeout must be at most 60 seconds to avoid blocking"
)
return value
@@ -76,16 +80,20 @@ def validate_update_interval(value):
CONFIG_SCHEMA = cv.All(
cv.Schema({
cv.GenerateID(): cv.declare_id(NutUpsComponent),
cv.Optional(CONF_UPDATE_INTERVAL, default="30s"): validate_update_interval,
cv.Optional(CONF_SIMULATION_MODE, default=False): cv.boolean,
cv.Optional(CONF_USB_VENDOR_ID, default=0x051D): cv.hex_uint16_t,
cv.Optional(CONF_USB_PRODUCT_ID, default=0x0002): cv.hex_uint16_t,
cv.Optional(CONF_PROTOCOL_TIMEOUT, default="15s"): validate_protocol_timeout,
cv.Optional(CONF_AUTO_DETECT_PROTOCOL, default=True): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA),
validate_usb_config
cv.Schema(
{
cv.GenerateID(): cv.declare_id(NutUpsComponent),
cv.Optional(CONF_UPDATE_INTERVAL, default="30s"): validate_update_interval,
cv.Optional(CONF_SIMULATION_MODE, default=False): cv.boolean,
cv.Optional(CONF_USB_VENDOR_ID, default=0x051D): cv.hex_uint16_t,
cv.Optional(CONF_USB_PRODUCT_ID, default=0x0002): cv.hex_uint16_t,
cv.Optional(
CONF_PROTOCOL_TIMEOUT, default="15s"
): validate_protocol_timeout,
cv.Optional(CONF_AUTO_DETECT_PROTOCOL, default=True): cv.boolean,
}
).extend(cv.COMPONENT_SCHEMA),
validate_usb_config,
)
+3 -2
View File
@@ -2,7 +2,6 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import (
CONF_ID,
CONF_TYPE,
DEVICE_CLASS_CONNECTIVITY,
DEVICE_CLASS_BATTERY,
@@ -14,7 +13,9 @@ from . import nut_ups_ns, NutUpsComponent, CONF_NUT_UPS_ID
DEPENDENCIES = ["nut_ups"]
NutUpsBinarySensor = nut_ups_ns.class_("NutUpsBinarySensor", binary_sensor.BinarySensor, cg.Component)
NutUpsBinarySensor = nut_ups_ns.class_(
"NutUpsBinarySensor", binary_sensor.BinarySensor, cg.Component
)
BINARY_SENSOR_TYPES = {
"online": {
+3 -1
View File
@@ -9,7 +9,9 @@ from . import nut_ups_ns, NutUpsComponent, CONF_NUT_UPS_ID
DEPENDENCIES = ["nut_ups"]
NutUpsTextSensor = nut_ups_ns.class_("NutUpsTextSensor", text_sensor.TextSensor, cg.Component)
NutUpsTextSensor = nut_ups_ns.class_(
"NutUpsTextSensor", text_sensor.TextSensor, cg.Component
)
TEXT_SENSOR_TYPES = [
"model",
+22 -16
View File
@@ -5,7 +5,6 @@ This ensures the Python configuration validation uses the same vendor list as C+
"""
import re
import os
from pathlib import Path
@@ -13,7 +12,7 @@ def parse_vendor_header(header_path):
"""Parse ups_vendors.h and extract vendor information."""
vendors = {}
with open(header_path, 'r') as f:
with open(header_path, "r") as f:
content = f.read()
# Find the KNOWN_UPS_VENDORS array
@@ -23,23 +22,24 @@ def parse_vendor_header(header_path):
for match in matches:
vendor_id_str, name, description = match
vendor_id = int(vendor_id_str, 16)
vendors[vendor_id] = {
'name': name,
'description': description
}
vendors[vendor_id] = {"name": name, "description": description}
return vendors
def generate_python_dict(vendors):
"""Generate Python dictionary code."""
lines = ["# Known UPS vendor IDs for validation", "# Auto-generated from ups_vendors.h", "KNOWN_VENDOR_IDS = {"]
lines = [
"# Known UPS vendor IDs for validation",
"# Auto-generated from ups_vendors.h",
"KNOWN_VENDOR_IDS = {",
]
for vendor_id, info in sorted(vendors.items()):
lines.append(f' 0x{vendor_id:04X}: "{info["name"]}",')
lines.append("}")
return '\n'.join(lines)
return "\n".join(lines)
def generate_udev_rules(vendors):
@@ -71,7 +71,7 @@ def update_udev_rules_in_setup_sh(setup_path, udev_rules):
print(f"Warning: {setup_path} not found!")
return False
with open(setup_path, 'r') as f:
with open(setup_path, "r") as f:
content = f.read()
# Find the udev rules section
@@ -95,9 +95,11 @@ def update_udev_rules_in_setup_sh(setup_path, udev_rules):
new_udev_section += udev_end
# Replace the section
new_content = content[:start_pos] + new_udev_section + content[end_pos + len(udev_end):]
new_content = (
content[:start_pos] + new_udev_section + content[end_pos + len(udev_end) :]
)
with open(setup_path, 'w') as f:
with open(setup_path, "w") as f:
f.write(new_content)
return True
@@ -125,15 +127,17 @@ def main():
# Update Python __init__.py
if init_path.exists():
# Read current __init__.py
with open(init_path, 'r') as f:
with open(init_path, "r") as f:
content = f.read()
# Replace the KNOWN_VENDOR_IDS section
pattern = r'KNOWN_VENDOR_IDS = \{[^}]*\}'
pattern = r"KNOWN_VENDOR_IDS = \{[^}]*\}"
if re.search(pattern, content, re.DOTALL):
new_content = re.sub(pattern, python_code.split('\n', 2)[2], content, flags=re.DOTALL)
new_content = re.sub(
pattern, python_code.split("\n", 2)[2], content, flags=re.DOTALL
)
with open(init_path, 'w') as f:
with open(init_path, "w") as f:
f.write(new_content)
print(f"✅ Updated {init_path}")
else:
@@ -148,7 +152,9 @@ def main():
udev_rules = generate_udev_rules(vendors)
if update_udev_rules_in_setup_sh(setup_path, udev_rules):
print(f"✅ Updated udev rules in {setup_path}")
print(f" Added {len([r for r in udev_rules if 'UPS' not in r])} standard + {len(vendors)} UPS vendor rules")
print(
f" Added {len([r for r in udev_rules if 'UPS' not in r])} standard + {len(vendors)} UPS vendor rules"
)
return 0