From d79aa229573dc7e9538ec7bb74a035cd7a1aa0ee Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Sat, 23 Mar 2019 09:52:01 -0500 Subject: [PATCH] Handle errors in storage methods Address problem noted in https://github.com/home-assistant/home-assistant/issues/19982. Fix will also require an update to HA amcrest component to bump the version requirement of this package. --- src/amcrest/storage.py | 37 +++++++++++++++++++------------------ src/amcrest/utils.py | 5 ++--- src/amcrest/video.py | 4 ++-- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/amcrest/storage.py b/src/amcrest/storage.py index 0a4935f..b1f2069 100644 --- a/src/amcrest/storage.py +++ b/src/amcrest/storage.py @@ -10,7 +10,8 @@ # GNU General Public License for more details. # # vim:sw=4:ts=4:et -from amcrest.utils import to_unit, percent +from .exceptions import AmcrestError +from .utils import to_unit, percent, pretty class Storage(object): @@ -29,26 +30,26 @@ def storage_device_names(self): ) return ret.content.decode('utf-8') - @property - def storage_used(self, dev='/dev/mmc0', unit='GB'): - ret = self.storage_device_info - - # pylint: disable=fixme - # TODO - # Use regex to enhance the filter - status = [s for s in ret.split() if '.UsedBytes=' in s][0] - return to_unit(status.split('=')[-1], unit) + def _extract_storage_value(self, param, unit): + try: + return to_unit( + pretty([part for part in self.storage_device_info.split() + if '.{}='.format(param) in part][0]), + unit) + except (AmcrestError, AttributeError, IndexError): + return 'unknown', 'GB' @property - def storage_total(self, dev='/dev/mmc0', unit='GB'): - ret = self.storage_device_info + def storage_used(self): + return self._extract_storage_value('UsedBytes', 'GB') - # pylint: disable=fixme - # TODO - # Use regex to enhance the filter - status = [s for s in ret.split() if '.TotalBytes=' in s][0] - return to_unit(status.split('=')[-1], unit) + @property + def storage_total(self): + return self._extract_storage_value('TotalBytes', 'GB') @property def storage_used_percent(self): - return percent(self.storage_used[0], self.storage_total[0]) + try: + return percent(self.storage_used[0], self.storage_total[0]) + except (ValueError, ZeroDivisionError): + return 'unknown' diff --git a/src/amcrest/utils.py b/src/amcrest/utils.py index 4472d7b..d8ec601 100644 --- a/src/amcrest/utils.py +++ b/src/amcrest/utils.py @@ -35,8 +35,7 @@ def pretty(value, delimiter='='): def percent(part, whole): """Convert data to percent""" - result = 100 * float(part) / float(whole) - return float('{:.{prec}f}'.format(result, prec=PRECISION)) + return round(100 * float(part) / float(whole), PRECISION) def str2bool(value): @@ -64,7 +63,7 @@ def to_unit(value, unit='B'): if unit in byte_array: result = value / 1024**byte_array.index(unit) - return (float('{:.{prec}f}'.format(result, prec=PRECISION)), unit) + return round(result, PRECISION), unit return value diff --git a/src/amcrest/video.py b/src/amcrest/video.py index c1ff996..560777c 100644 --- a/src/amcrest/video.py +++ b/src/amcrest/video.py @@ -132,7 +132,7 @@ def video_in_options(self): def video_in_option(self, param, profile='Day'): """ Return video input option. - + Params: param - parameter, such as 'DayNightColor' profile - 'Day', 'Night' or 'Normal' @@ -160,7 +160,7 @@ def set_video_in_option(self, param, value, profile='Day'): def day_night_color(self): """ Return Day & Night Color Mode for Day profile. - + Result is 0: always multicolor 1: autoswitch along with brightness 2: always monochrome