Skip to content

Commit

Permalink
asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed Jan 10, 2025
1 parent f64d218 commit 0a87bf2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion hass-addon-sunsynk-multi/Dockerfile.local
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FROM ghcr.io/kellerza/hass-addon-sunsynk-multi:edge
# FROM ghcr.io/kellerza/hass-addon-sunsynk-multi:0.7.5
FROM ghcr.io/kellerza/hass-addon-sunsynk-multi:7a2597f
FROM ghcr.io/kellerza/hass-addon-sunsynk-multi:b62a03d


# Install sunsynk from local source
Expand Down
13 changes: 5 additions & 8 deletions src/ha_addon_sunsynk_multi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import asyncio
import logging
from asyncio.events import AbstractEventLoop

from ha_addon_sunsynk_multi.a_inverter import STATE
from ha_addon_sunsynk_multi.a_sensor import MQTT, SS_TOPIC
Expand All @@ -23,9 +22,9 @@
_LOGGER = logging.getLogger(__name__)


async def main_loop(loop: AbstractEventLoop) -> None:
async def main_loop() -> None:
"""Main async loop."""
loop.set_debug(OPT.debug > 0)
asyncio.get_event_loop().set_debug(OPT.debug > 0)

# MQTT availability will always use first inverter's serial
MQTT.availability_topic = f"{SS_TOPIC}/{OPT.inverters[0].serial_nr}/availability"
Expand All @@ -34,11 +33,11 @@ async def main_loop(loop: AbstractEventLoop) -> None:
AsyncCallback(name="discovery_info", every=5, callback=callback_discovery_info)
)

for idx, ist in enumerate(STATE):
for ist in STATE:
try:
await ist.connect()
await ist.hass_discover_sensors()
ist.cb = build_callback_schedule(ist=ist, idx=idx)
ist.cb = build_callback_schedule(ist)
CALLBACKS.append(ist.cb)
except (ConnectionError, ValueError) as err:
ist.log_bold(str(err))
Expand Down Expand Up @@ -70,9 +69,7 @@ def main() -> None:
for ist in STATE:
ist.init_sensors()

loop = asyncio.get_event_loop_policy().get_event_loop()
loop.run_until_complete(main_loop(loop))
loop.close()
asyncio.run(main_loop())


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions src/ha_addon_sunsynk_multi/sensor_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def _print_table(
_LOGGER.info("%s\n%s", title, tab.get_string())


def build_callback_schedule(ist: AInverter, idx: int) -> AsyncCallback:
def build_callback_schedule(ist: AInverter) -> AsyncCallback:
"""Build the callback schedule."""
# pylint: disable=too-many-branches,too-many-statements
read_s, report_s = _build_schedules(idx)
read_s, report_s = _build_schedules(ist.index)

async def callback_sensor(now: int) -> None:
"""read or write sensors"""
Expand Down

0 comments on commit 0a87bf2

Please sign in to comment.