From 3c9c834e34afe32051cf2139268fa5ea63678f14 Mon Sep 17 00:00:00 2001 From: Brian Martin Date: Fri, 14 Jun 2019 09:11:52 -0400 Subject: [PATCH] change str_to_bool func Change str_to_bool func allowing for providing a CA Cert - Useful for Bumper --- sucks/__init__.py | 17 +++++++++++++---- tests/test_vacbot.py | 9 ++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/sucks/__init__.py b/sucks/__init__.py index da326f4..ed1efb1 100644 --- a/sucks/__init__.py +++ b/sucks/__init__.py @@ -10,6 +10,7 @@ import random import ssl import requests import stringcase +import os from sleekxmpp import ClientXMPP, Callback, MatchXPath from sleekxmpp.xmlstream import ET @@ -123,13 +124,21 @@ COMPONENT_FROM_ECOVACS = { 'dust_case_heap': COMPONENT_FILTER } -def str_to_bool(s): +def str_to_bool_or_cert(s): if s == 'True' or s == True: return True elif s == 'False' or s == False: return False else: - raise ValueError("Cannot covert {} to a bool".format(s)) + if not s == None: + if os.path.exists(s): # User could provide a path to a CA Cert as well, which is useful for Bumper + if os.path.isfile(s): + return s + else: + raise ValueError("Certificate path provided is not a file - {}".format(s)) + + raise ValueError("Cannot covert {} to a bool or certificate path".format(s)) + class EcoVacsAPI: CLIENT_KEY = "eJUWrzRv34qFSaYk" @@ -161,7 +170,7 @@ class EcoVacsAPI: #'deviceType': '2' - iphone } - self.verify_ssl = str_to_bool(verify_ssl) + self.verify_ssl = str_to_bool_or_cert(verify_ssl) _LOGGER.debug("Setting up EcoVacsAPI") self.resource = device_id[0:8] self.country = country @@ -633,7 +642,7 @@ class EcoVacsIOTMQ(ClientMQTT): self.vacuum = vacuum self.scheduler = sched.scheduler(time.time, time.sleep) self.scheduler_thread = threading.Thread(target=self.scheduler.run, daemon=True, name="mqtt_schedule_thread") - self.verify_ssl = str_to_bool(verify_ssl) + self.verify_ssl = str_to_bool_or_cert(verify_ssl) if server_address is None: self.hostname = ('mq-{}.ecouser.net'.format(self.continent)) diff --git a/tests/test_vacbot.py b/tests/test_vacbot.py index 8bb66b2..b57caac 100644 --- a/tests/test_vacbot.py +++ b/tests/test_vacbot.py @@ -388,5 +388,12 @@ def a_vacbot(bot=None, iotmq=False, monitor=False): bot, 'na', monitor=monitor) def test_str_to_bool(): - assert_raises(ValueError, str_to_bool, None) #Value error if str_to_bool can't convert + assert_raises(ValueError, str_to_bool_or_cert, None) #Value error if str_to_bool can't convert + assert_equals(True, str_to_bool_or_cert("True")) + assert_equals(False, str_to_bool_or_cert("False")) + assert_equals( + os.path.abspath(os.path.join(".", "tests", "test_vacbot.py")), + str_to_bool_or_cert(os.path.abspath(os.path.join(".","tests","test_vacbot.py"))) + ) + assert_raises(ValueError, str_to_bool_or_cert ,(os.path.abspath(os.path.join(".","tests")))) \ No newline at end of file