Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate async and sync methods #248

Merged
merged 1 commit into from
May 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Separate async and sync methods
MartinHjelmare committed May 3, 2020
commit 7511e723640adf1ca99daa809ad45245ef173c3b
8 changes: 6 additions & 2 deletions mysensors/cli/gateway_mqtt.py
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@ async def async_start_mqtt_client(loop, broker, port):
return mqttc


class MQTTClient:
class BaseMQTTClient:
"""MQTT client."""

def __init__(self, broker, port=1883, keepalive=60):
@@ -151,6 +151,10 @@ def message_callback(mqttc, userdata, msg):
self._client.message_callback_add(topic, message_callback)
self.topics[topic] = callback


class MQTTClient(BaseMQTTClient):
"""MQTT client."""

def start(self):
"""Run the MQTT client."""
_LOGGER.info("Start MQTT client")
@@ -164,7 +168,7 @@ def stop(self):
self._client.loop_stop()


class AsyncMQTTClient(MQTTClient):
class AsyncMQTTClient(BaseMQTTClient):
"""Async MQTT client."""

def __init__(self, loop, *args):
12 changes: 6 additions & 6 deletions mysensors/gateway_mqtt.py
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ def parse_message_to_mqtt(self, data):
# prefix/node/child/type/ack/subtype : payload
return "/{}".format(msg.encode("/"))[:-2], payload, msg.ack

def get_gateway_id(self):
def _get_gateway_id(self):
"""Return a unique id for the gateway."""
return (
self.tasks.transport.in_prefix if self.tasks.transport.in_prefix else None
@@ -128,6 +128,10 @@ def __init__(
)
super().__init__(transport, **kwargs)

def get_gateway_id(self):
"""Return a unique id for the gateway."""
return self._get_gateway_id()


class AsyncMQTTGateway(BaseAsyncGateway, BaseMQTTGateway):
"""MySensors async MQTT client gateway."""
@@ -157,7 +161,7 @@ def __init__(

async def get_gateway_id(self):
"""Return a unique id for the gateway."""
return super().get_gateway_id()
return self._get_gateway_id()


class MQTTTransport(Transport):
@@ -187,10 +191,6 @@ def __init__(
# prefix/node/child/type/ack/subtype : payload
self.gateway = gateway

def connect(self):
"""Connect to the transport."""
raise NotImplementedError

def disconnect(self):
"""Disconnect from the transport.
8 changes: 6 additions & 2 deletions mysensors/gateway_serial.py
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ def __init__(self, port, baud=115200, **kwargs):
self.port = port
self.baud = baud

def get_gateway_id(self):
def _get_gateway_id(self):
"""Return a unique id for the gateway."""
info = next(serial.tools.list_ports.grep(self.port), None)
return info.serial_number if info is not None else None
@@ -37,6 +37,10 @@ def __init__(self, *args, **kwargs):
transport = SyncTransport(self, sync_connect, **kwargs)
super().__init__(transport, *args, **kwargs)

def get_gateway_id(self):
"""Return a unique id for the gateway."""
return self._get_gateway_id()


def sync_connect(transport):
"""Connect to the serial port.
@@ -79,7 +83,7 @@ def __init__(self, *args, loop=None, **kwargs):
async def get_gateway_id(self):
"""Return a unique id for the gateway."""
serial_number = await self.tasks.loop.run_in_executor(
None, super().get_gateway_id
None, self._get_gateway_id
)
return serial_number

8 changes: 6 additions & 2 deletions mysensors/gateway_tcp.py
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ def _handle_i_version(self, msg): # pylint: disable=useless-return
self.tcp_disconnect_timer = time.time()
return None

def get_gateway_id(self):
def _get_gateway_id(self):
"""Return a unique id for the gateway."""
host, _ = self.server_address
try:
@@ -75,6 +75,10 @@ def __init__(self, *args, **kwargs):
transport = SyncTransport(self, sync_connect, **kwargs)
super().__init__(transport, *args, **kwargs)

def get_gateway_id(self):
"""Return a unique id for the gateway."""
return self._get_gateway_id()


def sync_connect(transport):
"""Connect to socket. This should be run in a new thread."""
@@ -142,7 +146,7 @@ def check_connection(self):

async def get_gateway_id(self):
"""Return a unique id for the gateway."""
mac = await self.tasks.loop.run_in_executor(None, super().get_gateway_id)
mac = await self.tasks.loop.run_in_executor(None, self._get_gateway_id)
return mac


16 changes: 0 additions & 16 deletions mysensors/task.py
Original file line number Diff line number Diff line change
@@ -70,22 +70,6 @@ def run_job(self, job=None):
)
return reply

def start(self):
"""Start the gateway and task allow tasks to be scheduled."""
raise NotImplementedError

def stop(self):
"""Stop the gateway and stop allowing tasks for the scheduler."""
raise NotImplementedError

def start_persistence(self):
"""Start persistence."""
raise NotImplementedError

def update_fw(self, nids, fw_type, fw_ver, fw_path=None):
"""Update firwmare."""
raise NotImplementedError


class SyncTasks(Tasks):
"""Sync version of tasks class."""
4 changes: 0 additions & 4 deletions mysensors/transport.py
Original file line number Diff line number Diff line change
@@ -27,10 +27,6 @@ def __init__(self, gateway, connect, timeout=1.0, reconnect_timeout=10.0, **kwar
self.reconnect_timeout = reconnect_timeout
self.timeout = timeout

def connect(self):
"""Connect to the transport."""
raise NotImplementedError

def disconnect(self):
"""Disconnect from the transport."""
if not self.protocol or not self.protocol.transport: