From b23482e0851ccd62a1846e753b129005e0f8332c Mon Sep 17 00:00:00 2001 From: David Rapan Date: Thu, 18 Jul 2024 00:07:22 +0200 Subject: [PATCH] feat: Inverter Power Switch --- custom_components/solarman/api.py | 2 -- custom_components/solarman/common.py | 6 ++++++ .../inverter_definitions/deye_sg01hp3.yaml | 17 ++++++---------- .../inverter_definitions/deye_sg04lp3.yaml | 17 ++++++---------- .../deye_sun-12k-sg04lp3.yaml | 20 +++++++------------ custom_components/solarman/sensor.py | 4 +--- custom_components/solarman/switch.py | 19 ++++++++---------- 7 files changed, 34 insertions(+), 51 deletions(-) diff --git a/custom_components/solarman/api.py b/custom_components/solarman/api.py index 0a60e6e..e153a45 100644 --- a/custom_components/solarman/api.py +++ b/custom_components/solarman/api.py @@ -198,8 +198,6 @@ async def async_get(self, runtime = 0): _LOGGER.warning(f"Querying ({start} - {end}) failed. #{runtime} [{format_exception(e)}]") await asyncio.sleep(TIMINGS_QUERY_EXCEPT_SLEEP) - #if (n := ACTION_RETRY_ATTEMPTS - attempts_left) >= 1: - # await asyncio.sleep(n) _LOGGER.debug(f"Querying {'succeeded.' if result == 1 else f'attempts left: {attempts_left}{'' if attempts_left > 0 else ', aborting.'}'}") diff --git a/custom_components/solarman/common.py b/custom_components/solarman/common.py index 0f3551f..f850828 100644 --- a/custom_components/solarman/common.py +++ b/custom_components/solarman/common.py @@ -4,6 +4,12 @@ from .const import * +def get_current_file_name(value): + result = value.rsplit('.', 1) + if len(result) > 0: + return result[-1] + return "" + async def async_execute(x): loop = asyncio.get_running_loop() return await loop.run_in_executor(None, x) diff --git a/custom_components/solarman/inverter_definitions/deye_sg01hp3.yaml b/custom_components/solarman/inverter_definitions/deye_sg01hp3.yaml index 0e87d9c..a7a9764 100644 --- a/custom_components/solarman/inverter_definitions/deye_sg01hp3.yaml +++ b/custom_components/solarman/inverter_definitions/deye_sg01hp3.yaml @@ -811,25 +811,20 @@ parameters: min: 0 max: 3000 - - group: Status + - group: "Controls" items: - # Device - Power on/off status - - name: "Device Power Status" + # Device - Power the Inverter On/Off + - name: "" update_interval: 30 - class: "enum" + class: "switch" state_class: "" uom: "" scale: 1 rule: 1 registers: [0x0227] - icon: "mdi:information" - options: ["Off", "On"] - lookup: - - key: 0x0000 - value: "Off" - - key: 0x0001 - value: "On" + - group: Status + items: # Device - AC Relay status - name: "Device Relay Status" update_interval: 30 diff --git a/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml b/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml index d399e67..82c889d 100644 --- a/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml +++ b/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml @@ -816,25 +816,20 @@ parameters: min: 0 max: 3000 - - group: Status + - group: "Controls" items: - # Device - Power on/off status - - name: "Device Power Status" + # Device - Power the Inverter On/Off + - name: "" update_interval: 30 - class: "enum" + class: "switch" state_class: "" uom: "" scale: 1 rule: 1 registers: [0x0227] - icon: "mdi:information" - options: ["Off", "On"] - lookup: - - key: 0x0000 - value: "Off" - - key: 0x0001 - value: "On" + - group: Status + items: # Device - AC Relay status - name: "Device Relay Status" update_interval: 30 diff --git a/custom_components/solarman/inverter_definitions/deye_sun-12k-sg04lp3.yaml b/custom_components/solarman/inverter_definitions/deye_sun-12k-sg04lp3.yaml index 1573061..e541b7e 100644 --- a/custom_components/solarman/inverter_definitions/deye_sun-12k-sg04lp3.yaml +++ b/custom_components/solarman/inverter_definitions/deye_sun-12k-sg04lp3.yaml @@ -378,12 +378,11 @@ parameters: items: - name: "Solar Sell" update_interval: 30 - class: "" + class: "switch" state_class: "" uom: "" scale: 1 rule: 1 - switch: True registers: [0x0091] - group: BMS @@ -819,25 +818,20 @@ parameters: min: 0 max: 3000 - - group: Status + - group: "Controls" items: - # Device - Power on/off status - - name: "Device Power Status" + # Device - Power the Inverter On/Off + - name: "" update_interval: 30 - class: "enum" + class: "switch" state_class: "" uom: "" scale: 1 rule: 1 registers: [0x0227] - icon: "mdi:information" - options: ["Off", "On"] - lookup: - - key: 0x0000 - value: "Off" - - key: 0x0001 - value: "On" + - group: Status + items: # Device - AC Relay status - name: "Device Relay Status" update_interval: 30 diff --git a/custom_components/solarman/sensor.py b/custom_components/solarman/sensor.py index 7292ed9..2753a33 100644 --- a/custom_components/solarman/sensor.py +++ b/custom_components/solarman/sensor.py @@ -34,8 +34,6 @@ def _create_sensor(coordinator, sensor, battery_nominal_voltage, battery_life_cy entity = SolarmanStatus(coordinator, sensor) elif sensor["name"] in ("Battery SOH", "Battery State", "Today Battery Life Cycles", "Total Battery Life Cycles"): entity = SolarmanBatterySensor(coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating) - #elif "switch" in sensor and sensor["switch"]: - # entity = SolarmanSwitchEntity(coordinator, sensor, battery_life_cycle_rating) else: entity = SolarmanSensor(coordinator, sensor, battery_life_cycle_rating) @@ -61,7 +59,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_ # _LOGGER.debug(f"async_setup: async_add_entities") - async_add_entities(_create_sensor(coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating) for sensor in sensors if not "switch" in sensor) + async_add_entities(_create_sensor(coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating) for sensor in sensors if (not "class" in sensor or not sensor["class"] in PLATFORMS)) return True async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool: diff --git a/custom_components/solarman/switch.py b/custom_components/solarman/switch.py index ce14336..fcd1bfa 100644 --- a/custom_components/solarman/switch.py +++ b/custom_components/solarman/switch.py @@ -29,9 +29,11 @@ _LOGGER = logging.getLogger(__name__) -def _create_sensor(coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating): +_PLATFORM = get_current_file_name(__name__) + +def _create_sensor(coordinator, sensor): try: - entity = SolarmanSwitchEntity(coordinator, sensor, battery_life_cycle_rating) + entity = SolarmanSwitchEntity(coordinator, sensor) entity.update() @@ -44,18 +46,13 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_ _LOGGER.debug(f"async_setup_entry: {config.options}") coordinator = hass.data[DOMAIN][config.entry_id] - options = config.options - - battery_nominal_voltage = options.get(CONF_BATTERY_NOMINAL_VOLTAGE) - battery_life_cycle_rating = options.get(CONF_BATTERY_LIFE_CYCLE_RATING) - sensors = coordinator.inverter.get_sensors() # Add entities. # _LOGGER.debug(f"async_setup: async_add_entities") - async_add_entities(_create_sensor(coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating) for sensor in sensors if "switch" in sensor) + async_add_entities(_create_sensor(coordinator, sensor) for sensor in sensors if ("class" in sensor and sensor["class"] == _PLATFORM)) return True async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool: @@ -63,8 +60,8 @@ async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool: return True class SolarmanSwitchEntity(SolarmanSensor, SwitchEntity): - def __init__(self, coordinator, sensor, battery_life_cycle_rating): - SolarmanSensor.__init__(self, coordinator, sensor, battery_life_cycle_rating) + def __init__(self, coordinator, sensor): + SolarmanSensor.__init__(self, coordinator, sensor, 0) # Set The Device Class of the entity. self._attr_device_class = SwitchDeviceClass.SWITCH # Set The Category of the entity. @@ -84,7 +81,7 @@ def is_on(self) -> bool | None: async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" - await self.coordinator.inverter.service_write_multiple_holding_registers(self.register, [65280,]) + await self.coordinator.inverter.service_write_multiple_holding_registers(self.register, [1,]) self._attr_state = 1 self.async_write_ha_state()