Skip to content

Commit

Permalink
fw upgrade status fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
modrisb committed Jun 27, 2024
1 parent 164bd55 commit acbc0a3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
32 changes: 20 additions & 12 deletions custom_components/pijups/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import subprocess
import time
import threading
from typing import Any

import voluptuous as vol
Expand Down Expand Up @@ -138,6 +139,7 @@ def __init__(self, config_entry):
self.fw_page_count = None
self.fw_processed_pages = None
self.fw_progress_action = None
self.fw_progress = threading.Event()

self.fw_update_time = None

Expand Down Expand Up @@ -293,11 +295,15 @@ async def async_step_firmware_confirm(
self, user_input: dict[str, Any] = None
) -> FlowResult:
"""Validate the user input allows us to connect."""
_LOGGER.debug("async_step_firmware_confirm user_input=%s", user_input)
_LOGGER.warning("async_step_firmware_confirm user_input=%s", user_input)
errors = {}
if user_input is not None:
self.fw_task = None
self.fw_progress.clear()
self.fw_task = self.hass.async_create_task(self.async_background_status())
await asyncio.sleep(0.2)
self.fw_progress.wait()
self.fw_progress.clear()
return await self.async_step_firmware_progress()
return self.async_show_form(
step_id="firmware_confirm",
Expand All @@ -309,22 +315,27 @@ async def async_step_firmware_progress(
self, user_input: dict[str, Any] = None
) -> FlowResult:
"""Validate the user input allows us to connect."""
_LOGGER.debug(
_LOGGER.warning(
"async_step_firmware_progress user_input=%s, pages %s, done %s",
user_input,
self.fw_page_count,
self.fw_processed_pages,
)
try:
await self.fw_task
finally:
self.fw_task = None
if not self.fw_task.done():
self.fw_progress.wait()
self.fw_progress.clear()
return self.async_show_progress(
step_id="firmware_progress",
progress_action=self.fw_progress_action,
progress_task=self.fw_task,
)

ret_data = self.async_show_progress_done(next_step_id="firmware_finish")
return ret_data

async def async_background_status(self):
"""FW upgrade utlity execution monitor."""
self.fw_progress.set()
_LOGGER.debug("async_background_status started")
self.pijups.piju_enabled = False # disable requests to device
fw_path_info = await self.hass.async_add_executor_job(
Expand Down Expand Up @@ -364,15 +375,12 @@ async def async_background_status(self):
else:
progress_action = "fw_started"
if self.fw_progress_action != progress_action:
self.async_show_progress(
step_id="firmware_progress",
progress_action=progress_action,
progress_task=self.fw_task,
)
self.fw_progress_action = progress_action
self.fw_progress.set()
finally:
self.pijups.piju_enabled = True # enable requests to device
self.fw_progress.set()
pass
self.pijups.piju_enabled = True # enable requests to device

async def async_step_firmware_finish(
self, user_input: dict[str, Any] = None
Expand Down
2 changes: 1 addition & 1 deletion custom_components/pijups/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"documentation": "https://github.com/modrisb/pijups/",
"issue_tracker": "https://github.com/modrisb/pijups/issues",
"requirements": ["smbus2==0.4.1"],
"version": "1.2.6",
"version": "1.2.7",
"dependencies": [],
"codeowners": ["@modrisb"],
"config_flow": true,
Expand Down
15 changes: 10 additions & 5 deletions tests/components/pijups/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,17 @@ async def run_test_entry_options_with_firmware_upgrade(hass, entry):
assert fw_upgrade_confirmation is not None
assert fw_upgrade_confirmation["step_id"] == "firmware_confirm"
assert fw_upgrade_confirmation["type"] == FlowResultType.FORM
fw_upgrade_finished = await hass.config_entries.options.async_configure(
options_flow_result["flow_id"], user_input={}
)
while True:
fw_upgrade_progress = await hass.config_entries.options.async_configure(
options_flow_result["flow_id"], user_input={}
)
assert fw_upgrade_progress is not None
if fw_upgrade_progress["type"] == FlowResultType.SHOW_PROGRESS:
assert not pijups.piju_enabled
else:
assert fw_upgrade_progress["type"] == FlowResultType.CREATE_ENTRY #SHOW_PROGRESS_DONE
break
assert pijups.piju_enabled
assert fw_upgrade_finished is not None
assert fw_upgrade_finished["type"] == FlowResultType.CREATE_ENTRY

await common.pijups_setup_and_run_test(
hass, True, run_test_entry_options_with_firmware_upgrade
Expand Down

0 comments on commit acbc0a3

Please sign in to comment.