Handle tplink credential change at run time (#108692)

This commit is contained in:
J. Nick Koston 2024-01-22 13:30:21 -10:00 committed by GitHub
parent 12b41c35ec
commit f6bc5c98b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 1 deletions

View file

@ -4,9 +4,10 @@ from __future__ import annotations
from datetime import timedelta
import logging
from kasa import SmartDevice, SmartDeviceException
from kasa import AuthenticationException, SmartDevice, SmartDeviceException
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@ -42,5 +43,7 @@ class TPLinkDataUpdateCoordinator(DataUpdateCoordinator[None]):
"""Fetch all device and sensor data from api."""
try:
await self.device.update(update_children=False)
except AuthenticationException as ex:
raise ConfigEntryAuthFailed from ex
except SmartDeviceException as ex:
raise UpdateFailed(ex) from ex

View file

@ -5,6 +5,7 @@ import copy
from datetime import timedelta
from unittest.mock import AsyncMock, MagicMock, patch
from kasa.exceptions import AuthenticationException
import pytest
from homeassistant import setup
@ -17,6 +18,8 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_USERNAME,
EVENT_HOMEASSISTANT_STARTED,
STATE_ON,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import EntityRegistry
@ -29,6 +32,7 @@ from . import (
IP_ADDRESS,
MAC_ADDRESS,
_mocked_dimmer,
_mocked_plug,
_patch_connect,
_patch_discovery,
_patch_single_discovery,
@ -256,3 +260,32 @@ async def test_config_entry_errors(
any(mock_config_entry.async_get_active_flows(hass, {SOURCE_REAUTH}))
== reauth_flows
)
async def test_plug_auth_fails(hass: HomeAssistant) -> None:
"""Test a smart plug auth failure."""
config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=MAC_ADDRESS)
config_entry.add_to_hass(hass)
plug = _mocked_plug()
with _patch_discovery(device=plug), _patch_connect(device=plug):
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done()
entity_id = "switch.my_plug"
state = hass.states.get(entity_id)
assert state.state == STATE_ON
plug.update = AsyncMock(side_effect=AuthenticationException)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30))
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_UNAVAILABLE
assert (
len(
hass.config_entries.flow.async_progress_by_handler(
DOMAIN, match_context={"source": SOURCE_REAUTH}
)
)
== 1
)