Skip to content

Commit

Permalink
(optionally) set controller event-listener if incorrect (cf. #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Mar 13, 2024
1 parent fc028a8 commit 6c4e6e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
- https://developers.home-assistant.io/blog/2020/05/08/logos-custom-integrations/
- https://github.com/home-assistant/brands

- [ ] Set controller event listener
- [x] Set controller event listener
- [x] config-flow
- [x] options-flow
- [x] controller-info attribute
- [ ] events-coordinator
- [x] events-coordinator

- [ ] DataCoordinator
- [ ] communalize data
Expand Down
26 changes: 26 additions & 0 deletions custom_components/uhppoted/coordinators/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import concurrent.futures
import threading
from ipaddress import IPv4Address
from dataclasses import dataclass

import async_timeout
Expand Down Expand Up @@ -29,6 +30,7 @@
from uhppoted.decode import unpack_bool

from ..const import CONF_LISTEN_ADDR
from ..const import CONF_EVENTS_DEST_ADDR
from ..const import ATTR_AVAILABLE
from ..const import ATTR_EVENTS
from ..const import ATTR_STATUS
Expand Down Expand Up @@ -125,6 +127,7 @@ def __init__(self, hass, options, poll, db, notify):
self._options = options
self._db = db
self._notify = notify
self._listener_addr = options.get(CONF_EVENTS_DEST_ADDR, None)
self._initialised = False
self._state = {
'events': {},
Expand Down Expand Up @@ -198,6 +201,7 @@ async def _get_events(self, contexts):
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
executor.map(lambda controller: self._record_special_events(api, lock, controller), contexts, timeout=1)
executor.map(lambda controller: self._set_event_listener(api, lock, controller), contexts, timeout=1)
executor.map(lambda controller: self._get_controller_events(api, lock, controller), contexts, timeout=1)
except Exception as err:
_LOGGER.error(f'error retrieving controller {controller} information ({err})')
Expand All @@ -216,6 +220,28 @@ def _record_special_events(self, api, lock, controller):
except Exception as err:
_LOGGER.warning(f'error enabling controller {controller} record special events ({err})')

def _set_event_listener(self, api, lock, controller):
if self._listener_addr != None:
_LOGGER.debug(f'check controller {controller} event listener')

try:
response = api.get_listener(controller)
if response.controller == controller:
addr = f'{response.address}:{response.port}'
if addr != self._listener_addr:
_LOGGER.warning(f'controller {controller} incorrect event listener address ({addr})')
host, port = self._listener_addr.split(':')
response = api.set_listener(controller, IPv4Address(host), int(port))
if response.controller == controller:
if response.ok:
_LOGGER.warning(
f'controller {controller} event listener address updated ({self._listener_addr})')
else:
_LOGGER.warning(f'failed to set controller {controller} event listener address')

except Exception as err:
_LOGGER.warning(f'error setting controller {controller} event listener ({err})')

def _get_controller_events(self, api, lock, controller):
_LOGGER.debug(f'fetch controller {controller} events')

Expand Down

0 comments on commit 6c4e6e6

Please sign in to comment.