Skip to content

Commit

Permalink
0.5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed Aug 27, 2023
1 parent 4151a99 commit 233b11f
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 11 deletions.
2 changes: 1 addition & 1 deletion hass-addon-sunsynk-multi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## **2023.08.27-0.5.6**
## **2023.08.27-0.5.7**

- Bugfix: read_batch_size
- 5ms delay before reads
Expand Down
2 changes: 1 addition & 1 deletion hass-addon-sunsynk-multi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG BUILD_FROM
FROM ${BUILD_FROM}

RUN pip3 install sunsynk[pymodbus,umodbus,solarman]==0.5.6 --no-cache-dir --disable-pip-version-check
RUN pip3 install sunsynk[pymodbus,umodbus,solarman]==0.5.7 --no-cache-dir --disable-pip-version-check

# Install sunsynk from local source
#! COPY sunsynk sunsynk
Expand Down
2 changes: 1 addition & 1 deletion hass-addon-sunsynk-multi/Dockerfile.local
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG BUILD_FROM
FROM ${BUILD_FROM}

# RUN pip3 install sunsynk[pymodbus,umodbus,solarman]==0.5.6 --no-cache-dir --disable-pip-version-check
# RUN pip3 install sunsynk[pymodbus,umodbus,solarman]==0.5.7 --no-cache-dir --disable-pip-version-check

# Install sunsynk from local source
COPY sunsynk sunsynk
Expand Down
2 changes: 1 addition & 1 deletion hass-addon-sunsynk-multi/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Sunsynk/Deye Inverter Add-on (multi)
version: 2023.08.27-0.5.6
version: 2023.08.27-0.5.7
slug: hass-addon-sunsynk-multi
description: Add-on for a Sunsynk/Deye branded Inverter
startup: services
Expand Down
14 changes: 8 additions & 6 deletions src/ha_addon_sunsynk_multi/sensor_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ async def callback_sensor(now: int) -> None:
if chg and asen.opt.schedule.change_any:
pub[asen] = hist[-1]
elif sensor in ist.inv.state.history:
last = ist.inv.state.history[sensor][-1]
if asen.opt.schedule.significant_change(
history=ist.inv.state.history[sensor][:-1],
last=ist.inv.state.history[sensor][-1],
last=last,
):
pub[asen] = ist.inv.state.history[sensor][-1]
ist.inv.state.history[sensor].clear()
ist.inv.state.history[sensor].append(last)
pub[asen] = last

# check fixed reporting
for sec, srun in report_s.items():
Expand All @@ -142,11 +145,10 @@ async def callback_sensor(now: int) -> None:
pub[asen] = ist.inv.state.historynn[sensor][-1]
continue
# average value is n
nhist = ist.inv.state.history[sensor]
if not nhist:
try:
pub[asen] = ist.inv.state.history_average(sensor)
except ValueError:
_LOGGER.warning("No history for %s", sensor)
continue
pub[asen] = sum(nhist) / len(nhist)
srun.next_run = now + sec

if pub:
Expand Down
2 changes: 1 addition & 1 deletion src/sunsynk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# pylint: enable=unused-import

VERSION = "0.5.6"
VERSION = "0.5.7"

CELSIUS: Final = "°C"
KWH: Final = "kWh"
Expand Down
10 changes: 10 additions & 0 deletions src/sunsynk/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ def update(self, new_regs: dict[int, int]) -> None:
for sen, (new, old) in changed.items():
self.onchange(sen, new, old)

def history_average(self, sensor: Sensor) -> NumType:
"""Return the average of the history."""
hist0, *hist = self.history[sensor] # raises ValueError if no history
if not hist:
return hist0
res = sum(hist) / len(hist)
self.history[sensor].clear()
self.history[sensor].append(res)
return res

# def history_done(self) -> None:
# """Flush the history."""
# self.history.clear()
Expand Down
65 changes: 65 additions & 0 deletions src/tests/sunsynk/test_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Sunsynk sensor state."""
import logging

import pytest

from sunsynk.rwsensors import SystemTimeRWSensor
from sunsynk.sensors import Sensor
from sunsynk.state import InverterState

_LOGGER = logging.getLogger(__name__)


def test_history(state: InverterState) -> None:
"""Test history with numeric values."""
a = Sensor(1, "Some Value")
state.track(a)

state.update({1: 100})
assert state[a] == 100
assert state.history[a] == [100]

state.update({1: 200})
assert state[a] == 200
assert state.history[a] == [100, 200]

state.update({1: 300})
assert state[a] == 300
assert state.history[a] == [100, 200, 300]

assert a not in state.historynn

assert state.history_average(a) == 250
assert state.history[a] == [250]


def test_history_raise(state: InverterState) -> None:
"""Test if we have a ValueError."""
a = Sensor(2, "Some Value")
state.track(a)
with pytest.raises(ValueError):
state.history_average(a)

state.update({2: 100})
assert state[a] == 100
assert state.history_average(a) == 100
assert state.history[a] == [100]

state.update({2: 111})
assert state.history[a] == [100, 111]
assert state.history_average(a) == 111
assert state.history[a] == [111]


def test_history_nn(state: InverterState) -> None:
"""Test history with non-numeric values."""
a = SystemTimeRWSensor((1, 2, 3), "Some Value")
state.track(a)

state.update({1: 1, 2: 2, 3: 3})
assert state.historynn[a] == [None, "2000-01-00 2:00:03"]
assert a not in state.history

state.update({1: 12, 2: 5, 3: 44})
assert state.historynn[a] == ["2000-01-00 2:00:03", "2000-12-00 5:00:44"]
assert a not in state.history

0 comments on commit 233b11f

Please sign in to comment.