core/tests/components/knx/test_interface_device.py

138 lines
5.0 KiB
Python
Raw Normal View History

Create, update and delete KNX entities from UI / WS-commands (#104079) * knx entity CRUD - initial commit - switch * platform dependent schema * coerce empty GA-lists to None * read entity configuration from WS * use entity_id instead of unique_id for lookup * Add device support * Rename KNXEntityStore to KNXConfigStore * fix test after rename * Send schema options for creating / editing entities * Return entity_id after entity creation * remove device_class config in favour of more-info-dialog settings * refactor group address schema for custom selector * Rename GA keys and remove invalid keys from schema * fix rebase * Fix deleting devices and their entities * Validate entity schema in extra step - return validation infos * Use exception to signal validation error; return validated data * Forward validation result when editing entities * Get proper validation error message for optional GAs * Add entity validation only WS command * use ulid instead of uuid * Fix error handling for edit unknown entity * Remove unused optional group address sets from validated schema * Add optional dpt field for ga_schema * Move knx config things to sub-key * Add light platform * async_forward_entry_setups only once * Test crate and remove devices * Test removing entities of a removed device * Test entity creation and storage * Test deleting entities * Test unsuccessful entity creation * Test updating entity data * Test get entity config * Test validate entity * Update entity data by entity_id instead of unique_id * Remove unnecessary uid unique check * remove schema_options * test fixture for entity creation * clean up group address schema class can be used to add custom serializer later * Revert: Add light platfrom * remove unused optional_ga_schema * Test GASelector * lint tests * Review * group entities before adding * fix / ignore mypy * always has_entity_name * Entity name: check for empty string when no device * use constants instead of strings in schema * Fix mypy errors for voluptuous schemas --------- Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
2024-07-21 18:01:48 +00:00
"""Test KNX interface device."""
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from xknx.core import XknxConnectionState, XknxConnectionType
from xknx.telegram import IndividualAddress
from homeassistant.components.knx.sensor import SCAN_INTERVAL
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
Create, update and delete KNX entities from UI / WS-commands (#104079) * knx entity CRUD - initial commit - switch * platform dependent schema * coerce empty GA-lists to None * read entity configuration from WS * use entity_id instead of unique_id for lookup * Add device support * Rename KNXEntityStore to KNXConfigStore * fix test after rename * Send schema options for creating / editing entities * Return entity_id after entity creation * remove device_class config in favour of more-info-dialog settings * refactor group address schema for custom selector * Rename GA keys and remove invalid keys from schema * fix rebase * Fix deleting devices and their entities * Validate entity schema in extra step - return validation infos * Use exception to signal validation error; return validated data * Forward validation result when editing entities * Get proper validation error message for optional GAs * Add entity validation only WS command * use ulid instead of uuid * Fix error handling for edit unknown entity * Remove unused optional group address sets from validated schema * Add optional dpt field for ga_schema * Move knx config things to sub-key * Add light platform * async_forward_entry_setups only once * Test crate and remove devices * Test removing entities of a removed device * Test entity creation and storage * Test deleting entities * Test unsuccessful entity creation * Test updating entity data * Test get entity config * Test validate entity * Update entity data by entity_id instead of unique_id * Remove unnecessary uid unique check * remove schema_options * test fixture for entity creation * clean up group address schema class can be used to add custom serializer later * Revert: Add light platfrom * remove unused optional_ga_schema * Test GASelector * lint tests * Review * group entities before adding * fix / ignore mypy * always has_entity_name * Entity name: check for empty string when no device * use constants instead of strings in schema * Fix mypy errors for voluptuous schemas --------- Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
2024-07-21 18:01:48 +00:00
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component
from .conftest import KNXTestKit
from tests.common import async_capture_events, async_fire_time_changed
Create, update and delete KNX entities from UI / WS-commands (#104079) * knx entity CRUD - initial commit - switch * platform dependent schema * coerce empty GA-lists to None * read entity configuration from WS * use entity_id instead of unique_id for lookup * Add device support * Rename KNXEntityStore to KNXConfigStore * fix test after rename * Send schema options for creating / editing entities * Return entity_id after entity creation * remove device_class config in favour of more-info-dialog settings * refactor group address schema for custom selector * Rename GA keys and remove invalid keys from schema * fix rebase * Fix deleting devices and their entities * Validate entity schema in extra step - return validation infos * Use exception to signal validation error; return validated data * Forward validation result when editing entities * Get proper validation error message for optional GAs * Add entity validation only WS command * use ulid instead of uuid * Fix error handling for edit unknown entity * Remove unused optional group address sets from validated schema * Add optional dpt field for ga_schema * Move knx config things to sub-key * Add light platform * async_forward_entry_setups only once * Test crate and remove devices * Test removing entities of a removed device * Test entity creation and storage * Test deleting entities * Test unsuccessful entity creation * Test updating entity data * Test get entity config * Test validate entity * Update entity data by entity_id instead of unique_id * Remove unnecessary uid unique check * remove schema_options * test fixture for entity creation * clean up group address schema class can be used to add custom serializer later * Revert: Add light platfrom * remove unused optional_ga_schema * Test GASelector * lint tests * Review * group entities before adding * fix / ignore mypy * always has_entity_name * Entity name: check for empty string when no device * use constants instead of strings in schema * Fix mypy errors for voluptuous schemas --------- Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
2024-07-21 18:01:48 +00:00
from tests.typing import WebSocketGenerator
async def test_diagnostic_entities(
hass: HomeAssistant,
knx: KNXTestKit,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test diagnostic entities."""
await knx.setup_integration({})
for entity_id in (
"sensor.knx_interface_individual_address",
"sensor.knx_interface_connection_established",
"sensor.knx_interface_connection_type",
"sensor.knx_interface_incoming_telegrams",
"sensor.knx_interface_incoming_telegram_errors",
"sensor.knx_interface_outgoing_telegrams",
"sensor.knx_interface_outgoing_telegram_errors",
"sensor.knx_interface_telegrams",
):
entity = entity_registry.async_get(entity_id)
assert entity.entity_category is EntityCategory.DIAGNOSTIC
for entity_id in (
"sensor.knx_interface_incoming_telegrams",
"sensor.knx_interface_outgoing_telegrams",
):
entity = entity_registry.async_get(entity_id)
assert entity.disabled is True
knx.xknx.connection_manager.cemi_count_incoming = 20
knx.xknx.connection_manager.cemi_count_incoming_error = 1
knx.xknx.connection_manager.cemi_count_outgoing = 10
knx.xknx.connection_manager.cemi_count_outgoing_error = 2
events = async_capture_events(hass, "state_changed")
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(events) == 3 # 5 polled sensors - 2 disabled
events.clear()
for entity_id, test_state in (
("sensor.knx_interface_individual_address", "0.0.0"),
("sensor.knx_interface_connection_type", "Tunnel TCP"),
# skipping connected_since timestamp
("sensor.knx_interface_incoming_telegram_errors", "1"),
("sensor.knx_interface_outgoing_telegram_errors", "2"),
("sensor.knx_interface_telegrams", "31"),
):
assert hass.states.get(entity_id).state == test_state
knx.xknx.connection_manager.connection_state_changed(
state=XknxConnectionState.DISCONNECTED
)
await hass.async_block_till_done()
assert len(events) == 4 # 3 not always_available + 3 force_update - 2 disabled
events.clear()
knx.xknx.current_address = IndividualAddress("1.1.1")
knx.xknx.connection_manager.connection_state_changed(
state=XknxConnectionState.CONNECTED,
connection_type=XknxConnectionType.TUNNEL_UDP,
)
await hass.async_block_till_done()
assert len(events) == 6 # all diagnostic sensors - counters are reset on connect
for entity_id, test_state in (
("sensor.knx_interface_individual_address", "1.1.1"),
("sensor.knx_interface_connection_type", "Tunnel UDP"),
# skipping connected_since timestamp
("sensor.knx_interface_incoming_telegram_errors", "0"),
("sensor.knx_interface_outgoing_telegram_errors", "0"),
("sensor.knx_interface_telegrams", "0"),
):
assert hass.states.get(entity_id).state == test_state
async def test_removed_entity(
hass: HomeAssistant, knx: KNXTestKit, entity_registry: er.EntityRegistry
) -> None:
"""Test unregister callback when entity is removed."""
with patch(
"xknx.core.connection_manager.ConnectionManager.unregister_connection_state_changed_cb"
) as unregister_mock:
await knx.setup_integration({})
entity_registry.async_update_entity(
"sensor.knx_interface_connection_established",
disabled_by=er.RegistryEntryDisabler.USER,
)
unregister_mock.assert_called_once()
Create, update and delete KNX entities from UI / WS-commands (#104079) * knx entity CRUD - initial commit - switch * platform dependent schema * coerce empty GA-lists to None * read entity configuration from WS * use entity_id instead of unique_id for lookup * Add device support * Rename KNXEntityStore to KNXConfigStore * fix test after rename * Send schema options for creating / editing entities * Return entity_id after entity creation * remove device_class config in favour of more-info-dialog settings * refactor group address schema for custom selector * Rename GA keys and remove invalid keys from schema * fix rebase * Fix deleting devices and their entities * Validate entity schema in extra step - return validation infos * Use exception to signal validation error; return validated data * Forward validation result when editing entities * Get proper validation error message for optional GAs * Add entity validation only WS command * use ulid instead of uuid * Fix error handling for edit unknown entity * Remove unused optional group address sets from validated schema * Add optional dpt field for ga_schema * Move knx config things to sub-key * Add light platform * async_forward_entry_setups only once * Test crate and remove devices * Test removing entities of a removed device * Test entity creation and storage * Test deleting entities * Test unsuccessful entity creation * Test updating entity data * Test get entity config * Test validate entity * Update entity data by entity_id instead of unique_id * Remove unnecessary uid unique check * remove schema_options * test fixture for entity creation * clean up group address schema class can be used to add custom serializer later * Revert: Add light platfrom * remove unused optional_ga_schema * Test GASelector * lint tests * Review * group entities before adding * fix / ignore mypy * always has_entity_name * Entity name: check for empty string when no device * use constants instead of strings in schema * Fix mypy errors for voluptuous schemas --------- Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
2024-07-21 18:01:48 +00:00
async def test_remove_interface_device(
hass: HomeAssistant,
knx: KNXTestKit,
device_registry: dr.DeviceRegistry,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test device removal."""
assert await async_setup_component(hass, "config", {})
await knx.setup_integration({})
client = await hass_ws_client(hass)
knx_devices = device_registry.devices.get_devices_for_config_entry_id(
knx.mock_config_entry.entry_id
)
assert len(knx_devices) == 1
assert knx_devices[0].name == "KNX Interface"
device_id = knx_devices[0].id
# interface device can't be removed
res = await client.remove_device(device_id, knx.mock_config_entry.entry_id)
assert not res["success"]
assert (
res["error"]["message"]
== "Failed to remove device entry, rejected by integration"
)