예제 #1
0
async def test_update_backups_old_names(updater: HaUpdater, server, backup: Backup, time: FakeTime, supervisor: SimulatedSupervisor, config: Config):
    config.override(Setting.CALL_BACKUP_SNAPSHOT, True)
    await updater.update()
    assert not updater._stale()
    assert updater._state() == "backed_up"
    verifyEntity(supervisor, "binary_sensor.snapshots_stale",
                 "off", {"friendly_name": "Snapshots Stale",
                         "device_class": "problem"})
    date = '1985-12-06T05:00:00+00:00'
    verifyEntity(supervisor, "sensor.snapshot_backup", "backed_up", {
        'friendly_name': 'Snapshot State',
        'last_snapshot': date,
        'snapshots': [{
            'date': date,
            'name': backup.name(),
            'size': backup.sizeString(),
            'state': backup.status(),
            'slug': backup.slug()
        }
        ],
        'snapshots_in_google_drive': 1,
        'snapshots_in_home_assistant': 1,
        'snapshots_in_hassio': 1,
        'size_in_home_assistant': Estimator.asSizeString(backup.size()),
        'size_in_google_drive': Estimator.asSizeString(backup.size())
    })
async def test_update_snapshots_sync(updater: HaUpdater, server,
                                     time: FakeTime, snapshot,
                                     supervisor: SimulatedSupervisor):
    await updater.update()
    assert not updater._stale()
    assert updater._state() == "backed_up"
    verifyEntity(supervisor, "binary_sensor.snapshots_stale", "off",
                 STALE_ATTRIBUTES)
    date = '1985-12-06T05:00:00+00:00'
    verifyEntity(
        supervisor, "sensor.snapshot_backup", "backed_up", {
            'friendly_name':
            'Snapshot State',
            'last_snapshot':
            date,
            'snapshots': [{
                'date': date,
                'name': snapshot.name(),
                'size': snapshot.sizeString(),
                'state': snapshot.status()
            }],
            'snapshots_in_google_drive':
            1,
            'snapshots_in_hassio':
            1,
            'snapshots_in_home_assistant':
            1,
            'size_in_home_assistant':
            Estimator.asSizeString(snapshot.size()),
            'size_in_google_drive':
            Estimator.asSizeString(snapshot.size())
        })
예제 #3
0
 def buildBackupMetrics(self):
     info = {}
     for source in self._sources:
         source_class = self._sources[source]
         source_info = {
             'backups': 0,
             'retained': 0,
             'deletable': 0,
             'name': source,
             'title': source_class.title(),
             'latest': None,
             'max': source_class.maxCount(),
             'enabled': source_class.enabled(),
             'icon': source_class.icon(),
             'ignored': 0,
         }
         size = 0
         ignored_size = 0
         latest = None
         for backup in self.backups():
             data: AbstractBackup = backup.getSource(source)
             if data is None:
                 continue
             if data.ignore() and backup.ignore():
                 source_info['ignored'] += 1
             if backup.ignore():
                 ignored_size += backup.size()
                 continue
             source_info['backups'] += 1
             if data.retained():
                 source_info['retained'] += 1
             else:
                 source_info['deletable'] += 1
             if latest is None or data.date() > latest:
                 latest = data.date()
             size += int(data.sizeInt())
         if latest is not None:
             source_info['latest'] = self._time.asRfc3339String(latest)
         source_info['size'] = Estimator.asSizeString(size)
         source_info['ignored_size'] = Estimator.asSizeString(ignored_size)
         free_space = source_class.freeSpace()
         if free_space is not None:
             source_info['free_space'] = Estimator.asSizeString(free_space)
         info[source] = source_info
     return info
예제 #4
0
async def test_update_backups_sync(updater: HaUpdater, server, time: FakeTime, backup: Backup, supervisor: SimulatedSupervisor):
    await updater.update()
    assert not updater._stale()
    assert updater._state() == "backed_up"
    verifyEntity(supervisor, "binary_sensor.backups_stale",
                 "off", STALE_ATTRIBUTES)
    date = '1985-12-06T05:00:00+00:00'
    verifyEntity(supervisor, "sensor.backup_state", "backed_up", {
        'friendly_name': 'Backup State',
        'last_backup': date,
        'last_uploaded': date,
        'backups': [{
            'date': date,
            'name': backup.name(),
            'size': backup.sizeString(),
            'state': backup.status(),
            'slug': backup.slug()
        }
        ],
        'backups_in_google_drive': 1,
        'backups_in_home_assistant': 1,
        'size_in_home_assistant': Estimator.asSizeString(backup.size()),
        'size_in_google_drive': Estimator.asSizeString(backup.size())
    })
    async def getstatus(self, request) -> Dict[Any, Any]:
        status: Dict[Any, Any] = {}
        status['folder_id'] = self.folder_finder.getCachedFolder()
        status['snapshots'] = []
        snapshots = self._coord.snapshots()
        for snapshot in snapshots:
            status['snapshots'].append(self.getSnapshotDetails(snapshot))
        status['restore_link'] = self._ha_source.getFullRestoreLink()
        status['drive_enabled'] = self._coord.enabled()
        status['ask_error_reports'] = not self.config.isExplicit(
            Setting.SEND_ERROR_REPORTS)
        status['warn_ingress_upgrade'] = False
        status['cred_version'] = self._global_info.credVersion
        status['free_space'] = Estimator.asSizeString(
            self._estimator.getBytesFree())
        next = self._coord.nextSnapshotTime()
        if next is None:
            status['next_snapshot_text'] = "Disabled"
            status['next_snapshot_machine'] = ""
            status['next_snapshot_detail'] = "Disabled"
        elif (next < self._time.now()):
            status['next_snapshot_text'] = self._time.formatDelta(
                self._time.now())
            status['next_snapshot_machine'] = self._time.asRfc3339String(
                self._time.now())
            status['next_snapshot_detail'] = self._time.toLocal(
                self._time.now()).strftime("%c")
        else:
            status['next_snapshot_text'] = self._time.formatDelta(next)
            status['next_snapshot_machine'] = self._time.asRfc3339String(next)
            status['next_snapshot_detail'] = self._time.toLocal(next).strftime(
                "%c")

        if len(snapshots) > 0:
            latest = snapshots[len(snapshots) - 1].date()
            status['last_snapshot_text'] = self._time.formatDelta(latest)
            status['last_snapshot_machine'] = self._time.asRfc3339String(
                latest)
            status['last_snapshot_detail'] = self._time.toLocal(
                latest).strftime("%c")
        else:
            status['last_snapshot_text'] = "Never"
            status['last_snapshot_machine'] = ""
            status['last_snapshot_detail'] = "Never"

        status['last_error'] = None
        if self._global_info._last_error is not None and self._global_info.isErrorSuppressed(
        ):
            status['last_error'] = self.processError(
                self._global_info._last_error)
        status["last_error_count"] = self._global_info.failureCount()
        status["ignore_errors_for_now"] = self._global_info.ignoreErrorsForNow(
        )
        status["syncing"] = self._coord.isSyncing()
        status["ignore_sync_error"] = self._coord.isWorkingThroughUpload()
        status["firstSync"] = self._global_info._first_sync
        status["maxSnapshotsInHasssio"] = self.config.get(
            Setting.MAX_SNAPSHOTS_IN_HASSIO)
        status["maxSnapshotsInDrive"] = self.config.get(
            Setting.MAX_SNAPSHOTS_IN_GOOGLE_DRIVE)
        status["snapshot_name_template"] = self.config.get(
            Setting.SNAPSHOT_NAME)
        status['sources'] = self._coord.buildSnapshotMetrics()
        status['authenticate_url'] = self.config.get(Setting.AUTHENTICATE_URL)
        status['choose_folder_url'] = self.config.get(
            Setting.CHOOSE_FOLDER_URL) + "?bg={0}&ac={1}".format(
                quote(self.config.get(Setting.BACKGROUND_COLOR)),
                quote(self.config.get(Setting.ACCENT_COLOR)))
        status['dns_info'] = self._global_info.getDnsInfo()
        status['enable_drive_upload'] = self.config.get(
            Setting.ENABLE_DRIVE_UPLOAD)
        status['is_custom_creds'] = self._coord._model.dest.isCustomCreds()
        status['is_specify_folder'] = self.config.get(
            Setting.SPECIFY_SNAPSHOT_FOLDER)
        return web.json_response(status)
예제 #6
0
async def test_check_space(estimator: Estimator, coord, config: Config):
    estimator.refresh()
    estimator.checkSpace(coord.backups())

    config.override(Setting.LOW_SPACE_THRESHOLD, estimator.getBytesFree() + 1)
    estimator.checkSpace(coord.backups())