Skip to content

Commit 0140e3e

Browse files
committed
table
1 parent 5d59a2b commit 0140e3e

7 files changed

Lines changed: 70 additions & 52 deletions

File tree

‎src/ha_addon_sunsynk_multi/sensor_callback.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Self
88

99
from sunsynk import RWSensor, Sensor, ValType
10-
from sunsynk.utils import pretty_table
10+
from sunsynk.utils import pretty_table, table_data
1111

1212
from .a_inverter import AInverter
1313
from .a_sensor import ASensor, SensorOption
@@ -56,8 +56,11 @@ def build_schedules(self, idx: int) -> Self:
5656

5757
def print_schedule(self, title: str, idx: int, sch: dict[int, SensorRun]) -> None:
5858
"""Print the sensor schedule."""
59-
data = [[e, ", ".join(s.sensor.id for s in r.sensors)] for e, r in sch.items()]
60-
tab = pretty_table(["s", "Sensors"], data)
59+
data = [
60+
{"s": e, "Sensors": ", ".join(s.sensor.id for s in r.sensors)}
61+
for e, r in sch.items()
62+
]
63+
tab = pretty_table(*table_data(data))
6164
inv_ref = ">1" if idx > 1 else "1"
6265
_LOG.debug("%s (inverter %s)\n%s", title, inv_ref, tab.get_string())
6366

‎src/ha_addon_sunsynk_multi/sensor_options.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ def init_sensors(self) -> None:
6868
if not DEFS.all:
6969
import_definitions()
7070
if OPT.overrides:
71-
_LOG.info(
72-
"Applying sensor overrides from configuration: %s", OPT.overrides
73-
)
7471
DEFS.override(OPT.overrides)
7572
self.clear()
7673

‎src/ha_addon_sunsynk_multi/timer_schedule.py‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sunsynk.helpers import slug
88
from sunsynk.rwsensors import RWSensor
99
from sunsynk.sensors import EnumSensor
10-
from sunsynk.utils import pretty_table
10+
from sunsynk.utils import pretty_table, table_data
1111

1212
_LOG = logging.getLogger(__name__)
1313

@@ -121,19 +121,18 @@ def init_schedules(schedules: list[Schedule]) -> None:
121121
# _LOG.info("Added %s", sch)
122122
SCHEDULES[sch.key] = sch
123123

124-
hdr = ["Key", "src", "Read", "Report", "Change by", "Change %", "Change any"]
125124
data = [
126-
[
127-
schn,
128-
source.get(schn, ""),
129-
sch.read_every if sch.read_every else "once",
130-
sch.report_every if sch.report_every else "",
131-
sch.change_by if sch.change_by else "",
132-
sch.change_percent if sch.change_percent else "",
133-
sch.change_any if sch.change_any else "",
134-
]
125+
{
126+
"Key": schn,
127+
"src": source.get(schn, ""),
128+
"Read": sch.read_every if sch.read_every else "once",
129+
"Report": sch.report_every if sch.report_every else "",
130+
"Change by": sch.change_by if sch.change_by else "",
131+
"Change %": sch.change_percent if sch.change_percent else "",
132+
"Change any": sch.change_any if sch.change_any else "",
133+
}
135134
for schn, sch in SCHEDULES.items()
136135
]
137-
tab = pretty_table(hdr, data)
136+
tab = pretty_table(*table_data(data))
138137

139138
_LOG.info("Schedules:\n%s", tab.get_string())

‎src/sunsynk/sensors.py‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
slug,
1515
unpack_value,
1616
)
17+
from sunsynk.utils import pretty_table, table_data
1718

1819
_LOG = logging.getLogger(__name__)
1920
LOG_TRACE = 15 # logging.addLevelName(LOG_TRACE, "TRACE")
@@ -222,30 +223,39 @@ def _copy(old: Sensor) -> Sensor:
222223
setattr(_copy(sen), attrn, news)
223224
return news
224225

226+
info: list[dict[str, str]] = []
227+
225228
for key, val in values.items():
226229
sen_name, _, sen_attr = key.partition(".")
227230
sen = self.all.get(slug(sen_name))
231+
row = {
232+
"Sensor": sen.name if sen else sen_name,
233+
"Attribute": sen_attr,
234+
"Value": str(val),
235+
"Message": "?",
236+
}
237+
info.append(row)
228238
if sen is None:
229-
_LOG.error("Override: Sensor %s not found, skipping", sen_name)
239+
row["Message"] = "✕ Sensor not found"
230240
continue
241+
231242
if sen_attr == "":
232243
if not isinstance(sen, Constant):
233-
_LOG.warning(
234-
"Override for %s is not a Constant sensor, skipping", sen_name
244+
row["Message"] = (
245+
"✕ Empty attributes can only be used with Constants"
235246
)
236247
continue
237248
sen_attr = "value"
238249

239250
if not hasattr(sen, sen_attr):
240-
_LOG.error(
241-
"Override: Sensor %s has no attribute %s, skipping",
242-
sen_name,
243-
sen_attr,
244-
)
251+
row["Message"] = "✕ Attribute not found on target sensor"
245252
continue
246253

247254
setattr(_copy(sen), sen_attr, val)
248-
_LOG.info("Override: Sensor %s.%s set to %s", sen_name, sen_attr, val)
255+
row["Message"] = f"✓ {getattr(sen, sen_attr, '?')} -> {val}"
256+
257+
tab = pretty_table(*table_data(info))
258+
_LOG.info("Applying sensor overrides from configuration\n%s", tab.get_string())
249259

250260

251261
@dataclass(slots=True, eq=False)

‎src/sunsynk/utils.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def table_data[T](
7373
) -> tuple[list[str], list[list[T | None]]]:
7474
"""Convert a list of dictionaries to a table data format."""
7575
if headers is None:
76-
headers = list({k for v in data for k in v.keys()})
76+
hdrs = {}
77+
for row in data:
78+
hdrs.update(row)
79+
headers = list(hdrs)
7780
return headers, [[v.get(k) for k in headers] for v in data]
7881

7982

‎uv.lock‎

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎www/docs/reference/multi-options.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,14 @@ For information about available sensors, refer to the sensor [definitions](./def
142142

143143
```log
144144
[08:19:42] INFO Importing sensor definitions single-phase (view the source online: https://github.com/kellerza/sunsynk/tree/main/src/sunsynk/definitions/single_phase.py )
145-
[08:19:42] INFO Applying sensor overrides from configuration: {'prog4_power.max': 4990}
146-
[08:19:42] INFO Override: Sensor prog4_power.max set to 4990
145+
[08:19:42] INFO Applying sensor overrides from configuration
146+
+-------------+-----------+-------+----------+
147+
| Sensor | Attribute | Value | Message |
148+
+-------------+-----------+-------+----------+
149+
| Serial | trace | 1 | ✓ 0 -> 1 |
150+
| Battery SOC | trace | 1 | ✓ 0 -> 1 |
151+
| Prog5 power | trace | 1 | ✓ 0 -> 1 |
152+
+-------------+-----------+-------+----------+
147153
```
148154

149155
:::

0 commit comments

Comments
 (0)