Use an assumed switch state until refresh callback is complete (#59805)

This commit is contained in:
Nathan Spencer 2021-11-18 22:29:38 -07:00 committed by GitHub
parent ff21453f58
commit 406cbcfe2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 25 deletions

View File

@ -67,19 +67,21 @@ class LitterRobotControlEntity(LitterRobotEntity):
self, action: MethodType, *args: Any, **kwargs: Any self, action: MethodType, *args: Any, **kwargs: Any
) -> bool: ) -> bool:
"""Perform an action and initiates a refresh of the robot data after a few seconds.""" """Perform an action and initiates a refresh of the robot data after a few seconds."""
success = False
try: try:
await action(*args, **kwargs) success = await action(*args, **kwargs)
except InvalidCommandException as ex: # pragma: no cover except InvalidCommandException as ex: # pragma: no cover
# this exception should only occur if the underlying API for commands changes # this exception should only occur if the underlying API for commands changes
_LOGGER.error(ex) _LOGGER.error(ex)
return False success = False
if success:
self.async_cancel_refresh_callback() self.async_cancel_refresh_callback()
self._refresh_callback = async_call_later( self._refresh_callback = async_call_later(
self.hass, REFRESH_WAIT_TIME_SECONDS, self.async_call_later_callback self.hass, REFRESH_WAIT_TIME_SECONDS, self.async_call_later_callback
) )
return True return success
async def async_call_later_callback(self, *_) -> None: async def async_call_later_callback(self, *_) -> None:
"""Perform refresh request on callback.""" """Perform refresh request on callback."""
@ -118,3 +120,16 @@ class LitterRobotConfigEntity(LitterRobotControlEntity):
"""A Litter-Robot entity that can control configuration of the unit.""" """A Litter-Robot entity that can control configuration of the unit."""
_attr_entity_category = ENTITY_CATEGORY_CONFIG _attr_entity_category = ENTITY_CATEGORY_CONFIG
def __init__(self, robot: Robot, entity_type: str, hub: LitterRobotHub) -> None:
"""Init a Litter-Robot control entity."""
super().__init__(robot=robot, entity_type=entity_type, hub=hub)
self._assumed_state: Any = None
async def perform_action_and_assume_state(
self, action: MethodType, assumed_state: Any
) -> bool:
"""Perform an action and assume the state passed in if call is successful."""
if await self.perform_action_and_refresh(action, assumed_state):
self._assumed_state = assumed_state
self.async_write_ha_state()

View File

@ -13,7 +13,7 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
UPDATE_INTERVAL_SECONDS = 10 UPDATE_INTERVAL_SECONDS = 20
class LitterRobotHub: class LitterRobotHub:

View File

@ -3,7 +3,11 @@
"name": "Litter-Robot", "name": "Litter-Robot",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/litterrobot", "documentation": "https://www.home-assistant.io/integrations/litterrobot",
"requirements": ["pylitterbot==2021.10.1"], "requirements": [
"codeowners": ["@natekspencer"], "pylitterbot==2021.11.0"
],
"codeowners": [
"@natekspencer"
],
"iot_class": "cloud_polling" "iot_class": "cloud_polling"
} }

View File

@ -19,6 +19,8 @@ class LitterRobotNightLightModeSwitch(LitterRobotConfigEntity, SwitchEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if switch is on.""" """Return true if switch is on."""
if self._refresh_callback is not None:
return self._assumed_state
return self.robot.night_light_mode_enabled return self.robot.night_light_mode_enabled
@property @property
@ -28,11 +30,11 @@ class LitterRobotNightLightModeSwitch(LitterRobotConfigEntity, SwitchEntity):
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on.""" """Turn the switch on."""
await self.perform_action_and_refresh(self.robot.set_night_light, True) await self.perform_action_and_assume_state(self.robot.set_night_light, True)
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off.""" """Turn the switch off."""
await self.perform_action_and_refresh(self.robot.set_night_light, False) await self.perform_action_and_assume_state(self.robot.set_night_light, False)
class LitterRobotPanelLockoutSwitch(LitterRobotConfigEntity, SwitchEntity): class LitterRobotPanelLockoutSwitch(LitterRobotConfigEntity, SwitchEntity):
@ -41,6 +43,8 @@ class LitterRobotPanelLockoutSwitch(LitterRobotConfigEntity, SwitchEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if switch is on.""" """Return true if switch is on."""
if self._refresh_callback is not None:
return self._assumed_state
return self.robot.panel_lock_enabled return self.robot.panel_lock_enabled
@property @property
@ -50,11 +54,11 @@ class LitterRobotPanelLockoutSwitch(LitterRobotConfigEntity, SwitchEntity):
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on.""" """Turn the switch on."""
await self.perform_action_and_refresh(self.robot.set_panel_lockout, True) await self.perform_action_and_assume_state(self.robot.set_panel_lockout, True)
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off.""" """Turn the switch off."""
await self.perform_action_and_refresh(self.robot.set_panel_lockout, False) await self.perform_action_and_assume_state(self.robot.set_panel_lockout, False)
ROBOT_SWITCHES: list[tuple[type[LitterRobotConfigEntity], str]] = [ ROBOT_SWITCHES: list[tuple[type[LitterRobotConfigEntity], str]] = [

View File

@ -1604,7 +1604,7 @@ pylibrespot-java==0.1.0
pylitejet==0.3.0 pylitejet==0.3.0
# homeassistant.components.litterrobot # homeassistant.components.litterrobot
pylitterbot==2021.10.1 pylitterbot==2021.11.0
# homeassistant.components.loopenergy # homeassistant.components.loopenergy
pyloopenergy==0.2.1 pyloopenergy==0.2.1

View File

@ -971,7 +971,7 @@ pylibrespot-java==0.1.0
pylitejet==0.3.0 pylitejet==0.3.0
# homeassistant.components.litterrobot # homeassistant.components.litterrobot
pylitterbot==2021.10.1 pylitterbot==2021.11.0
# homeassistant.components.lutron_caseta # homeassistant.components.lutron_caseta
pylutron-caseta==0.11.0 pylutron-caseta==0.11.0

View File

@ -1,5 +1,6 @@
"""Test the Litter-Robot switch entity.""" """Test the Litter-Robot switch entity."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import MagicMock
import pytest import pytest
@ -9,7 +10,13 @@ from homeassistant.components.switch import (
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
) )
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_CATEGORY_CONFIG, STATE_ON from homeassistant.const import (
ATTR_ENTITY_ID,
ENTITY_CATEGORY_CONFIG,
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry from homeassistant.helpers import entity_registry
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
@ -21,13 +28,13 @@ NIGHT_LIGHT_MODE_ENTITY_ID = "switch.test_night_light_mode"
PANEL_LOCKOUT_ENTITY_ID = "switch.test_panel_lockout" PANEL_LOCKOUT_ENTITY_ID = "switch.test_panel_lockout"
async def test_switch(hass, mock_account): async def test_switch(hass: HomeAssistant, mock_account: MagicMock):
"""Tests the switch entity was set up.""" """Tests the switch entity was set up."""
await setup_integration(hass, mock_account, PLATFORM_DOMAIN) await setup_integration(hass, mock_account, PLATFORM_DOMAIN)
switch = hass.states.get(NIGHT_LIGHT_MODE_ENTITY_ID) state = hass.states.get(NIGHT_LIGHT_MODE_ENTITY_ID)
assert switch assert state
assert switch.state == STATE_ON assert state.state == STATE_ON
ent_reg = entity_registry.async_get(hass) ent_reg = entity_registry.async_get(hass)
entity_entry = ent_reg.async_get(NIGHT_LIGHT_MODE_ENTITY_ID) entity_entry = ent_reg.async_get(NIGHT_LIGHT_MODE_ENTITY_ID)
@ -42,12 +49,14 @@ async def test_switch(hass, mock_account):
(PANEL_LOCKOUT_ENTITY_ID, "set_panel_lockout"), (PANEL_LOCKOUT_ENTITY_ID, "set_panel_lockout"),
], ],
) )
async def test_on_off_commands(hass, mock_account, entity_id, robot_command): async def test_on_off_commands(
hass: HomeAssistant, mock_account: MagicMock, entity_id: str, robot_command: str
):
"""Test sending commands to the switch.""" """Test sending commands to the switch."""
await setup_integration(hass, mock_account, PLATFORM_DOMAIN) await setup_integration(hass, mock_account, PLATFORM_DOMAIN)
switch = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert switch assert state
data = {ATTR_ENTITY_ID: entity_id} data = {ATTR_ENTITY_ID: entity_id}
@ -65,3 +74,6 @@ async def test_on_off_commands(hass, mock_account, entity_id, robot_command):
future = utcnow() + timedelta(seconds=REFRESH_WAIT_TIME_SECONDS) future = utcnow() + timedelta(seconds=REFRESH_WAIT_TIME_SECONDS)
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
assert getattr(mock_account.robots[0], robot_command).call_count == count assert getattr(mock_account.robots[0], robot_command).call_count == count
state = hass.states.get(entity_id)
assert state
assert state.state == STATE_ON if service == SERVICE_TURN_ON else STATE_OFF