From 1fb0525f2186f92c211fb50a0765ca4d3eeb9c8b Mon Sep 17 00:00:00 2001 From: wimb0 <11030068+wimb0@users.noreply.github.com> Date: Tue, 11 Feb 2025 14:38:49 +0100 Subject: [PATCH] Ruff Format --- custom_components/saj_modbus/__init__.py | 2 ++ custom_components/saj_modbus/config_flow.py | 4 ++-- custom_components/saj_modbus/const.py | 2 ++ custom_components/saj_modbus/hub.py | 18 ++++++++++-------- custom_components/saj_modbus/payload.py | 2 +- custom_components/saj_modbus/sensor.py | 7 ++----- custom_components/saj_modbus/services.py | 19 ++++++++----------- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/custom_components/saj_modbus/__init__.py b/custom_components/saj_modbus/__init__.py index ac2d995..e661416 100644 --- a/custom_components/saj_modbus/__init__.py +++ b/custom_components/saj_modbus/__init__.py @@ -1,4 +1,5 @@ """The SAJ Modbus Integration.""" + import asyncio import logging @@ -60,6 +61,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): return True + async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload SAJ mobus entry.""" diff --git a/custom_components/saj_modbus/config_flow.py b/custom_components/saj_modbus/config_flow.py index 92f1b5c..5b97892 100644 --- a/custom_components/saj_modbus/config_flow.py +++ b/custom_components/saj_modbus/config_flow.py @@ -6,8 +6,7 @@ import voluptuous as vol from homeassistant import config_entries -from homeassistant.const import (CONF_HOST, CONF_NAME, CONF_PORT, - CONF_SCAN_INTERVAL) +from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SCAN_INTERVAL from homeassistant.core import HomeAssistant, callback from .const import DEFAULT_NAME, DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DOMAIN @@ -40,6 +39,7 @@ def saj_modbus_entries(hass: HomeAssistant): for config_entry in hass.config_entries.async_entries(DOMAIN) } + class SAJModbusConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """SAJ Modbus configflow.""" diff --git a/custom_components/saj_modbus/const.py b/custom_components/saj_modbus/const.py index 10931b4..6ac8cd4 100644 --- a/custom_components/saj_modbus/const.py +++ b/custom_components/saj_modbus/const.py @@ -32,6 +32,7 @@ class SajModbusNumberEntityDescription(NumberEntityDescription): """A class that describes SAJ number entities.""" + NUMBER_TYPES: dict[str, list[SajModbusNumberEntityDescription]] = { "LimitPower": SajModbusNumberEntityDescription( name="Limit Power", @@ -48,6 +49,7 @@ class SajModbusNumberEntityDescription(NumberEntityDescription): class SajModbusSensorEntityDescription(SensorEntityDescription): """A class that describes SAJ sensor entities.""" + COUNTER_SENSOR_TYPES: dict[str, list[SajModbusSensorEntityDescription]] = { "TodayEnergy": SajModbusSensorEntityDescription( name="Power generation on current day", diff --git a/custom_components/saj_modbus/hub.py b/custom_components/saj_modbus/hub.py index 17740ad..c06372e 100644 --- a/custom_components/saj_modbus/hub.py +++ b/custom_components/saj_modbus/hub.py @@ -1,14 +1,11 @@ """SAJ Modbus Hub.""" + from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from voluptuous.validators import Number import logging import threading from datetime import datetime, timedelta -from homeassistant.core import ( - CALLBACK_TYPE, - callback, - HomeAssistant -) +from homeassistant.core import CALLBACK_TYPE, callback, HomeAssistant from homeassistant.helpers import entity_registry from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN from pymodbus.client import ModbusTcpClient @@ -156,8 +153,11 @@ def read_modbus_realtime_data(self) -> dict: mpvmode = decoder.decode_16bit_uint() if mpvmode == 2: - data["limitpower"] = (110 if mpvmode != self.data.get("mpvmode") - else self.data.get("limitpower")) + data["limitpower"] = ( + 110 + if mpvmode != self.data.get("mpvmode") + else self.data.get("limitpower") + ) data["mpvmode"] = mpvmode @@ -323,7 +323,9 @@ def set_limitpower(self, value: float) -> None: """Limit the power output of the inverter.""" if self.limiter_is_disabled(): return - response = self._write_registers(unit=1, address=0x801F, values=[int(value * 10)]) + response = self._write_registers( + unit=1, address=0x801F, values=[int(value * 10)] + ) if response.isError(): return self.data["limitpower"] = value diff --git a/custom_components/saj_modbus/payload.py b/custom_components/saj_modbus/payload.py index 317f89d..e278c42 100644 --- a/custom_components/saj_modbus/payload.py +++ b/custom_components/saj_modbus/payload.py @@ -7,6 +7,7 @@ Based up on the original of pyModbus https://github.com/pymodbus-dev/pymodbus/blob/v3.8.3/pymodbus/payload.py """ + from __future__ import annotations @@ -274,7 +275,6 @@ def __init__(self, payload, byteorder=Endian.LITTLE, wordorder=Endian.BIG): self._byteorder = byteorder self._wordorder = wordorder - @classmethod def fromRegisters( cls, diff --git a/custom_components/saj_modbus/sensor.py b/custom_components/saj_modbus/sensor.py index 012992a..1eff543 100644 --- a/custom_components/saj_modbus/sensor.py +++ b/custom_components/saj_modbus/sensor.py @@ -83,9 +83,8 @@ def unique_id(self) -> str | None: @property def native_value(self): """Return the native value of the sensor.""" - return ( - self.coordinator.data.get(self.entity_description.key, None) - ) + return self.coordinator.data.get(self.entity_description.key, None) + class SajCounterSensor(SajSensor): """Representation of a SAJ Modbus counter sensor.""" @@ -98,5 +97,3 @@ def native_value(self): if self.coordinator.data.get("mpvmode") in (1, 2): # "Waiting" or "Normal" return self.coordinator.data.get(self.entity_description.key) return None - - diff --git a/custom_components/saj_modbus/services.py b/custom_components/saj_modbus/services.py index 1f16e39..b84e283 100644 --- a/custom_components/saj_modbus/services.py +++ b/custom_components/saj_modbus/services.py @@ -7,10 +7,7 @@ from homeassistant.const import ATTR_DEVICE_ID from homeassistant.core import HomeAssistant, ServiceCall, callback -from homeassistant.helpers import ( - device_registry as dr, - config_validation as cv -) +from homeassistant.helpers import device_registry as dr, config_validation as cv from .const import DOMAIN as SAJ_DOMAIN @@ -19,10 +16,12 @@ SERVICE_SET_DATE_TIME = "set_datetime" SERVICE_SET_DATE_TIME_SCHEMA = vol.All( - vol.Schema({ - vol.Required(ATTR_DEVICE_ID): str, - vol.Optional(ATTR_DATETIME): cv.datetime, - }) + vol.Schema( + { + vol.Required(ATTR_DEVICE_ID): str, + vol.Optional(ATTR_DATETIME): cv.datetime, + } + ) ) SUPPORTED_SERVICES = (SERVICE_SET_DATE_TIME,) @@ -67,7 +66,5 @@ async def async_set_date_time(hass: HomeAssistant, data: Mapping[str, Any]) -> N hub = hass.data[SAJ_DOMAIN][device_entry.name]["hub"] await hass.async_add_executor_job( - hub.set_date_and_time, - data.get(ATTR_DATETIME, None) + hub.set_date_and_time, data.get(ATTR_DATETIME, None) ) -