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
5 files changed
+86 -69

No files matched your search

+28 -20
View File
@@ -36,15 +36,15 @@ def validate_usb_config(config):
"""Validate USB configuration with helpful warnings."""
vendor_id = config.get(CONF_USB_VENDOR_ID, 0x051D)
product_id = config.get(CONF_USB_PRODUCT_ID, 0x0002)
# Validate non-zero IDs
if vendor_id == 0 or product_id == 0:
raise cv.Invalid("USB vendor and product IDs must be non-zero")
# Warn about unknown vendor IDs
if vendor_id not in KNOWN_VENDOR_IDS:
raise cv.Invalid("Unknown vendor id")
return config
@@ -52,12 +52,16 @@ def validate_protocol_timeout(value):
"""Validate protocol timeout with reasonable bounds."""
value = cv.positive_time_period_milliseconds(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
@@ -65,27 +69,31 @@ def validate_update_interval(value):
"""Validate update interval for UPS monitoring."""
value = cv.update_interval(value)
ms = value.total_milliseconds
if ms < 5000:
cg.global_ns.logger.LOGGER.warning(
"Update interval less than 5 seconds may cause excessive USB traffic. "
"Consider using 10s or higher for production."
)
return 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,
)
@@ -97,4 +105,4 @@ async def to_code(config):
cg.add(var.set_usb_vendor_id(config[CONF_USB_VENDOR_ID]))
cg.add(var.set_usb_product_id(config[CONF_USB_PRODUCT_ID]))
cg.add(var.set_protocol_timeout(config[CONF_PROTOCOL_TIMEOUT]))
cg.add(var.set_auto_detect_protocol(config[CONF_AUTO_DETECT_PROTOCOL]))
cg.add(var.set_auto_detect_protocol(config[CONF_AUTO_DETECT_PROTOCOL]))
+4 -3
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": {
@@ -61,4 +62,4 @@ async def to_code(config):
# Override config with sensor type defaults if not specified
if "device_class" not in config and "device_class" in sensor_config:
cg.add(var.set_device_class(sensor_config["device_class"]))
cg.add(var.set_device_class(sensor_config["device_class"]))
+1 -1
View File
@@ -94,4 +94,4 @@ async def to_code(config):
cg.add(var.set_state_class(sensor_config["state_class"]))
if "accuracy_decimals" not in config and "accuracy_decimals" in sensor_config:
cg.add(var.set_accuracy_decimals(sensor_config["accuracy_decimals"]))
cg.add(var.set_accuracy_decimals(sensor_config["accuracy_decimals"]))
+4 -2
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",
@@ -36,4 +38,4 @@ async def to_code(config):
sensor_type = config[CONF_TYPE]
cg.add(var.set_sensor_type(sensor_type))
cg.add(parent.register_text_sensor(var, sensor_type))
cg.add(parent.register_text_sensor(var, sensor_type))