Skip to content

Commit

Permalink
Optimistic data updates after API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
signalkraft committed Feb 26, 2024
1 parent 129b498 commit a460de4
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions custom_components/mypyllant/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,12 @@ async def set_holiday(self, **kwargs):
await self.coordinator.api.set_holiday(
self.system, start, end, setpoint=setpoint
)
await self.coordinator.async_request_refresh_delayed()
await self.coordinator.async_request_refresh_delayed(20)

async def cancel_holiday(self):
_LOGGER.debug("Canceling holiday on System %s", self.system.id)
await self.coordinator.api.cancel_holiday(self.system)
await self.coordinator.async_request_refresh_delayed()
await self.coordinator.async_request_refresh_delayed(20)

async def set_zone_time_program(self, **kwargs):
_LOGGER.debug("Canceling holiday on System %s", self.system.id)
Expand Down
5 changes: 5 additions & 0 deletions custom_components/mypyllant/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ async def async_request_refresh_delayed(self, delay=None):
The API takes a long time to return updated values (i.e. after setting a new heating mode)
This function waits for a few second and then refreshes
"""

# API calls sometimes update the models, so we update the data before waiting for the refresh
# to see immediate changes in the UI
self.async_set_updated_data(self.data)
if not delay:
delay = self.entry.options.get(OPTION_REFRESH_DELAY, DEFAULT_REFRESH_DELAY)
if delay:
_LOGGER.debug("Waiting %ss before refreshing data", delay)
await asyncio.sleep(delay)
await self.async_request_refresh()

Expand Down
2 changes: 1 addition & 1 deletion custom_components/mypyllant/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/signalkraft/mypyllant-component/issues",
"requirements": [
"myPyllant==0.7.24"
"myPyllant==0.7.25"
],
"version": "v0.7.3"
}
6 changes: 3 additions & 3 deletions custom_components/mypyllant/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def name(self):
def native_value(self):
if self.holiday_remaining:
if self.native_unit_of_measurement == UnitOfTime.DAYS:
return round(self.holiday_remaining.total_seconds() / 3600 / 24)
return round(self.holiday_remaining.days)
else:
return round(self.holiday_remaining.total_seconds() / 3600)
else:
Expand All @@ -97,14 +97,14 @@ async def async_set_native_value(self, value: float) -> None:
if value == 0:
await self.coordinator.api.cancel_holiday(self.system)
# Holiday values need a long time to show up in the API
await self.coordinator.async_request_refresh_delayed(10)
await self.coordinator.async_request_refresh_delayed(20)
else:
if self.native_unit_of_measurement == UnitOfTime.DAYS:
value = value * 24
end = datetime.now(self.system.timezone) + timedelta(hours=value)
await self.coordinator.api.set_holiday(self.system, end=end)
# Holiday values need a long time to show up in the API
await self.coordinator.async_request_refresh_delayed(10)
await self.coordinator.async_request_refresh_delayed(20)

@property
def unique_id(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions custom_components/mypyllant/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ async def async_turn_on(self, **kwargs):
)
await self.coordinator.api.set_holiday(self.system, end=end)
# Holiday values need a long time to show up in the API
await self.coordinator.async_request_refresh_delayed(10)
await self.coordinator.async_request_refresh_delayed(20)

async def async_turn_off(self, **kwargs):
await self.coordinator.api.cancel_holiday(self.system)
# Holiday values need a long time to show up in the API
await self.coordinator.async_request_refresh_delayed(10)
await self.coordinator.async_request_refresh_delayed(20)

@property
def unique_id(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions custom_components/mypyllant/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ async def async_set_temperature(self, **kwargs: Any) -> None:
await self.coordinator.api.set_domestic_hot_water_temperature(
self.domestic_hot_water, int(target_temp)
)
await self.coordinator.async_request_refresh_delayed()

async def async_set_operation_mode(
self, operation_mode: str, **kwargs: Any
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ types-PyYAML~=6.0.12.12

# Need specific versions
pytest-homeassistant-custom-component==0.13.101
myPyllant==0.7.24
myPyllant==0.7.25

# Versions handled by pytest-homeassistant-custom-component
freezegun
Expand Down
Binary file modified docs/docs/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ async def __call__(self, *args, **kwargs):
return super().__call__(*args, **kwargs)


@pytest.fixture(scope="session", autouse=True)
def asyncio_sleep_mock():
"""
Disable sleep in asyncio to avoid delays in test execution
"""
with mock.patch(
"asyncio.sleep",
new_callable=AsyncMock,
) as _fixture:
yield _fixture


@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
"""Enable custom integrations in all tests."""
Expand Down
1 change: 1 addition & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async def call_service(
data=test_user_input,
options=TEST_OPTIONS,
)
hass.data["integrations"] = {DOMAIN: mock.MagicMock()}
config_entry.add_to_hass(hass)
await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()
Expand Down

0 comments on commit a460de4

Please sign in to comment.