Detect offline hub children and fail fast on writes to them (#1830)
Some checks failed
CI / Perform Lint Checks (3.15) (push) Has been cancelled
CodeQL Checks / Analyze (python) (push) Has been cancelled
CI / Python 3.11 on macos-latest (push) Has been cancelled
CI / Python 3.12 on macos-latest (push) Has been cancelled
CI / Python 3.13 on macos-latest (push) Has been cancelled
CI / Python 3.14 on macos-latest (push) Has been cancelled
CI / Python 3.15 on macos-latest (push) Has been cancelled
CI / Python 3.11 on ubuntu-latest (push) Has been cancelled
CI / Python 3.12 on ubuntu-latest (push) Has been cancelled
CI / Python 3.13 on ubuntu-latest (push) Has been cancelled
CI / Python 3.14 on ubuntu-latest (push) Has been cancelled
CI / Python 3.15 on ubuntu-latest (push) Has been cancelled
CI / Python 3.11 on windows-latest (push) Has been cancelled
CI / Python 3.12 on windows-latest (push) Has been cancelled
CI / Python 3.13 on windows-latest (push) Has been cancelled
CI / Python 3.14 on windows-latest (push) Has been cancelled
CI / Python 3.15 on windows-latest (push) Has been cancelled
Stale / stale (push) Has been cancelled

Hubs keep reporting for children they have lost contact with, so callers
see stale values as if they were live. Trying to write to such devices
fail after timeouts with a generic error.
This PR exposes the offline/online information as reported by the hub,
and refuses writes to unreachable child devices without waiting for
device errors.

In details:
- Add new API: `Device.is_available`; hub children report unavailable
when the hub marks them offline or no longer lists them.
- New feature `available` for child devices to make it visible to the
CLI.
- Refuse writes to unavailable children with the new
`DeviceUnavailableError` instead of waiting for the hub to time out.
This commit is contained in:
Teemu R.
2026-11-01 18:18:03 +01:00
committed by GitHub
parent 81243ed87f
commit a68f2b3304
13 changed files with 229 additions and 16 deletions

View File

@@ -14,11 +14,17 @@ from freezegun.api import FrozenDateTimeFactory
from kasa import Credentials, Device, DeviceType, Module
from kasa.credentials import DEFAULT_CREDENTIALS, get_default_credentials
from kasa.exceptions import AuthenticationError, DeviceError, KasaException
from kasa.smartcam import SmartCamDevice
from kasa.exceptions import (
AuthenticationError,
DeviceError,
DeviceUnavailableError,
KasaException,
)
from kasa.smartcam import SmartCamChild, SmartCamDevice
from kasa.smartcam.modules.time import Time
from ..conftest import device_smartcam, hub_smartcam
from ..fakeprotocol_smartcam import FakeSmartCamTransport
class _ChangeAdminPasswordPayload(TypedDict):
@@ -96,6 +102,28 @@ async def test_hub(dev: Device) -> None:
assert child.device_id
@hub_smartcam
@pytest.mark.requires_dummy
@pytest.mark.parametrize("online", [True, False])
async def test_camera_child_is_available(dev: Device, online: bool) -> None:
"""Test a camera child reports the reachability the hub reports for it."""
child = next((c for c in dev.children if isinstance(c, SmartCamChild)), None)
if child is None:
pytest.skip(f"Device {dev} fixture does not have camera children")
assert isinstance(dev.protocol._transport, FakeSmartCamTransport)
for info in dev.protocol._transport.info["getChildDeviceList"]["child_device_list"]:
if info["device_id"] == child.device_id:
info["online"] = online
await dev.update()
assert child.is_available is online
assert child.features["available"].value is online
if not online:
with pytest.raises(DeviceUnavailableError):
await child.set_alias("new")
@device_smartcam
async def test_wifi_scan(dev: SmartCamDevice) -> None:
fake_scan_data = {