Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def command(self, cmd, retries=None, timeout_cmd=None, stream=False):
"""amcrest.Http.command wrapper to catch errors."""
try:
ret = super().command(cmd, retries, timeout_cmd, stream)
except AmcrestError:
with self._wrap_lock:
was_online = self.available
self._wrap_errors += 1
_LOGGER.debug("%s camera errs: %i", self._wrap_name, self._wrap_errors)
offline = not self.available
if offline and was_online:
_LOGGER.error("%s camera offline: Too many errors", self._wrap_name)
with self._token_lock:
self._token = None
dispatcher_send(
self._hass, service_signal(SERVICE_UPDATE, self._wrap_name)
)
self._unsub_recheck = track_time_interval(
self._hass, self._wrap_test_online, RECHECK_INTERVAL
)
raise
elif self._sensor_type == SENSOR_SDCARD:
storage = self._api.storage_all
try:
self._attrs["Total"] = "{:.2f} {}".format(*storage["total"])
except ValueError:
self._attrs["Total"] = "{} {}".format(*storage["total"])
try:
self._attrs["Used"] = "{:.2f} {}".format(*storage["used"])
except ValueError:
self._attrs["Used"] = "{} {}".format(*storage["used"])
try:
self._state = "{:.2f}".format(storage["used_percent"])
except ValueError:
self._state = storage["used_percent"]
except AmcrestError as error:
log_update_error(_LOGGER, "update", self.name, "sensor", error)
def update(self):
"""Update entity."""
if not self.available:
return
_LOGGER.debug("Updating %s binary sensor", self._name)
try:
if self._sensor_type == BINARY_SENSOR_MOTION_DETECTED:
self._state = self._api.is_motion_detected
elif self._sensor_type == BINARY_SENSOR_ONLINE:
self._state = self._api.available
except AmcrestError as error:
log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
def update(self):
"""Update setting state."""
if not self.available:
return
_LOGGER.debug("Updating %s switch", self._name)
try:
if self._setting == MOTION_DETECTION:
detection = self._api.is_motion_detector_on()
elif self._setting == MOTION_RECORDING:
detection = self._api.is_record_on_motion_detection()
self._state = detection
except AmcrestError as error:
log_update_error(_LOGGER, "update", self.name, "switch", error)
def _set_color_bw(self, cbw):
"""Set camera color mode."""
try:
self._api.day_night_color = _CBW.index(cbw)
except AmcrestError as error:
log_update_error(
_LOGGER, "set", self.name, f"camera color mode to {cbw}", error
)
else:
self._color_bw = cbw
self.schedule_update_ha_state()