Skip to content

Commit

Permalink
Handle errors in storage methods
Browse files Browse the repository at this point in the history
Address problem noted in home-assistant/core#19982. Fix will also require an update to HA amcrest component to bump the version requirement of this package.
  • Loading branch information
pnbruckner committed Mar 23, 2019
1 parent 3d0c79f commit d79aa22
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
37 changes: 19 additions & 18 deletions src/amcrest/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'
5 changes: 2 additions & 3 deletions src/amcrest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/amcrest/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d79aa22

Please sign in to comment.