diff --git a/esphome/dashboard/dashboard.py b/esphome/dashboard/dashboard.py index 1575d9f9b1..f0e9604141 100644 --- a/esphome/dashboard/dashboard.py +++ b/esphome/dashboard/dashboard.py @@ -515,45 +515,45 @@ class MDNSStatusThread(threading.Thread): class PingStatusThread(threading.Thread): def run(self): - pool = multiprocessing.Pool(processes=8) - while not STOP_EVENT.is_set(): - # Only do pings if somebody has the dashboard open + with multiprocessing.Pool(processes=8) as pool: + while not STOP_EVENT.is_set(): + # Only do pings if somebody has the dashboard open - def callback(ret): - PING_RESULT[ret[0]] = ret[1] + def callback(ret): + PING_RESULT[ret[0]] = ret[1] - entries = _list_dashboard_entries() - queue = collections.deque() - for entry in entries: - if entry.address is None: - PING_RESULT[entry.filename] = None - continue + entries = _list_dashboard_entries() + queue = collections.deque() + for entry in entries: + if entry.address is None: + PING_RESULT[entry.filename] = None + continue - result = pool.apply_async( - _ping_func, (entry.filename, entry.address), callback=callback - ) - queue.append(result) + result = pool.apply_async( + _ping_func, (entry.filename, entry.address), callback=callback + ) + queue.append(result) - while queue: - item = queue[0] - if item.ready(): - queue.popleft() - continue + while queue: + item = queue[0] + if item.ready(): + queue.popleft() + continue - try: - item.get(0.1) - except OSError: - # ping not installed - pass - except multiprocessing.TimeoutError: - pass + try: + item.get(0.1) + except OSError: + # ping not installed + pass + except multiprocessing.TimeoutError: + pass - if STOP_EVENT.is_set(): - pool.terminate() - return + if STOP_EVENT.is_set(): + pool.terminate() + return - PING_REQUEST.wait() - PING_REQUEST.clear() + PING_REQUEST.wait() + PING_REQUEST.clear() class PingRequestHandler(BaseHandler): diff --git a/esphome/espota2.py b/esphome/espota2.py index 785cb155df..351f6feda9 100644 --- a/esphome/espota2.py +++ b/esphome/espota2.py @@ -296,15 +296,14 @@ def run_ota_impl_(remote_host, remote_port, password, filename): _LOGGER.error("Connecting to %s:%s failed: %s", remote_host, remote_port, err) return 1 - file_handle = open(filename, "rb") - try: - perform_ota(sock, password, file_handle, filename) - except OTAError as err: - _LOGGER.error(str(err)) - return 1 - finally: - sock.close() - file_handle.close() + with open(filename, "rb") as file_handle: + try: + perform_ota(sock, password, file_handle, filename) + except OTAError as err: + _LOGGER.error(str(err)) + return 1 + finally: + sock.close() return 0 diff --git a/esphome/helpers.py b/esphome/helpers.py index d9730f96a7..ad7b8272b2 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -60,10 +60,10 @@ def cpp_string_escape(string, encoding="utf-8"): def run_system_command(*args): import subprocess - p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = p.communicate() - rc = p.returncode - return rc, stdout, stderr + with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p: + stdout, stderr = p.communicate() + rc = p.returncode + return rc, stdout, stderr def mkdir_p(path): diff --git a/requirements_test.txt b/requirements_test.txt index 9e4f175852..e612d5871b 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,4 +1,4 @@ -pylint==2.7.2 +pylint==2.8.2 flake8==3.9.0 black==21.5b0 pillow>4.0.0