Skip to content

Commit

Permalink
connect() as separate method (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindwe authored Oct 23, 2022
1 parent 88f6ccb commit 32fc855
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ This system/service/software is not officially supported or endorsed by Glen Dim
# or full serial and IP if you do not want to discover on UDP:
hub = nobo('123123123123', ip='10.0.0.128', discover=False, synchronous=False)

# Connect to the hub
await hub.start()
# Connect to the hub and get initial data
await hub.connect()

# Inspect what you get
def update(hub):
Expand All @@ -29,12 +29,16 @@ This system/service/software is not officially supported or endorsed by Glen Dim
print(hub.overrides)
print(hub.temperatures)

# Read the initial data
update(hub)

# Listen for data updates - register before getting initial data to avoid race condition
hub.register_callback(callback=update)

# Get initial data
update(hub)

# Start the background tasks for reading responses and keep connction alive
# This will connect to the hub if necessary
await hub.start()

# Hang around and wait for data updates
await asyncio.sleep(60)

Expand All @@ -59,7 +63,7 @@ It is possible to discover hubs on the local network, and also test connectivity
# Test connection to the first
(serial, ip) = hubs.pop()
hub = nobo(serial + '123', ip=ip, discover=False, synchronous=False)
await hub.async_connect_hub(ip=test_ip, serial=self.serial)
await hub.connect()

# Then start the background tasks
await hub.start()
Expand All @@ -80,7 +84,7 @@ If the connection is lost, it will attempt to reconnect.

### Command Functions

These functions sends commands to the hub.
These functions send commands to the hub.

* async_send_command - Send a list of command string(s) to the hub
* async_create_override - Override hub/zones/components
Expand All @@ -95,6 +99,7 @@ not perform any I/O, and can safely be called from the event loop.
* get_current_zone_mode - Get the mode of a zone at a certain time
* get_current_component_temperature - Get the current temperature from a component
* get_current_zone_temperature - Get the current temperature from (the first component in) a zone
* get_zone_override_mode - Get the override mode for the zone

## Backwards compatibility

Expand Down
61 changes: 32 additions & 29 deletions pynobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,39 +355,42 @@ def deregister_callback(self, callback=lambda *args, **kwargs: None):
"""
self._callbacks.remove(callback)

async def start(self):
"""Discover Ecohub and start the TCP client."""
async def connect(self):
"""Connect to Ecohub, either by scanning or directly."""
connected = False
if self.discover:
_LOGGER.info('Looking for Nobø Ecohub with serial: %s and ip: %s', self.serial, self.ip)
discovered_hubs = await self.async_discover_hubs(serial=self.serial, ip=self.ip)
if not discovered_hubs:
_LOGGER.error('Failed to discover any Nobø Ecohubs')
raise Exception('Failed to discover any Nobø Ecohubs')
while discovered_hubs:
(discover_ip, discover_serial) = discovered_hubs.pop()
connected = await self.async_connect_hub(discover_ip, discover_serial)
if connected:
break # We connect to the first valid hub, no reason to try the rest
else:
# check if we have an IP
if not self.ip:
_LOGGER.error('Could not connect, no ip address provided')
raise ValueError('Could not connect, no ip address provided')

if not self._writer:
# Get a socket connection, either by scanning or directly
connected = False
if self.discover:
_LOGGER.info('Looking for Nobø Ecohub with serial: %s and ip: %s', self.serial, self.ip)
discovered_hubs = await self.async_discover_hubs(serial=self.serial, ip=self.ip)
if not discovered_hubs:
_LOGGER.error('Failed to discover any Nobø Ecohubs')
raise Exception('Failed to discover any Nobø Ecohubs')
while discovered_hubs:
(discover_ip, discover_serial) = discovered_hubs.pop()
connected = await self.async_connect_hub(discover_ip, discover_serial)
if connected:
break # We connect to the first valid hub, no reason to try the rest
else:
# check if we have an IP
if not self.ip:
_LOGGER.error('Could not connect, no ip address provided')
raise ValueError('Could not connect, no ip address provided')
# check if we have a valid serial before we start connection
if len(self.serial) != 12:
_LOGGER.error('Could not connect, no valid serial number provided')
raise ValueError('Could not connect, no valid serial number provided')

# check if we have a valid serial before we start connection
if len(self.serial) != 12:
_LOGGER.error('Could not connect, no valid serial number provided')
raise ValueError('Could not connect, no valid serial number provided')
connected = await self.async_connect_hub(self.ip, self.serial)

connected = await self.async_connect_hub(self.ip, self.serial)
if not connected:
_LOGGER.error('Could not connect to Nobø Ecohub')
raise Exception(f'Failed to connect to Nobø Ecohub with serial: {self.serial} and ip: {self.ip}')

if not connected:
_LOGGER.error('Could not connect to Nobø Ecohub')
raise Exception(f'Failed to connect to Nobø Ecohub with serial: {self.serial} and ip: {self.ip}')
async def start(self):
"""Discover Ecohub and start the TCP client."""

if not self._writer:
await self.connect()

# Start the tasks to send keep-alive and receive data
self._keep_alive_task = asyncio.create_task(self.keep_alive())
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# For a discussion on single-sourcing the version across setup.py and the
# project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.5.0',
version='1.6.0',
description='Nobø Hub / Nobø Energy Control TCP/IP Interface',

license='GPLv3+',
Expand Down

0 comments on commit 32fc855

Please sign in to comment.