Skip to content

Commit

Permalink
Fix KNX Keyfile upload (home-assistant#89029)
Browse files Browse the repository at this point in the history
* Fix KNX Keyfile upload

* use shutil.move instead
  • Loading branch information
farmio authored and balloob committed Mar 2, 2023
1 parent 3e961d3 commit f7eaeb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 8 additions & 3 deletions homeassistant/components/knx/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator
from pathlib import Path
import shutil
from typing import Any, Final

import voluptuous as vol
Expand Down Expand Up @@ -549,9 +550,12 @@ async def async_step_knxkeys_tunnel_select(
),
None,
)
_tunnel_identifier = selected_tunnel_ia or self.new_entry_data.get(
CONF_HOST
)
_tunnel_suffix = f" @ {_tunnel_identifier}" if _tunnel_identifier else ""
self.new_title = (
f"{'Secure ' if _if_user_id else ''}"
f"Tunneling @ {selected_tunnel_ia or self.new_entry_data[CONF_HOST]}"
f"{'Secure ' if _if_user_id else ''}Tunneling{_tunnel_suffix}"
)
return self.finish_flow()

Expand Down Expand Up @@ -708,7 +712,8 @@ def _process_upload() -> tuple[Keyring | None, dict[str, str]]:
else:
dest_path = Path(self.hass.config.path(STORAGE_DIR, DOMAIN))
dest_path.mkdir(exist_ok=True)
file_path.rename(dest_path / DEFAULT_KNX_KEYRING_FILENAME)
dest_file = dest_path / DEFAULT_KNX_KEYRING_FILENAME
shutil.move(file_path, dest_file)
return keyring, errors

keyring, errors = await self.hass.async_add_executor_job(_process_upload)
Expand Down
11 changes: 6 additions & 5 deletions tests/components/knx/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ def patch_file_upload(return_value=FIXTURE_KEYRING, side_effect=None):
side_effect=side_effect,
), patch(
"pathlib.Path.mkdir"
) as mkdir_mock:
file_path_mock = Mock()
file_upload_mock.return_value.__enter__.return_value = file_path_mock
) as mkdir_mock, patch(
"shutil.move"
) as shutil_move_mock:
file_upload_mock.return_value.__enter__.return_value = Mock()
yield return_value
if side_effect:
mkdir_mock.assert_not_called()
file_path_mock.rename.assert_not_called()
shutil_move_mock.assert_not_called()
else:
mkdir_mock.assert_called_once()
file_path_mock.rename.assert_called_once()
shutil_move_mock.assert_called_once()


def _gateway_descriptor(
Expand Down

0 comments on commit f7eaeb7

Please sign in to comment.