Fix KNX Keyfile upload (#89029)

* Fix KNX Keyfile upload

* use shutil.move instead
This commit is contained in:
Matthias Alphart 2023-03-02 16:13:02 +01:00 committed by Paulus Schoutsen
parent 3e961d3e17
commit f7eaeb7a39
2 changed files with 14 additions and 8 deletions

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator from collections.abc import AsyncGenerator
from pathlib import Path from pathlib import Path
import shutil
from typing import Any, Final from typing import Any, Final
import voluptuous as vol import voluptuous as vol
@ -549,9 +550,12 @@ class KNXCommonFlow(ABC, FlowHandler):
), ),
None, 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 = ( self.new_title = (
f"{'Secure ' if _if_user_id else ''}" f"{'Secure ' if _if_user_id else ''}Tunneling{_tunnel_suffix}"
f"Tunneling @ {selected_tunnel_ia or self.new_entry_data[CONF_HOST]}"
) )
return self.finish_flow() return self.finish_flow()
@ -708,7 +712,8 @@ class KNXCommonFlow(ABC, FlowHandler):
else: else:
dest_path = Path(self.hass.config.path(STORAGE_DIR, DOMAIN)) dest_path = Path(self.hass.config.path(STORAGE_DIR, DOMAIN))
dest_path.mkdir(exist_ok=True) 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 return keyring, errors
keyring, errors = await self.hass.async_add_executor_job(_process_upload) keyring, errors = await self.hass.async_add_executor_job(_process_upload)

View file

@ -77,16 +77,17 @@ def patch_file_upload(return_value=FIXTURE_KEYRING, side_effect=None):
side_effect=side_effect, side_effect=side_effect,
), patch( ), patch(
"pathlib.Path.mkdir" "pathlib.Path.mkdir"
) as mkdir_mock: ) as mkdir_mock, patch(
file_path_mock = Mock() "shutil.move"
file_upload_mock.return_value.__enter__.return_value = file_path_mock ) as shutil_move_mock:
file_upload_mock.return_value.__enter__.return_value = Mock()
yield return_value yield return_value
if side_effect: if side_effect:
mkdir_mock.assert_not_called() mkdir_mock.assert_not_called()
file_path_mock.rename.assert_not_called() shutil_move_mock.assert_not_called()
else: else:
mkdir_mock.assert_called_once() mkdir_mock.assert_called_once()
file_path_mock.rename.assert_called_once() shutil_move_mock.assert_called_once()
def _gateway_descriptor( def _gateway_descriptor(