Now using threading for running the bulk action

This commit is contained in:
BlueFox 2025-05-17 22:49:55 +02:00
parent 44b48485be
commit 023b29fcae

View File

@ -20,6 +20,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse # to parse the cli args! import argparse # to parse the cli args!
import requests # needed to access the http endpoints import requests # needed to access the http endpoints
import threading # for running the requests simultaneously
import ipaddress # to validate IP addresses import ipaddress # to validate IP addresses
from fqdn import FQDN # validate FQDNs from fqdn import FQDN # validate FQDNs
@ -166,7 +167,7 @@ class TasmotonovRunner:
def run_custom_action(self, action): def run_custom_action(self, action):
# and finally turn on or off or toggle the lights! # 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}') self.logger.log(f'Running the action "{action}" on the following device: {address}')
try: try:
answer = requests.get(f'http://{address}/cm?cmnd=Power%20{str(action).lower()}') answer = requests.get(f'http://{address}/cm?cmnd=Power%20{str(action).lower()}')
@ -174,11 +175,22 @@ class TasmotonovRunner:
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
self.logger.log_warning(f'Could not connect to "{address}". Skipping...') self.logger.log_warning(f'Could not connect to "{address}". Skipping...')
success = False success = False
if answer.status_code != 200: if not str(answer.status_code).startswith("20"):
self.logger.log_warning(f'{address} returned the following non-200 status code: {answer.status_code}. Skipping...') self.logger.log_warning(f'{address} returned the following non-20x status code: {answer.status_code}. Skipping...')
success = False success = False
if success: if success:
self.logger.log_success(f"Ran action {str(action).upper()} on {self.logger.COLORS['OKCYAN']}{address}{self.logger.COLORS['ENDC']} successfully.") 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): def run(self):
self.run_custom_action(self.action) self.run_custom_action(self.action)