diff --git a/tasmotonov.py b/tasmotonov.py index c16835a..443ad1a 100755 --- a/tasmotonov.py +++ b/tasmotonov.py @@ -20,6 +20,7 @@ along with this program. If not, see . import argparse # to parse the cli args! import requests # needed to access the http endpoints +import threading # for running the requests simultaneously import ipaddress # to validate IP addresses from fqdn import FQDN # validate FQDNs @@ -166,7 +167,7 @@ class TasmotonovRunner: def run_custom_action(self, action): # and finally turn on or off or toggle the lights! - for address in list(self.tasmota_addresses.keys()): + def thread_runner(address): self.logger.log(f'Running the action "{action}" on the following device: {address}') try: answer = requests.get(f'http://{address}/cm?cmnd=Power%20{str(action).lower()}') @@ -174,11 +175,22 @@ class TasmotonovRunner: except requests.exceptions.ConnectionError: self.logger.log_warning(f'Could not connect to "{address}". Skipping...') success = False - if answer.status_code != 200: - self.logger.log_warning(f'{address} returned the following non-200 status code: {answer.status_code}. Skipping...') + if not str(answer.status_code).startswith("20"): + self.logger.log_warning(f'{address} returned the following non-20x status code: {answer.status_code}. Skipping...') success = False if success: self.logger.log_success(f"Ran action {str(action).upper()} on {self.logger.COLORS['OKCYAN']}{address}{self.logger.COLORS['ENDC']} successfully.") + + threads = [] + for address in list(self.tasmota_addresses.keys()): + t = threading.Thread(target=thread_runner, args=(address,)) + threads.append(t) + t.start() + # wait for all threads to finish execution + for t in threads: + t.join() + + def run(self): self.run_custom_action(self.action)