Skip to content

Commit

Permalink
feat: Inverter Power Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Jul 17, 2024
1 parent 1672353 commit b23482e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 51 deletions.
2 changes: 0 additions & 2 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'}'}")

Expand Down
6 changes: 6 additions & 0 deletions custom_components/solarman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 6 additions & 11 deletions custom_components/solarman/inverter_definitions/deye_sg01hp3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 6 additions & 11 deletions custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
19 changes: 8 additions & 11 deletions custom_components/solarman/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -44,27 +46,22 @@ 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:
_LOGGER.debug(f"async_unload_entry: {config.options}")
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.
Expand All @@ -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()

Expand Down

0 comments on commit b23482e

Please sign in to comment.