Restart modem: pause Smart Power auto power-cycle until the modem is back
Lint / JS (eslint) (push) Successful in 14s
Lint / Python (flake8) (push) Successful in 1m5s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m13s
Lint / Python (flake8) (pull_request) Successful in 1m14s
Test / Python Tests (pytest) (push) Successful in 1m51s
Security / Python Security (bandit) (pull_request) Successful in 1m25s
Lint / JS (eslint) (pull_request) Successful in 19s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Test / Python Tests (pytest) (pull_request) Successful in 1m32s

UniFi Smart Power on the RPDU's modem outlet saw the internet down while
the modem rebooted and cut its power mid-boot: once during the 11-20
restart and twice after the 11-24 AT&T-app reboot. UniFi only exposes
auto power-cycle as on or off, so the button now turns it off for every
outlet that has it on, and the monitor turns it back on when the restart
ends (back, or after 30 minutes).

- UnifiClient.auto_power_cycle_outlets / set_auto_power_cycle: sends the
  device's outlet_overrides back with only cycle_enabled changed, then
  reads them again to confirm. The only UniFi setting Gandalf changes.
- A failed pause doesn't stop the restart; a failed restart turns it back
  on. A failed restore is retried every cycle and shown as broken in
  Needs attention until it works.
- The panel note and confirm text mention the pause.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-24 00:13:46 -05:00
1 parent a7792063ba
commit a5d4fc3156
9 files changed
+304 -19

No files matched your search

+34 -4
View File
@@ -879,17 +879,26 @@ class TestCalmDashboard:
class TestModemRestart:
CFG = dict(CONFIG, modem={'url': 'http://192.0.2.254', 'access_code': 'secret'},
uptime_kuma={'url': 'http://kuma.test', 'username': 'u', 'password': 'p', 'maintenance_id': 7})
uptime_kuma={'url': 'http://kuma.test', 'username': 'u', 'password': 'p', 'maintenance_id': 7},
unifi={'controller': 'https://udm.test', 'api_key': 'k'})
def _post(self, client, body=None, headers=AUTH, cfg=None, kuma_error=None, restart=None):
OUTLET = {'device_id': 'pdu1', 'device': 'RPDU', 'index': 13, 'name': 'AT&T Modem'}
def _post(self, client, body=None, headers=AUTH, cfg=None, kuma_error=None, restart=None,
paused=None, restore_error=None):
admin = MagicMock()
if isinstance(restart, Exception):
admin.restart.side_effect = restart
else:
admin.restart.return_value = restart or 'accepted'
self.pause = MagicMock(return_value=paused or ([self.OUTLET], None))
self.restore = MagicMock(return_value=restore_error)
with patch.object(gandalf, '_cfg', cfg or self.CFG), \
patch.object(gandalf.kuma, 'set_maintenance', side_effect=kuma_error) as km, \
patch.object(gandalf.modem_api, 'ModemAdmin', return_value=admin):
patch.object(gandalf.modem_api, 'ModemAdmin', return_value=admin), \
patch.object(gandalf, 'UnifiClient', MagicMock()), \
patch.object(gandalf, 'pause_smart_power', self.pause), \
patch.object(gandalf, 'restore_smart_power', self.restore):
gandalf.db.create_suppression.return_value = 42
resp = client.post('/api/modem/restart', json=body if body is not None else {'confirm': True},
headers=headers)
@@ -910,13 +919,34 @@ class TestModemRestart:
def test_silences_then_restarts(self, client):
resp, km, admin = self._post(client)
assert resp.status_code == 200
assert resp.get_json() == {'ok': True, 'result': 'accepted', 'kuma': True, 'suppression_id': 42}
assert resp.get_json() == {'ok': True, 'result': 'accepted', 'kuma': True, 'suppression_id': 42,
'smart_power_paused': 1, 'smart_power_error': None}
km.assert_called_once_with(self.CFG['uptime_kuma'], True)
args = gandalf.db.create_suppression.call_args.args
assert args[:3] == ('all', '', '') and args[4] == 'jared' and args[5] == gandalf.MODEM_RESTART_SILENCE_MIN
admin.restart.assert_called_once_with()
key, state = gandalf.db.set_state.call_args.args
assert key == 'modem_restart' and state['suppression_id'] == 42 and state['kuma'] is True
assert state['smart_power_paused'] == [self.OUTLET]
assert self.pause.called and not self.restore.called
def test_smart_power_failure_does_not_stop_the_restart(self, client):
resp, _, admin = self._post(client, paused=([], 'HTTP 401'))
assert resp.status_code == 200 and admin.restart.called
assert resp.get_json()['smart_power_error'] == 'HTTP 401'
def test_failed_restart_turns_smart_power_back_on(self, client):
resp, _, _ = self._post(client, restart=gandalf.modem_api.ModemUnreachable('timeout'))
assert resp.status_code == 502
assert self.restore.call_args.args[1] == [self.OUTLET]
def test_failed_restart_and_failed_restore_is_remembered(self, client):
resp, _, _ = self._post(client, restart=gandalf.modem_api.ModemUnreachable('timeout'),
restore_error='HTTP 500')
assert resp.status_code == 502
key, state = gandalf.db.set_state.call_args.args
assert key == 'modem_restart' and state['finished_at'] and state['smart_power_error'] == 'HTTP 500'
assert state['smart_power_paused'] == [self.OUTLET] and not state.get('smart_power_restored')
def test_kuma_failure_stops_unless_accepted(self, client):
resp, _, admin = self._post(client, kuma_error=gandalf.kuma.KumaError('login refused'))