From efd437c356cc6bc09adfb202e0b2ef3f2f4e63ec Mon Sep 17 00:00:00 2001 From: Andreas Date: Tue, 24 Nov 2026 15:12:40 +0100 Subject: [PATCH] Add KNX project data from other sources (#192341) --- homeassistant/components/knx/__init__.py | 5 +- homeassistant/components/knx/project.py | 109 +- homeassistant/components/knx/strings.json | 6 + homeassistant/components/knx/websocket.py | 25 + tests/components/knx/conftest.py | 6 + .../knx/fixtures/project_current.json | 1205 +++++++++++++++++ tests/components/knx/test_project.py | 224 +++ tests/components/knx/test_websocket.py | 69 + 8 files changed, 1647 insertions(+), 2 deletions(-) create mode 100644 tests/components/knx/fixtures/project_current.json diff --git a/homeassistant/components/knx/__init__.py b/homeassistant/components/knx/__init__.py index 9c8765df5081..6c9ec3bc80c0 100644 --- a/homeassistant/components/knx/__init__.py +++ b/homeassistant/components/knx/__init__.py @@ -41,7 +41,10 @@ from .const import ( from .expose import create_combined_knx_exposure from .knx_module import KNXModule from .llm_api import async_register_llm_api -from .project import STORAGE_KEY as PROJECT_STORAGE_KEY +from .project import ( + STORAGE_KEY as PROJECT_STORAGE_KEY, + async_set_project as async_set_project, # for other integrations +) from .schema import ( BinarySensorSchema, ButtonSchema, diff --git a/homeassistant/components/knx/project.py b/homeassistant/components/knx/project.py index 62bcb40c91f5..b069e0186343 100644 --- a/homeassistant/components/knx/project.py +++ b/homeassistant/components/knx/project.py @@ -1,14 +1,19 @@ """Handle KNX project data.""" import asyncio +from collections.abc import Mapping from dataclasses import dataclass import logging -from typing import Final +from typing import Any, Final +from awesomeversion import AwesomeVersion, AwesomeVersionException +import probatio from xknx import XKNX from xknx.dpt import DPTBase +from xknx.exceptions import CouldNotParseAddress from xknx.telegram.address import DeviceAddressableType, GroupAddress, GroupAddressType from xknxproject import XKNXProj +from xknxproject.__version__ import __version__ as XKNXPROJECT_VERSION from xknxproject.models import ( Device, DPTType, @@ -21,9 +26,11 @@ from xknxproject.models import ( from homeassistant.components.file_upload import process_uploaded_file from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.storage import Store from .const import DOMAIN +from .services import get_knx_module _LOGGER = logging.getLogger(__name__) @@ -31,6 +38,102 @@ STORAGE_VERSION: Final = 1 STORAGE_KEY: Final = f"{DOMAIN}/knx_project.json" +def _group_addresses(value: dict[str, Any]) -> dict[str, Any]: + """Validate group addresses beyond their types.""" + for key, group_address in value.items(): + address = group_address["address"] + try: + GroupAddress(address) + except CouldNotParseAddress as err: + raise probatio.Invalid(f"invalid group address `{address}`") from err + if key != address: + raise probatio.Invalid(f"group address `{address}` is keyed as `{key}`") + return value + + +def _info(value: dict[str, Any]) -> dict[str, Any]: + """Validate the project info beyond its types.""" + probatio.In([style.value for style in XknxProjectGroupAddressStyle])( + value["group_address_style"] + ) + return value + + +# Project data in the format of the installed xknxproject, built from its types. +PROJECT_DATA_SCHEMA: Final = probatio.TypedDictSchema( + KNXProjectModel, + {"group_addresses": _group_addresses, "info": _info}, + extra=probatio.ALLOW_EXTRA, +) + + +def _check_parser_version(project: Any) -> None: + """Refuse data of a minor version of xknxproject other than the installed one. + + A minor version can add required keys, which the schema would only report + as missing. Data without a version is left to the schema to report. + """ + info = project.get("info") if isinstance(project, Mapping) else None + version = info.get("xknxproject_version") if isinstance(info, Mapping) else None + if not isinstance(version, str): + return + try: + data_version = AwesomeVersion(version) + installed_version = AwesomeVersion(XKNXPROJECT_VERSION) + compatible = (data_version.major, data_version.minor) == ( + installed_version.major, + installed_version.minor, + ) + except AwesomeVersionException: + compatible = False + if not compatible: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="project_data_incompatible", + translation_placeholders={ + "version": version, + "installed_version": XKNXPROJECT_VERSION, + }, + ) + + +def validate_project_data(project: Any) -> KNXProjectModel: + """Validate project data in the format of the installed xknxproject. + + Raises HomeAssistantError when the data was created for another minor + version of xknxproject, or doesn't have its format. + """ + _check_parser_version(project) + try: + data = PROJECT_DATA_SCHEMA(project) + except probatio.Invalid as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="project_data_invalid", + translation_placeholders={"error": str(err)}, + ) from err + return data + + +async def async_set_project(hass: HomeAssistant, project: Mapping[str, Any]) -> None: + """Replace the KNX project with project data in the format of xknxproject. + + For integrations and tools that provide project data from another source + than a project file, eg. a project editor keeping it up to date while it is + edited. The data is validated first; it is stored and loaded as an uploaded + project file is. + + Each call writes the whole project to storage and reloads its group + addresses. Debouncing frequent updates is left to the caller. + + Raises HomeAssistantError when the KNX integration is not loaded, or the + data is invalid or incompatible. The current project is kept then. + """ + knx_module = get_knx_module(hass) + data = validate_project_data(project) + await knx_module.project.async_store_project(knx_module.xknx, data) + + @dataclass class GroupAddressInfo: """Group address info for runtime usage.""" @@ -131,6 +234,10 @@ class KNXProject: return xknxproj.parse() project = await self.hass.async_add_executor_job(_parse_project) + await self.async_store_project(xknx, project) + + async def async_store_project(self, xknx: XKNX, project: KNXProjectModel) -> None: + """Store and load parsed project data, replacing the current project.""" async with self._project_lock: await self._store.async_save(project) # Invalidate rather than populate: whether the full project is held diff --git a/homeassistant/components/knx/strings.json b/homeassistant/components/knx/strings.json index e281cfd685ae..0e8c70f28eee 100644 --- a/homeassistant/components/knx/strings.json +++ b/homeassistant/components/knx/strings.json @@ -1645,6 +1645,12 @@ "llm_tool_failed": { "message": "KNX tool call failed: {error}" }, + "project_data_incompatible": { + "message": "The KNX project data is for xknxproject {version}, which is not compatible with the installed xknxproject {installed_version}." + }, + "project_data_invalid": { + "message": "Invalid KNX project data: {error}" + }, "service_event_register_ga_not_found": { "message": "Could not find registered event for `{group_addresses}` to remove." }, diff --git a/homeassistant/components/knx/websocket.py b/homeassistant/components/knx/websocket.py index 5ca499d99412..4e7ba411317f 100644 --- a/homeassistant/components/knx/websocket.py +++ b/homeassistant/components/knx/websocket.py @@ -39,6 +39,7 @@ from .const import ( UI_DEVICE_ID_PREFIX, ) from .dpt import get_supported_dpts +from .project import async_set_project from .storage.config_store import ConfigStoreException from .storage.const import CONF_DATA from .storage.entity_store_schema import ( @@ -76,6 +77,7 @@ async def register_panel(hass: HomeAssistant) -> None: websocket_api.async_register_command(hass, ws_query_telegrams) websocket_api.async_register_command(hass, ws_subscribe_telegram) websocket_api.async_register_command(hass, ws_get_knx_project) + websocket_api.async_register_command(hass, ws_set_knx_project) websocket_api.async_register_command(hass, ws_get_entity_suggestions) websocket_api.async_register_command(hass, ws_validate_entity) websocket_api.async_register_command(hass, ws_create_entity) @@ -260,6 +262,29 @@ async def ws_get_knx_project( ) +@websocket_api.require_admin +@websocket_api.websocket_command( + { + probatio.Required("type"): "knx/set_knx_project", + probatio.Required("project"): dict, + } +) +@websocket_api.async_response +async def ws_set_knx_project( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict, +) -> None: + """Replace the KNX project with project data in the format of xknxproject. + + The whole project is one message, so it is limited by the maximum message + size of the WebSocket API (4 MiB): a larger message closes the connection. + Larger project data can be set with `async_set_project` from Python. + """ + await async_set_project(hass, msg["project"]) + connection.send_result(msg["id"]) + + @websocket_api.require_admin @websocket_api.websocket_command( { diff --git a/tests/components/knx/conftest.py b/tests/components/knx/conftest.py index 59f2959c57ae..05695633eb4b 100644 --- a/tests/components/knx/conftest.py +++ b/tests/components/knx/conftest.py @@ -463,6 +463,12 @@ async def project_data(hass: HomeAssistant) -> dict[str, Any]: return await async_load_json_object_fixture(hass, "project.json", DOMAIN) +@pytest.fixture +async def current_project_data(hass: HomeAssistant) -> dict[str, Any]: + """Return project data in the format of the installed xknxproject.""" + return await async_load_json_object_fixture(hass, "project_current.json", DOMAIN) + + @pytest.fixture async def load_knxproj( project_data: dict[str, Any], hass_storage: dict[str, Any] diff --git a/tests/components/knx/fixtures/project_current.json b/tests/components/knx/fixtures/project_current.json new file mode 100644 index 000000000000..bf64a27fe17f --- /dev/null +++ b/tests/components/knx/fixtures/project_current.json @@ -0,0 +1,1205 @@ +{ + "info": { + "project_id": "P-0242", + "name": "xknx test project", + "last_modified": "2024-02-16T18:17:22.3052256Z", + "group_address_style": "ThreeLevel", + "guid": "7aceb083-274e-4568-806f-b8f9df992873", + "created_by": "ETS5", + "schema_version": "20", + "tool_version": "5.7.1428.39779", + "xknxproject_version": "3.11.1", + "language_code": null + }, + "communication_objects": { + "1.1.5/O-40_R-1433": { + "name": "Ausgang B", + "number": 40, + "text": "Output B", + "function_text": "Move blinds/shutter up-down", + "description": "", + "device_address": "1.1.5", + "device_application": "M-0002_A-A066-14-550B", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": null + } + ], + "object_size": "1 Bit", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": false + }, + "group_address_links": ["1/0/5"], + "dpas": null, + "text_parameter": null + }, + "1.1.5/O-41_R-1434": { + "name": "Ausgang B", + "number": 41, + "text": "Output B", + "function_text": "Slat adjustm./stop up-down", + "description": "", + "device_address": "1.1.5", + "device_application": "M-0002_A-A066-14-550B", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": null + } + ], + "object_size": "1 Bit", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": false + }, + "group_address_links": ["1/0/4"], + "dpas": null, + "text_parameter": null + }, + "1.1.5/O-70_R-1505": { + "name": "Ausgang C", + "number": 70, + "text": "Output C", + "function_text": "Move blinds/shutter up-down", + "description": "", + "device_address": "1.1.5", + "device_application": "M-0002_A-A066-14-550B", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": 8 + } + ], + "object_size": "1 Bit", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": false + }, + "group_address_links": ["1/0/3"], + "dpas": null, + "text_parameter": null + }, + "1.1.5/O-71_R-1506": { + "name": "Ausgang C", + "number": 71, + "text": "Output C", + "function_text": "Slat adjustm./stop up-down", + "description": "", + "device_address": "1.1.5", + "device_application": "M-0002_A-A066-14-550B", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": 8 + } + ], + "object_size": "1 Bit", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": false + }, + "group_address_links": ["1/0/2"], + "dpas": null, + "text_parameter": null + }, + "1.1.5/O-100_R-1577": { + "name": "Ausgang D", + "number": 100, + "text": "Output D", + "function_text": "Move blinds/shutter up-down", + "description": "", + "device_address": "1.1.5", + "device_application": "M-0002_A-A066-14-550B", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": null + } + ], + "object_size": "1 Bit", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": false + }, + "group_address_links": ["1/0/1"], + "dpas": null, + "text_parameter": null + }, + "1.1.5/O-101_R-1578": { + "name": "Ausgang D", + "number": 101, + "text": "Output D", + "function_text": "Slat adjustm./stop up-down", + "description": "", + "device_address": "1.1.5", + "device_application": "M-0002_A-A066-14-550B", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": 8 + } + ], + "object_size": "1 Bit", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": false + }, + "group_address_links": ["1/0/0"], + "dpas": null, + "text_parameter": null + }, + "1.1.5/O-4_R-1417": { + "name": "Ausgang A-X", + "number": 4, + "text": "Output A-X", + "function_text": "Wind alarm no. 1", + "description": "", + "device_address": "1.1.5", + "device_application": "M-0002_A-A066-14-550B", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": 1 + } + ], + "object_size": "1 Bit", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": true, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["2/0/6"], + "dpas": null, + "text_parameter": null + }, + "1.1.6/O-0_R-665": { + "name": "Kanal A", + "number": 0, + "text": "ChannelA", + "function_text": "Output value", + "description": "2-byte generic", + "device_address": "1.1.6", + "device_application": "M-0002_A-A01B-14-1B7C", + "module_def": null, + "channel": null, + "dpts": [], + "object_size": "2 Bytes", + "flags": { + "read": true, + "write": false, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["2/1/22"], + "dpas": null, + "text_parameter": null + }, + "1.1.6/O-20_R-8108": { + "name": "Kanal C", + "number": 20, + "text": "ChannelC", + "function_text": "Measured value outside range", + "description": "1-bit undefined DPT", + "device_address": "1.1.6", + "device_application": "M-0002_A-A01B-14-1B7C", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": null + } + ], + "object_size": "1 Bit", + "flags": { + "read": true, + "write": false, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["2/1/10"], + "dpas": null, + "text_parameter": null + }, + "1.1.6/O-9_R-9073": { + "name": "Kanal B", + "number": 9, + "text": "ChannelB", + "function_text": "Output value", + "description": "2-byte DPT 9 any", + "device_address": "1.1.6", + "device_application": "M-0002_A-A01B-14-1B7C", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 9, + "sub": null + }, + { + "main": 9, + "sub": 1 + }, + { + "main": 9, + "sub": 2 + }, + { + "main": 9, + "sub": 3 + }, + { + "main": 9, + "sub": 4 + }, + { + "main": 9, + "sub": 5 + }, + { + "main": 9, + "sub": 6 + }, + { + "main": 9, + "sub": 7 + }, + { + "main": 9, + "sub": 8 + }, + { + "main": 9, + "sub": 9 + }, + { + "main": 9, + "sub": 10 + }, + { + "main": 9, + "sub": 11 + }, + { + "main": 9, + "sub": 20 + }, + { + "main": 9, + "sub": 21 + }, + { + "main": 9, + "sub": 22 + }, + { + "main": 9, + "sub": 23 + }, + { + "main": 9, + "sub": 24 + }, + { + "main": 9, + "sub": 25 + }, + { + "main": 9, + "sub": 26 + }, + { + "main": 9, + "sub": 27 + }, + { + "main": 9, + "sub": 28 + }, + { + "main": 9, + "sub": 29 + }, + { + "main": 9, + "sub": 30 + }, + { + "main": 9, + "sub": 31 + } + ], + "object_size": "2 Bytes", + "flags": { + "read": true, + "write": false, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["2/1/21", "2/1/22", "2/1/23"], + "dpas": null, + "text_parameter": null + }, + "1.1.6/O-18_R-7994": { + "name": "Kanal C", + "number": 18, + "text": "ChannelC", + "function_text": "Output value", + "description": "2-byte DPT 9 generic", + "device_address": "1.1.6", + "device_application": "M-0002_A-A01B-14-1B7C", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 9, + "sub": null + } + ], + "object_size": "2 Bytes", + "flags": { + "read": true, + "write": false, + "communication": true, + "update": false, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["2/1/23", "7/1/2"], + "dpas": null, + "text_parameter": null + }, + "1.1.7/MD-2_M-20_MI-1_O-2-2_R-3": { + "name": "RTR1 - 2 - Controller", + "number": 935, + "text": "RTR 1 (name in application) - Ausgang", + "function_text": "Command value - Heating", + "description": "", + "device_address": "1.1.7", + "device_application": "M-0008_A-20E0-21-9997-O000A", + "module_def": { + "definition": "MD-2", + "root_number": 2 + }, + "channel": "MD-2_M-20_MI-1_CH-81", + "dpts": [ + { + "main": 5, + "sub": 1 + } + ], + "object_size": "1 Byte", + "flags": { + "read": true, + "write": false, + "communication": true, + "update": true, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["7/1/1"], + "dpas": null, + "text_parameter": { + "identifier": "MD-2_M-20_MI-1_P-15_R-17", + "value": "name in application" + } + }, + "1.1.7/MD-2_M-20_MI-1_O-5-0_R-17": { + "name": "RTR1 - 0 - RSM", + "number": 333, + "text": "RTR 1 (name in application) - Eingang", + "function_text": "Setpoint temperature - Active operating mode", + "description": "", + "device_address": "1.1.7", + "device_application": "M-0008_A-20E0-21-9997-O000A", + "module_def": { + "definition": "MD-2", + "root_number": 0 + }, + "channel": "MD-2_M-20_MI-1_CH-81", + "dpts": [ + { + "main": 9, + "sub": 1 + } + ], + "object_size": "2 Bytes", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": true, + "read_on_init": false, + "transmit": false + }, + "group_address_links": ["7/1/0"], + "dpas": null, + "text_parameter": { + "identifier": "MD-2_M-20_MI-1_P-15_R-17", + "value": "name in application" + } + }, + "1.1.7/MD-2_M-20_MI-1_O-5-10_R-18": { + "name": "RTR1 - 10 - RSM", + "number": 343, + "text": "RTR 1 (name in application) - Ausgang", + "function_text": "Setpoint temperature - Active operating mode - Status", + "description": "", + "device_address": "1.1.7", + "device_application": "M-0008_A-20E0-21-9997-O000A", + "module_def": { + "definition": "MD-2", + "root_number": 10 + }, + "channel": "MD-2_M-20_MI-1_CH-81", + "dpts": [ + { + "main": 9, + "sub": 1 + } + ], + "object_size": "2 Bytes", + "flags": { + "read": true, + "write": false, + "communication": true, + "update": true, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["7/0/0"], + "dpas": null, + "text_parameter": { + "identifier": "MD-2_M-20_MI-1_P-15_R-17", + "value": "name in application" + } + }, + "1.1.7/MD-2_M-20_MI-1_O-7-1_R-45": { + "name": "RTR1 - 1 - Sensor", + "number": 1438, + "text": "RTR 1 (name in application) - Eingang", + "function_text": "Room temperature - Measured value 1", + "description": "", + "device_address": "1.1.7", + "device_application": "M-0008_A-20E0-21-9997-O000A", + "module_def": { + "definition": "MD-2", + "root_number": 1 + }, + "channel": "MD-2_M-20_MI-1_CH-81", + "dpts": [ + { + "main": 9, + "sub": 1 + } + ], + "object_size": "2 Bytes", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": true, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["7/1/2"], + "dpas": null, + "text_parameter": { + "identifier": "MD-2_M-20_MI-1_P-15_R-17", + "value": "name in application" + } + }, + "1.1.7/MD-2_M-4_MI-1_O-7-1_R-45": { + "name": "RTR1 - 1 - Sensor", + "number": 1448, + "text": "RTR 2 (...) - Eingang", + "function_text": "Room temperature - Measured value 1", + "description": "", + "device_address": "1.1.7", + "device_application": "M-0008_A-20E0-21-9997-O000A", + "module_def": { + "definition": "MD-2", + "root_number": 1 + }, + "channel": "MD-2_M-4_MI-1_CH-81", + "dpts": [ + { + "main": 9, + "sub": 1 + } + ], + "object_size": "2 Bytes", + "flags": { + "read": false, + "write": true, + "communication": true, + "update": true, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["2/0/1"], + "dpas": null, + "text_parameter": { + "identifier": "MD-2_M-4_MI-1_P-15_R-17", + "value": null + } + }, + "1.1.7/O-3_R-3": { + "name": "Objekt", + "number": 3, + "text": "Manual operation - Input/Output", + "function_text": "Temporary status indication", + "description": "no-channel GO", + "device_address": "1.1.7", + "device_application": "M-0008_A-20E0-21-9997-O000A", + "module_def": null, + "channel": null, + "dpts": [ + { + "main": 1, + "sub": 2 + } + ], + "object_size": "1 Bit", + "flags": { + "read": true, + "write": true, + "communication": true, + "update": true, + "read_on_init": false, + "transmit": true + }, + "group_address_links": ["2/0/0"], + "dpas": null, + "text_parameter": null + } + }, + "topology": { + "0": { + "name": "Backbone Bereich", + "description": null, + "lines": { + "0": { + "name": "Bereichslinie", + "description": null, + "devices": [], + "medium_type": "KNXnet/IP (IP)" + } + } + }, + "1": { + "name": "Neuer Bereich", + "description": null, + "lines": { + "0": { + "name": "Hauptlinie", + "description": null, + "devices": [], + "medium_type": "KNXnet/IP (IP)" + }, + "1": { + "name": "Neue Linie", + "description": null, + "devices": ["1.1.0", "1.1.5", "1.1.6", "1.1.7"], + "medium_type": "Twisted Pair (TP)" + } + } + } + }, + "devices": { + "1.1.0": { + "name": "SCN-IP100.03 IP Router with Secure", + "hardware_name": "SCN-IP100.03 IP Router with Secure", + "order_number": "SCN-IP100.03", + "description": "", + "manufacturer_name": "MDT technologies", + "individual_address": "1.1.0", + "application": "M-0083_A-0139-22-F35B-O0072", + "project_uid": 25, + "product_id": "M-0083_H-136-5-O0072_P-SCN.2DIP100.2E03", + "hardware_id": "M-0083_H-136-5-O0072", + "hardware_program_id": "M-0083_H-136-5-O0072_HP-0139-22-F35B-O0072", + "space_id": null, + "trade_id": null, + "communication_object_ids": [], + "channels": {}, + "group_object_tree": [], + "serial_number": "AIN6QBMW", + "last_download": null, + "individual_address_loaded": false, + "application_program_loaded": false, + "communication_part_loaded": false, + "medium_config_loaded": false, + "parameters_loaded": false + }, + "1.1.5": { + "name": "JRA/S4.230.2.1 Blind/RollerShutterAct,M,4f,230V", + "hardware_name": "JRA/S4.230.2.1 Blind/RollerShutterAct,M,4f,230V", + "order_number": "2CDG 110 121 R0011", + "description": "", + "manufacturer_name": "ABB", + "individual_address": "1.1.5", + "application": "M-0002_A-A066-14-550B", + "project_uid": 31, + "product_id": "M-0002_H-2CDG.20110.20121.20R0011-1_P-2CDG.20110.20121.20R0011", + "hardware_id": "M-0002_H-2CDG.20110.20121.20R0011-1", + "hardware_program_id": "M-0002_H-2CDG.20110.20121.20R0011-1_HP-A066-14-550B", + "space_id": null, + "trade_id": null, + "communication_object_ids": [ + "1.1.5/O-40_R-1433", + "1.1.5/O-41_R-1434", + "1.1.5/O-70_R-1505", + "1.1.5/O-71_R-1506", + "1.1.5/O-100_R-1577", + "1.1.5/O-101_R-1578", + "1.1.5/O-4_R-1417" + ], + "channels": {}, + "group_object_tree": [], + "serial_number": "", + "last_download": null, + "individual_address_loaded": false, + "application_program_loaded": false, + "communication_part_loaded": false, + "medium_config_loaded": false, + "parameters_loaded": false + }, + "1.1.6": { + "name": "AE/S4.2 Analogue Input,4-fold,MDRC", + "hardware_name": "AE/S4.2 Analogue Input,4-fold,MDRC", + "order_number": "2CDG 110 030 R0011", + "description": "", + "manufacturer_name": "ABB", + "individual_address": "1.1.6", + "application": "M-0002_A-A01B-14-1B7C", + "project_uid": 49, + "product_id": "M-0002_H-2CDG.20110.20030.20R0011-1_P-2CDG.20110.20030.20R0011", + "hardware_id": "M-0002_H-2CDG.20110.20030.20R0011-1", + "hardware_program_id": "M-0002_H-2CDG.20110.20030.20R0011-1_HP-A01B-14-1B7C", + "space_id": null, + "trade_id": null, + "communication_object_ids": [ + "1.1.6/O-0_R-665", + "1.1.6/O-20_R-8108", + "1.1.6/O-9_R-9073", + "1.1.6/O-18_R-7994" + ], + "channels": {}, + "group_object_tree": [], + "serial_number": "", + "last_download": null, + "individual_address_loaded": false, + "application_program_loaded": false, + "communication_part_loaded": false, + "medium_config_loaded": false, + "parameters_loaded": false + }, + "1.1.7": { + "name": "Custom name prefix - Heizungsaktor 6fach mit Regler", + "hardware_name": "Heating actuator 6-gang with controller", + "order_number": "2139 00", + "description": "", + "manufacturer_name": "GIRA Giersiepen", + "individual_address": "1.1.7", + "application": "M-0008_A-20E0-21-9997-O000A", + "project_uid": 60, + "product_id": "M-0008_H-8.20.2F.2F.202139.2000-0-O000A_P-2139.2000", + "hardware_id": "M-0008_H-8.20.2F.2F.202139.2000-0-O000A", + "hardware_program_id": "M-0008_H-8.20.2F.2F.202139.2000-0-O000A_HP-20E0-21-9997-O000A", + "space_id": null, + "trade_id": null, + "communication_object_ids": [ + "1.1.7/MD-2_M-20_MI-1_O-2-2_R-3", + "1.1.7/MD-2_M-20_MI-1_O-5-0_R-17", + "1.1.7/MD-2_M-20_MI-1_O-5-10_R-18", + "1.1.7/MD-2_M-20_MI-1_O-7-1_R-45", + "1.1.7/MD-2_M-4_MI-1_O-7-1_R-45", + "1.1.7/O-3_R-3" + ], + "channels": { + "MD-2_M-20_MI-1_CH-81": { + "identifier": "MD-2_M-20_MI-1_CH-81", + "name": "Raumtemperaturregler 1 (name in application)", + "communication_object_ids": [ + "1.1.7/MD-2_M-20_MI-1_O-2-2_R-3", + "1.1.7/MD-2_M-20_MI-1_O-5-0_R-17", + "1.1.7/MD-2_M-20_MI-1_O-5-10_R-18", + "1.1.7/MD-2_M-20_MI-1_O-7-1_R-45" + ], + "functional_blocks": null + }, + "MD-2_M-4_MI-1_CH-81": { + "identifier": "MD-2_M-4_MI-1_CH-81", + "name": "channel name in properties", + "communication_object_ids": ["1.1.7/MD-2_M-4_MI-1_O-7-1_R-45"], + "functional_blocks": null + } + }, + "group_object_tree": [ + { + "type": "Channel", + "identifier": "MD-2_M-20_MI-1_CH-81", + "name": "Raumtemperaturregler 1 (name in application)", + "communication_object_ids": [ + "1.1.7/MD-2_M-20_MI-1_O-2-2_R-3", + "1.1.7/MD-2_M-20_MI-1_O-5-0_R-17", + "1.1.7/MD-2_M-20_MI-1_O-5-10_R-18", + "1.1.7/MD-2_M-20_MI-1_O-7-1_R-45" + ], + "nodes": [] + }, + { + "type": "Channel", + "identifier": "MD-2_M-4_MI-1_CH-81", + "name": "channel name in properties", + "communication_object_ids": ["1.1.7/MD-2_M-4_MI-1_O-7-1_R-45"], + "nodes": [] + } + ], + "serial_number": "", + "last_download": null, + "individual_address_loaded": false, + "application_program_loaded": false, + "communication_part_loaded": false, + "medium_config_loaded": false, + "parameters_loaded": false + } + }, + "group_addresses": { + "1/0/0": { + "name": "Test", + "identifier": "GA-1", + "raw_address": 2048, + "address": "1/0/0", + "project_uid": 34, + "dpt": { + "main": 1, + "sub": 8 + }, + "data_secure": false, + "communication_object_ids": ["1.1.5/O-101_R-1578"], + "description": "", + "comment": "" + }, + "1/0/1": { + "name": "Behang D auf/ab", + "identifier": "GA-2", + "raw_address": 2049, + "address": "1/0/1", + "project_uid": 35, + "dpt": { + "main": 1, + "sub": null + }, + "data_secure": false, + "communication_object_ids": ["1.1.5/O-100_R-1577"], + "description": "", + "comment": "" + }, + "1/0/2": { + "name": "Lamelle C auf/ab", + "identifier": "GA-3", + "raw_address": 2050, + "address": "1/0/2", + "project_uid": 36, + "dpt": { + "main": 1, + "sub": 8 + }, + "data_secure": false, + "communication_object_ids": ["1.1.5/O-71_R-1506"], + "description": "", + "comment": "" + }, + "1/0/3": { + "name": "Behang C auf/ab", + "identifier": "GA-4", + "raw_address": 2051, + "address": "1/0/3", + "project_uid": 37, + "dpt": { + "main": 1, + "sub": 8 + }, + "data_secure": false, + "communication_object_ids": ["1.1.5/O-70_R-1505"], + "description": "", + "comment": "" + }, + "1/0/4": { + "name": "Lamelle B auf/ab", + "identifier": "GA-5", + "raw_address": 2052, + "address": "1/0/4", + "project_uid": 38, + "dpt": { + "main": 1, + "sub": null + }, + "data_secure": false, + "communication_object_ids": ["1.1.5/O-41_R-1434"], + "description": "", + "comment": "" + }, + "1/0/5": { + "name": "BehangB auf/ab", + "identifier": "GA-6", + "raw_address": 2053, + "address": "1/0/5", + "project_uid": 39, + "dpt": { + "main": 1, + "sub": null + }, + "data_secure": false, + "communication_object_ids": ["1.1.5/O-40_R-1433"], + "description": "", + "comment": "" + }, + "2/0/0": { + "name": "channel-test", + "identifier": "GA-19", + "raw_address": 4096, + "address": "2/0/0", + "project_uid": 66, + "dpt": { + "main": 1, + "sub": 2 + }, + "data_secure": true, + "communication_object_ids": ["1.1.7/O-3_R-3"], + "description": "", + "comment": "" + }, + "2/0/1": { + "name": "Temperature", + "identifier": "GA-18", + "raw_address": 4097, + "address": "2/0/1", + "project_uid": 65, + "dpt": { + "main": 9, + "sub": 1 + }, + "data_secure": true, + "communication_object_ids": ["1.1.7/MD-2_M-4_MI-1_O-7-1_R-45"], + "description": "", + "comment": "" + }, + "2/0/6": { + "name": "Windalarm", + "identifier": "GA-7", + "raw_address": 4102, + "address": "2/0/6", + "project_uid": 42, + "dpt": { + "main": 1, + "sub": 1 + }, + "data_secure": false, + "communication_object_ids": ["1.1.5/O-4_R-1417"], + "description": "", + "comment": "" + }, + "2/1/1": { + "name": "Not linked to a comm object", + "identifier": "GA-8", + "raw_address": 4353, + "address": "2/1/1", + "project_uid": 43, + "dpt": null, + "data_secure": false, + "communication_object_ids": [], + "description": "no dpt", + "comment": "" + }, + "2/1/2": { + "name": "Not linked to a comm object 2", + "identifier": "GA-11", + "raw_address": 4354, + "address": "2/1/2", + "project_uid": 47, + "dpt": { + "main": 9, + "sub": 1 + }, + "data_secure": false, + "communication_object_ids": [], + "description": "temperature", + "comment": "" + }, + "2/1/10": { + "name": "generic 1-bit", + "identifier": "GA-13", + "raw_address": 4362, + "address": "2/1/10", + "project_uid": 50, + "dpt": { + "main": 1, + "sub": null + }, + "data_secure": false, + "communication_object_ids": ["1.1.6/O-20_R-8108"], + "description": "", + "comment": "" + }, + "2/1/21": { + "name": "No DPT in GA", + "identifier": "GA-12", + "raw_address": 4373, + "address": "2/1/21", + "project_uid": 48, + "dpt": { + "main": 9, + "sub": null + }, + "data_secure": false, + "communication_object_ids": ["1.1.6/O-9_R-9073"], + "description": "multiple DPT 9 in 1 CO", + "comment": "" + }, + "2/1/22": { + "name": "No DPT in GA 2", + "identifier": "GA-9", + "raw_address": 4374, + "address": "2/1/22", + "project_uid": 44, + "dpt": { + "main": 9, + "sub": null + }, + "data_secure": false, + "communication_object_ids": ["1.1.6/O-0_R-665", "1.1.6/O-9_R-9073"], + "description": "multiple DPT 9 and not defined in COs", + "comment": "" + }, + "2/1/23": { + "name": "No DPT in GA 3", + "identifier": "GA-10", + "raw_address": 4375, + "address": "2/1/23", + "project_uid": 45, + "dpt": { + "main": 9, + "sub": null + }, + "data_secure": false, + "communication_object_ids": ["1.1.6/O-9_R-9073", "1.1.6/O-18_R-7994"], + "description": "DPT 9 generic and multiple in COs", + "comment": "" + }, + "7/0/0": { + "name": "ds 0", + "identifier": "GA-15", + "raw_address": 14336, + "address": "7/0/0", + "project_uid": 58, + "dpt": { + "main": 9, + "sub": 1 + }, + "data_secure": false, + "communication_object_ids": ["1.1.7/MD-2_M-20_MI-1_O-5-10_R-18"], + "description": "security: off (inherited from middle group)", + "comment": "" + }, + "7/1/0": { + "name": "ds 1", + "identifier": "GA-14", + "raw_address": 14592, + "address": "7/1/0", + "project_uid": 57, + "dpt": { + "main": 9, + "sub": 1 + }, + "data_secure": true, + "communication_object_ids": ["1.1.7/MD-2_M-20_MI-1_O-5-0_R-17"], + "description": "security: auto", + "comment": "" + }, + "7/1/1": { + "name": "ds 2", + "identifier": "GA-16", + "raw_address": 14593, + "address": "7/1/1", + "project_uid": 59, + "dpt": { + "main": 5, + "sub": 1 + }, + "data_secure": true, + "communication_object_ids": ["1.1.7/MD-2_M-20_MI-1_O-2-2_R-3"], + "description": "security: on", + "comment": "" + }, + "7/1/2": { + "name": "ds + non ds", + "identifier": "GA-17", + "raw_address": 14594, + "address": "7/1/2", + "project_uid": 64, + "dpt": { + "main": 9, + "sub": 1 + }, + "data_secure": false, + "communication_object_ids": [ + "1.1.6/O-18_R-7994", + "1.1.7/MD-2_M-20_MI-1_O-7-1_R-45" + ], + "description": "", + "comment": "" + } + }, + "group_ranges": { + "1": { + "name": "Neue Hauptgruppe", + "address_start": 2048, + "address_end": 4095, + "group_addresses": [], + "comment": "", + "group_ranges": { + "1/0": { + "name": "Neue Mittelgruppe", + "address_start": 2048, + "address_end": 2303, + "group_addresses": [ + "1/0/0", + "1/0/1", + "1/0/2", + "1/0/3", + "1/0/4", + "1/0/5" + ], + "comment": "", + "group_ranges": {} + } + } + }, + "2": { + "name": "Neue Hauptgruppe", + "address_start": 4096, + "address_end": 6143, + "group_addresses": [], + "comment": "", + "group_ranges": { + "2/0": { + "name": "Neue Mittelgruppe", + "address_start": 4096, + "address_end": 4351, + "group_addresses": ["2/0/6", "2/0/1", "2/0/0"], + "comment": "", + "group_ranges": {} + }, + "2/1": { + "name": "DPT tests", + "address_start": 4352, + "address_end": 4607, + "group_addresses": [ + "2/1/1", + "2/1/22", + "2/1/23", + "2/1/2", + "2/1/21", + "2/1/10" + ], + "comment": "", + "group_ranges": {} + } + } + }, + "7": { + "name": "DataSecureTest", + "address_start": 14336, + "address_end": 16383, + "group_addresses": [], + "comment": "", + "group_ranges": { + "7/0": { + "name": "Non-DS", + "address_start": 14336, + "address_end": 14591, + "group_addresses": ["7/0/0"], + "comment": "", + "group_ranges": {} + }, + "7/1": { + "name": "DS-Auto", + "address_start": 14592, + "address_end": 14847, + "group_addresses": ["7/1/0", "7/1/1", "7/1/2"], + "comment": "", + "group_ranges": {} + } + } + } + }, + "locations": { + "Test (1)": { + "type": "Building", + "identifier": "P-0242-0_BP-1", + "name": "Test (1)", + "usage_id": null, + "usage_text": "", + "number": "", + "description": "", + "project_uid": 6, + "devices": [], + "spaces": {}, + "functions": [] + } + }, + "trades": [], + "functions": {} +} diff --git a/tests/components/knx/test_project.py b/tests/components/knx/test_project.py index a28ae00b7081..b6f8279dea03 100644 --- a/tests/components/knx/test_project.py +++ b/tests/components/knx/test_project.py @@ -9,6 +9,7 @@ left to whoever asks for it, so setup does not populate the cache. from __future__ import annotations import asyncio +from collections.abc import Callable from typing import Any from unittest.mock import patch @@ -16,8 +17,11 @@ import pytest from pytest_homeassistant.typing import WebSocketGenerator from xknx.telegram.address import GroupAddress +from homeassistant.components.knx import async_set_project from homeassistant.components.knx.const import KNX_MODULE_KEY +from homeassistant.components.knx.project import STORAGE_KEY as KNX_PROJECT_STORAGE_KEY from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from .conftest import KNXTestKit @@ -141,3 +145,223 @@ async def test_new_project_replaces_group_addresses( assert "0/0/2" in project.group_addresses assert xknx.group_address_dpt.get(GroupAddress("0/0/1")) is None assert xknx.group_address_dpt.get(GroupAddress("0/0/2")) is not None + + +@pytest.mark.usefixtures("load_knxproj") +async def test_set_project( + hass: HomeAssistant, + knx: KNXTestKit, + current_project_data: dict[str, Any], + hass_storage: dict[str, Any], +) -> None: + """Project data from another source is stored and loaded like an upload.""" + await knx.setup_integration() + project = hass.data[KNX_MODULE_KEY].project + xknx = hass.data[KNX_MODULE_KEY].xknx + assert await project.get_knxproject() is not None + assert "0/0/3" in project.group_addresses + + group_addresses = current_project_data["group_addresses"] + new_data = { + **current_project_data, + "group_addresses": { + address: ga for address, ga in group_addresses.items() if address != "1/0/0" + }, + } + await async_set_project(hass, new_data) + + assert project.info is not None + assert project.info["name"] == "xknx test project" + assert set(project.group_addresses) == set(group_addresses) - {"1/0/0"} + assert xknx.group_address_dpt.get(GroupAddress("0/0/3")) is None + assert xknx.group_address_dpt.get(GroupAddress("2/0/1")) is not None + assert hass_storage[KNX_PROJECT_STORAGE_KEY]["data"] == new_data + knxproject = await project.get_knxproject() + assert knxproject is not None + assert knxproject["info"]["name"] == "xknx test project" + + +async def test_set_project_keeps_unknown_data( + hass: HomeAssistant, + knx: KNXTestKit, + current_project_data: dict[str, Any], + hass_storage: dict[str, Any], +) -> None: + """Data the installed xknxproject doesn't know is stored as it is.""" + await knx.setup_integration() + new_data = { + **current_project_data, + "new_section": {}, + "group_addresses": { + address: {**ga, "new_key": "kept"} + for address, ga in current_project_data["group_addresses"].items() + }, + } + + await async_set_project(hass, new_data) + + assert hass.data[KNX_MODULE_KEY].project.loaded + assert hass_storage[KNX_PROJECT_STORAGE_KEY]["data"] == new_data + + +def _without(data: dict[str, Any], key: str) -> dict[str, Any]: + return {k: v for k, v in data.items() if k != key} + + +# a device of the test project with channels +DEVICE = "1.1.7" +CHANNEL = "MD-2_M-20_MI-1_CH-81" + + +def _with_group_address(data: dict[str, Any], **changes: Any) -> dict[str, Any]: + group_address = data["group_addresses"]["1/0/0"] + return { + **data, + "group_addresses": { + **data["group_addresses"], + "1/0/0": {**group_address, **changes}, + }, + } + + +def _with_device(data: dict[str, Any], device: dict[str, Any]) -> dict[str, Any]: + return {**data, "devices": {**data["devices"], DEVICE: device}} + + +def _with_channel(data: dict[str, Any], **changes: Any) -> dict[str, Any]: + device = data["devices"][DEVICE] + channel = device["channels"][CHANNEL] + return _with_device( + data, + {**device, "channels": {**device["channels"], CHANNEL: {**channel, **changes}}}, + ) + + +def _with_info(data: dict[str, Any], **changes: Any) -> dict[str, Any]: + return {**data, "info": {**data["info"], **changes}} + + +async def test_set_project_two_level( + hass: HomeAssistant, + knx: KNXTestKit, + current_project_data: dict[str, Any], + monkeypatch: pytest.MonkeyPatch, +) -> None: + """The group addresses of a two level project are taken.""" + # loading sets the address format of xknx for the whole process: setting + # the attribute to its own value restores it on teardown + monkeypatch.setattr(GroupAddress, "address_format", GroupAddress.address_format) + await knx.setup_integration() + group_address = current_project_data["group_addresses"]["1/0/0"] + new_data = { + **_with_info(current_project_data, group_address_style="TwoLevel"), + "group_addresses": {"0/1": {**group_address, "address": "0/1"}}, + } + + await async_set_project(hass, new_data) + + assert set(hass.data[KNX_MODULE_KEY].project.group_addresses) == {"0/1"} + + +@pytest.mark.parametrize( + ("change", "translation_key"), + [ + (lambda data: _without(data, "devices"), "project_data_invalid"), + (lambda data: _without(data, "functions"), "project_data_invalid"), + ( + lambda data: {**data, "info": _without(data["info"], "name")}, + "project_data_invalid", + ), + ( + lambda data: _with_info(data, group_address_style="FourLevel"), + "project_data_invalid", + ), + (lambda data: _with_group_address(data, address="x/y"), "project_data_invalid"), + ( + # the address is read from the field, but links refer to the key + lambda data: _with_group_address(data, address="1/0/1"), + "project_data_invalid", + ), + ( + lambda data: _with_group_address(data, dpt={"main": "1", "sub": None}), + "project_data_invalid", + ), + ( + lambda data: _with_device(data, _without(data["devices"][DEVICE], "name")), + "project_data_invalid", + ), + ( + # the semantics entity suggestions read + lambda data: _with_device( + data, _without(data["devices"][DEVICE], "channels") + ), + "project_data_invalid", + ), + ( + lambda data: _with_channel(data, functional_blocks="FB-1"), + "project_data_invalid", + ), + ( + lambda data: { + **data, + "communication_objects": { + object_id: _without(com_object, "dpas") + for object_id, com_object in data["communication_objects"].items() + }, + }, + "project_data_invalid", + ), + ( + lambda data: { + **_without(data, "trades"), + "info": {**data["info"], "xknxproject_version": "3.10.0"}, + }, + "project_data_incompatible", + ), + ( + # a required key of the installed version + lambda data: _without(data, "trades"), + "project_data_invalid", + ), + ( + lambda data: _with_info(data, xknxproject_version="99.0.0"), + "project_data_incompatible", + ), + ( + lambda data: _with_info(data, xknxproject_version="unknown"), + "project_data_incompatible", + ), + ], +) +@pytest.mark.usefixtures("load_knxproj") +async def test_set_project_invalid_data( + hass: HomeAssistant, + knx: KNXTestKit, + project_data: dict[str, Any], + current_project_data: dict[str, Any], + hass_storage: dict[str, Any], + change: Callable[[dict[str, Any]], dict[str, Any]], + translation_key: str, +) -> None: + """Invalid or incompatible data is refused and the project is kept.""" + await knx.setup_integration() + project = hass.data[KNX_MODULE_KEY].project + stored = hass_storage[KNX_PROJECT_STORAGE_KEY] + + with pytest.raises(HomeAssistantError) as exc_info: + await async_set_project(hass, change(current_project_data)) + + assert exc_info.value.translation_key == translation_key + assert hass_storage[KNX_PROJECT_STORAGE_KEY] is stored + assert project.info == project_data["info"] + assert "0/0/3" in project.group_addresses + + +async def test_set_project_not_loaded( + hass: HomeAssistant, current_project_data: dict[str, Any] +) -> None: + """Project data needs a loaded KNX integration.""" + with pytest.raises(HomeAssistantError) as exc_info: + await async_set_project(hass, current_project_data) + + assert exc_info.value.translation_key == "integration_not_loaded" diff --git a/tests/components/knx/test_websocket.py b/tests/components/knx/test_websocket.py index 6c2e052cdc8d..f5f6614d0fb2 100644 --- a/tests/components/knx/test_websocket.py +++ b/tests/components/knx/test_websocket.py @@ -176,6 +176,75 @@ async def test_knx_project_file_process_error( assert not hass.data[KNX_MODULE_KEY].project.loaded +async def test_knx_set_project( + hass: HomeAssistant, + knx: KNXTestKit, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], + current_project_data: dict[str, Any], +) -> None: + """Test knx/set_knx_project command for storing and loading project data.""" + await knx.setup_integration() + client = await hass_ws_client(hass) + assert not hass.data[KNX_MODULE_KEY].project.loaded + + await client.send_json_auto_id( + {"type": "knx/set_knx_project", "project": current_project_data} + ) + res = await client.receive_json() + + assert res["success"], res + assert hass.data[KNX_MODULE_KEY].project.loaded + assert hass_storage[KNX_PROJECT_STORAGE_KEY]["data"] == current_project_data + + +async def test_knx_set_project_invalid( + hass: HomeAssistant, + knx: KNXTestKit, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], + current_project_data: dict[str, Any], +) -> None: + """Test knx/set_knx_project refuses invalid project data.""" + await knx.setup_integration() + client = await hass_ws_client(hass) + + await client.send_json_auto_id( + { + "type": "knx/set_knx_project", + "project": {**current_project_data, "group_addresses": None}, + } + ) + res = await client.receive_json() + + assert not res["success"] + assert res["error"]["code"] == "home_assistant_error" + assert res["error"]["translation_key"] == "project_data_invalid" + assert not hass.data[KNX_MODULE_KEY].project.loaded + assert KNX_PROJECT_STORAGE_KEY not in hass_storage + + +async def test_knx_set_project_not_loaded( + hass: HomeAssistant, + knx: KNXTestKit, + hass_ws_client: WebSocketGenerator, + current_project_data: dict[str, Any], +) -> None: + """Test knx/set_knx_project when the config entry is unloaded.""" + await knx.setup_integration() + await hass.config_entries.async_unload(knx.mock_config_entry.entry_id) + client = await hass_ws_client(hass) + + await client.send_json_auto_id( + {"type": "knx/set_knx_project", "project": current_project_data} + ) + res = await client.receive_json() + + assert not res["success"] + assert res["error"]["code"] == "home_assistant_error" + assert res["error"]["translation_key"] == "integration_not_loaded" + + @pytest.mark.usefixtures("load_knxproj") async def test_knx_project_file_remove( hass: HomeAssistant,