Clean up tests for LCN (#125493)

* Remove patches on 3rd party module level

* Cleanup test_init

* Cleanup platform tests

* Cleanup test_services

* Cleanup test_websockets

* Cleanup test_device_trigger

* Cleanup test_events

* Remove unused fixture
This commit is contained in:
Andre Lengwenus 2024-09-08 09:20:57 +02:00 committed by GitHub
parent 03a6eb26be
commit 5e1b4b2d23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 1715 additions and 1118 deletions

View File

@ -25,6 +25,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import PchkConnectionManager
from .const import CONF_DIM_MODE, CONF_SK_NUM_TRIES, DIM_MODES, DOMAIN from .const import CONF_DIM_MODE, CONF_SK_NUM_TRIES, DIM_MODES, DOMAIN
from .helpers import purge_device_registry, purge_entity_registry from .helpers import purge_device_registry, purge_entity_registry
@ -78,7 +79,7 @@ async def validate_connection(data: ConfigType) -> str | None:
_LOGGER.debug("Validating connection parameters to PCHK host '%s'", host_name) _LOGGER.debug("Validating connection parameters to PCHK host '%s'", host_name)
connection = pypck.connection.PchkConnectionManager( connection = PchkConnectionManager(
host, port, username, password, settings=settings host, port, username, password, settings=settings
) )

View File

@ -53,12 +53,20 @@ class MockPchkConnectionManager(PchkConnectionManager):
async def async_close(self) -> None: async def async_close(self) -> None:
"""Mock closing a connection to PCHK.""" """Mock closing a connection to PCHK."""
@patch.object(pypck.connection, "ModuleConnection", MockModuleConnection)
@patch.object(pypck.connection, "GroupConnection", MockGroupConnection)
def get_address_conn(self, addr, request_serials=False): def get_address_conn(self, addr, request_serials=False):
"""Get LCN address connection.""" """Get LCN address connection."""
return super().get_address_conn(addr, request_serials) return super().get_address_conn(addr, request_serials)
@patch.object(pypck.connection, "ModuleConnection", MockModuleConnection)
def get_module_conn(self, addr, request_serials=False):
"""Get LCN module connection."""
return super().get_module_conn(addr, request_serials)
@patch.object(pypck.connection, "GroupConnection", MockGroupConnection)
def get_group_conn(self, addr):
"""Get LCN group connection."""
return super().get_group_conn(addr)
scan_modules = AsyncMock() scan_modules = AsyncMock()
send_command = AsyncMock() send_command = AsyncMock()
@ -119,15 +127,6 @@ async def init_integration(
return lcn_connection return lcn_connection
@pytest.fixture(name="lcn_connection")
async def init_lcn_connection(
hass: HomeAssistant,
entry: MockConfigEntry,
) -> MockPchkConnectionManager:
"""Set up the LCN integration in Home Assistantand yield connection object."""
return await init_integration(hass, entry)
async def setup_component(hass: HomeAssistant) -> None: async def setup_component(hass: HomeAssistant) -> None:
"""Set up the LCN component.""" """Set up the LCN component."""
fixture_filename = "lcn/config.json" fixture_filename = "lcn/config.json"

View File

@ -0,0 +1,139 @@
# serializer version: 1
# name: test_setup_lcn_binary_sensor[binary_sensor.binary_sensor1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.binary_sensor1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Binary_Sensor1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-binsensor1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_binary_sensor[binary_sensor.binary_sensor1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Binary_Sensor1',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.binary_sensor1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_setup_lcn_binary_sensor[binary_sensor.sensor_keylock-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.sensor_keylock',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Sensor_KeyLock',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-a5',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_binary_sensor[binary_sensor.sensor_keylock-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Sensor_KeyLock',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.sensor_keylock',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_setup_lcn_binary_sensor[binary_sensor.sensor_lockregulator1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.sensor_lockregulator1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Sensor_LockRegulator1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-r1varsetpoint',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_binary_sensor[binary_sensor.sensor_lockregulator1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Sensor_LockRegulator1',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.sensor_lockregulator1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---

View File

@ -0,0 +1,97 @@
# serializer version: 1
# name: test_setup_lcn_cover[cover.cover_outputs-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'cover',
'entity_category': None,
'entity_id': 'cover.cover_outputs',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Cover_Outputs',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': <CoverEntityFeature: 11>,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-outputs',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_cover[cover.cover_outputs-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'assumed_state': True,
'friendly_name': 'Cover_Outputs',
'supported_features': <CoverEntityFeature: 11>,
}),
'context': <ANY>,
'entity_id': 'cover.cover_outputs',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'open',
})
# ---
# name: test_setup_lcn_cover[cover.cover_relays-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'cover',
'entity_category': None,
'entity_id': 'cover.cover_relays',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Cover_Relays',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': <CoverEntityFeature: 11>,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-motor1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_cover[cover.cover_relays-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'assumed_state': True,
'friendly_name': 'Cover_Relays',
'supported_features': <CoverEntityFeature: 11>,
}),
'context': <ANY>,
'entity_id': 'cover.cover_relays',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'open',
})
# ---

View File

@ -0,0 +1,167 @@
# serializer version: 1
# name: test_setup_lcn_light[light.light_output1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.light_output1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light_Output1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': <LightEntityFeature: 32>,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-output1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_light[light.light_output1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'brightness': None,
'color_mode': None,
'friendly_name': 'Light_Output1',
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 32>,
}),
'context': <ANY>,
'entity_id': 'light.light_output1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_setup_lcn_light[light.light_output2-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.ONOFF: 'onoff'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.light_output2',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light_Output2',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': <LightEntityFeature: 32>,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-output2',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_light[light.light_output2-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'color_mode': None,
'friendly_name': 'Light_Output2',
'supported_color_modes': list([
<ColorMode.ONOFF: 'onoff'>,
]),
'supported_features': <LightEntityFeature: 32>,
}),
'context': <ANY>,
'entity_id': 'light.light_output2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_setup_lcn_light[light.light_relay1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.ONOFF: 'onoff'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.light_relay1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light_Relay1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-relay1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_light[light.light_relay1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'color_mode': None,
'friendly_name': 'Light_Relay1',
'supported_color_modes': list([
<ColorMode.ONOFF: 'onoff'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.light_relay1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -0,0 +1,187 @@
# serializer version: 1
# name: test_setup_lcn_sensor[sensor.sensor_led6-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.sensor_led6',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Sensor_Led6',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-led6',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_sensor[sensor.sensor_led6-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Sensor_Led6',
}),
'context': <ANY>,
'entity_id': 'sensor.sensor_led6',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_setup_lcn_sensor[sensor.sensor_logicop1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.sensor_logicop1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Sensor_LogicOp1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-logicop1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_sensor[sensor.sensor_logicop1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Sensor_LogicOp1',
}),
'context': <ANY>,
'entity_id': 'sensor.sensor_logicop1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_setup_lcn_sensor[sensor.sensor_setpoint1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.sensor_setpoint1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Sensor_Setpoint1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-r1varsetpoint',
'unit_of_measurement': '°C',
})
# ---
# name: test_setup_lcn_sensor[sensor.sensor_setpoint1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Sensor_Setpoint1',
'unit_of_measurement': '°C',
}),
'context': <ANY>,
'entity_id': 'sensor.sensor_setpoint1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_setup_lcn_sensor[sensor.sensor_var1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.sensor_var1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Sensor_Var1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-var1',
'unit_of_measurement': '°C',
})
# ---
# name: test_setup_lcn_sensor[sensor.sensor_var1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Sensor_Var1',
'unit_of_measurement': '°C',
}),
'context': <ANY>,
'entity_id': 'sensor.sensor_var1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---

View File

@ -1,203 +0,0 @@
# serializer version: 1
# name: test_service_dyn_text
tuple(
0,
'text in row 1',
)
# ---
# name: test_service_led
tuple(
<LedPort.LED6: 5>,
<LedStatus.BLINK: 'B'>,
)
# ---
# name: test_service_lock_keys
tuple(
0,
list([
<KeyLockStateModifier.OFF: '0'>,
<KeyLockStateModifier.OFF: '0'>,
<KeyLockStateModifier.ON: '1'>,
<KeyLockStateModifier.ON: '1'>,
<KeyLockStateModifier.TOGGLE: 'U'>,
<KeyLockStateModifier.TOGGLE: 'U'>,
<KeyLockStateModifier.NOCHANGE: '-'>,
<KeyLockStateModifier.NOCHANGE: '-'>,
]),
)
# ---
# name: test_service_lock_keys_tab_a_temporary
tuple(
10,
<TimeUnit.SECONDS: 'S'>,
list([
<KeyLockStateModifier.OFF: '0'>,
<KeyLockStateModifier.OFF: '0'>,
<KeyLockStateModifier.ON: '1'>,
<KeyLockStateModifier.ON: '1'>,
<KeyLockStateModifier.TOGGLE: 'U'>,
<KeyLockStateModifier.TOGGLE: 'U'>,
<KeyLockStateModifier.NOCHANGE: '-'>,
<KeyLockStateModifier.NOCHANGE: '-'>,
]),
)
# ---
# name: test_service_lock_regulator
tuple(
0,
True,
)
# ---
# name: test_service_output_abs
tuple(
0,
100,
9,
)
# ---
# name: test_service_output_rel
tuple(
0,
25,
)
# ---
# name: test_service_output_toggle
tuple(
0,
9,
)
# ---
# name: test_service_pck
tuple(
'PIN4',
)
# ---
# name: test_service_relays
tuple(
list([
<RelayStateModifier.OFF: '0'>,
<RelayStateModifier.OFF: '0'>,
<RelayStateModifier.ON: '1'>,
<RelayStateModifier.ON: '1'>,
<RelayStateModifier.TOGGLE: 'U'>,
<RelayStateModifier.TOGGLE: 'U'>,
<RelayStateModifier.NOCHANGE: '-'>,
<RelayStateModifier.NOCHANGE: '-'>,
]),
)
# ---
# name: test_service_send_keys
tuple(
list([
list([
True,
False,
False,
False,
True,
False,
False,
False,
]),
list([
False,
False,
False,
False,
False,
False,
False,
False,
]),
list([
False,
False,
False,
False,
False,
False,
False,
False,
]),
list([
False,
False,
False,
False,
False,
False,
False,
True,
]),
]),
<SendKeyCommand.HIT: 'K'>,
)
# ---
# name: test_service_send_keys_hit_deferred
tuple(
list([
list([
True,
False,
False,
False,
True,
False,
False,
False,
]),
list([
False,
False,
False,
False,
False,
False,
False,
False,
]),
list([
False,
False,
False,
False,
False,
False,
False,
False,
]),
list([
False,
False,
False,
False,
False,
False,
False,
True,
]),
]),
5,
<TimeUnit.SECONDS: 'S'>,
)
# ---
# name: test_service_var_abs
tuple(
<Var.VAR1ORTVAR: 0>,
75.0,
<VarUnit.PERCENT: '%'>,
)
# ---
# name: test_service_var_rel
tuple(
<Var.VAR1ORTVAR: 0>,
10.0,
<VarUnit.PERCENT: '%'>,
<RelVarRef.CURRENT: 1>,
)
# ---
# name: test_service_var_reset
tuple(
<Var.VAR1ORTVAR: 0>,
)
# ---

View File

@ -0,0 +1,231 @@
# serializer version: 1
# name: test_setup_lcn_switch[switch.switch_group5-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.switch_group5',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Switch_Group5',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-g000005-relay1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_switch[switch.switch_group5-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Switch_Group5',
}),
'context': <ANY>,
'entity_id': 'switch.switch_group5',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_setup_lcn_switch[switch.switch_output1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.switch_output1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Switch_Output1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-output1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_switch[switch.switch_output1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Switch_Output1',
}),
'context': <ANY>,
'entity_id': 'switch.switch_output1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_setup_lcn_switch[switch.switch_output2-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.switch_output2',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Switch_Output2',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-output2',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_switch[switch.switch_output2-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Switch_Output2',
}),
'context': <ANY>,
'entity_id': 'switch.switch_output2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_setup_lcn_switch[switch.switch_relay1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.switch_relay1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Switch_Relay1',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-relay1',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_switch[switch.switch_relay1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Switch_Relay1',
}),
'context': <ANY>,
'entity_id': 'switch.switch_relay1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_setup_lcn_switch[switch.switch_relay2-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.switch_relay2',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Switch_Relay2',
'platform': 'lcn',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'lcn/config_entry_pchk.json-m000007-relay2',
'unit_of_measurement': None,
})
# ---
# name: test_setup_lcn_switch[switch.switch_relay2-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Switch_Relay2',
}),
'context': <ANY>,
'entity_id': 'switch.switch_relay2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -1,68 +1,46 @@
"""Test for the LCN binary sensor platform.""" """Test for the LCN binary sensor platform."""
from unittest.mock import patch
from pypck.inputs import ModStatusBinSensors, ModStatusKeyLocks, ModStatusVar from pypck.inputs import ModStatusBinSensors, ModStatusKeyLocks, ModStatusVar
from pypck.lcn_addr import LcnAddr from pypck.lcn_addr import LcnAddr
from pypck.lcn_defs import Var, VarValue from pypck.lcn_defs import Var, VarValue
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.lcn.helpers import get_device_connection from homeassistant.components.lcn.helpers import get_device_connection
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import MockConfigEntry, init_integration
from tests.common import snapshot_platform
BINARY_SENSOR_LOCKREGULATOR1 = "binary_sensor.sensor_lockregulator1" BINARY_SENSOR_LOCKREGULATOR1 = "binary_sensor.sensor_lockregulator1"
BINARY_SENSOR_SENSOR1 = "binary_sensor.binary_sensor1" BINARY_SENSOR_SENSOR1 = "binary_sensor.binary_sensor1"
BINARY_SENSOR_KEYLOCK = "binary_sensor.sensor_keylock" BINARY_SENSOR_KEYLOCK = "binary_sensor.sensor_keylock"
async def test_setup_lcn_binary_sensor(hass: HomeAssistant, lcn_connection) -> None: async def test_setup_lcn_binary_sensor(
"""Test the setup of binary sensor.""" hass: HomeAssistant,
for entity_id in ( entity_registry: er.EntityRegistry,
BINARY_SENSOR_LOCKREGULATOR1, entry: MockConfigEntry,
BINARY_SENSOR_SENSOR1, snapshot: SnapshotAssertion,
BINARY_SENSOR_KEYLOCK,
):
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNKNOWN
async def test_entity_state(hass: HomeAssistant, lcn_connection) -> None:
"""Test state of entity."""
state = hass.states.get(BINARY_SENSOR_LOCKREGULATOR1)
assert state
state = hass.states.get(BINARY_SENSOR_SENSOR1)
assert state
state = hass.states.get(BINARY_SENSOR_KEYLOCK)
assert state
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None: ) -> None:
"""Test the attributes of an entity.""" """Test the setup of binary sensor."""
with patch("homeassistant.components.lcn.PLATFORMS", [Platform.BINARY_SENSOR]):
await init_integration(hass, entry)
entity_setpoint1 = entity_registry.async_get(BINARY_SENSOR_LOCKREGULATOR1) await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
assert entity_setpoint1
assert entity_setpoint1.unique_id == f"{entry.entry_id}-m000007-r1varsetpoint"
assert entity_setpoint1.original_name == "Sensor_LockRegulator1"
entity_binsensor1 = entity_registry.async_get(BINARY_SENSOR_SENSOR1)
assert entity_binsensor1
assert entity_binsensor1.unique_id == f"{entry.entry_id}-m000007-binsensor1"
assert entity_binsensor1.original_name == "Binary_Sensor1"
entity_keylock = entity_registry.async_get(BINARY_SENSOR_KEYLOCK)
assert entity_keylock
assert entity_keylock.unique_id == f"{entry.entry_id}-m000007-a5"
assert entity_keylock.original_name == "Sensor_KeyLock"
async def test_pushed_lock_setpoint_status_change( async def test_pushed_lock_setpoint_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant,
entry: MockConfigEntry,
) -> None: ) -> None:
"""Test the lock setpoint sensor changes its state on status received.""" """Test the lock setpoint sensor changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
@ -86,9 +64,11 @@ async def test_pushed_lock_setpoint_status_change(
async def test_pushed_binsensor_status_change( async def test_pushed_binsensor_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the binary port sensor changes its state on status received.""" """Test the binary port sensor changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
states = [False] * 8 states = [False] * 8
@ -114,9 +94,11 @@ async def test_pushed_binsensor_status_change(
async def test_pushed_keylock_status_change( async def test_pushed_keylock_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the keylock sensor changes its state on status received.""" """Test the keylock sensor changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
states = [[False] * 8 for i in range(4)] states = [[False] * 8 for i in range(4)]
@ -141,8 +123,10 @@ async def test_pushed_keylock_status_change(
assert state.state == STATE_ON assert state.state == STATE_ON
async def test_unload_config_entry(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the binary sensor is removed when the config entry is unloaded.""" """Test the binary sensor is removed when the config entry is unloaded."""
await init_integration(hass, entry)
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
assert hass.states.get(BINARY_SENSOR_LOCKREGULATOR1).state == STATE_UNAVAILABLE assert hass.states.get(BINARY_SENSOR_LOCKREGULATOR1).state == STATE_UNAVAILABLE
assert hass.states.get(BINARY_SENSOR_SENSOR1).state == STATE_UNAVAILABLE assert hass.states.get(BINARY_SENSOR_SENSOR1).state == STATE_UNAVAILABLE

View File

@ -282,6 +282,6 @@ async def test_unload_config_entry(
"""Test the climate is removed when the config entry is unloaded.""" """Test the climate is removed when the config entry is unloaded."""
await init_integration(hass, entry) await init_integration(hass, entry)
await hass.config_entries.async_forward_entry_unload(entry, DOMAIN_CLIMATE) await hass.config_entries.async_unload(entry.entry_id)
state = hass.states.get("climate.climate1") state = hass.states.get("climate.climate1")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE

View File

@ -48,7 +48,7 @@ async def test_step_import(
"""Test for import step.""" """Test for import step."""
with ( with (
patch("pypck.connection.PchkConnectionManager.async_connect"), patch("homeassistant.components.lcn.PchkConnectionManager.async_connect"),
patch("homeassistant.components.lcn.async_setup", return_value=True), patch("homeassistant.components.lcn.async_setup", return_value=True),
patch("homeassistant.components.lcn.async_setup_entry", return_value=True), patch("homeassistant.components.lcn.async_setup_entry", return_value=True),
): ):
@ -76,7 +76,7 @@ async def test_step_import_existing_host(
mock_entry = MockConfigEntry(domain=DOMAIN, data=mock_data) mock_entry = MockConfigEntry(domain=DOMAIN, data=mock_data)
mock_entry.add_to_hass(hass) mock_entry.add_to_hass(hass)
# Initialize a config flow with different data but same host address # Initialize a config flow with different data but same host address
with patch("pypck.connection.PchkConnectionManager.async_connect"): with patch("homeassistant.components.lcn.PchkConnectionManager.async_connect"):
imported_data = IMPORT_DATA.copy() imported_data = IMPORT_DATA.copy()
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=imported_data DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=imported_data
@ -105,7 +105,8 @@ async def test_step_import_error(
) -> None: ) -> None:
"""Test for error in import is handled correctly.""" """Test for error in import is handled correctly."""
with patch( with patch(
"pypck.connection.PchkConnectionManager.async_connect", side_effect=error "homeassistant.components.lcn.PchkConnectionManager.async_connect",
side_effect=error,
): ):
data = IMPORT_DATA.copy() data = IMPORT_DATA.copy()
data.update({CONF_HOST: "pchk"}) data.update({CONF_HOST: "pchk"})
@ -132,7 +133,7 @@ async def test_show_form(hass: HomeAssistant) -> None:
async def test_step_user(hass: HomeAssistant) -> None: async def test_step_user(hass: HomeAssistant) -> None:
"""Test for user step.""" """Test for user step."""
with ( with (
patch("pypck.connection.PchkConnectionManager.async_connect"), patch("homeassistant.components.lcn.PchkConnectionManager.async_connect"),
patch("homeassistant.components.lcn.async_setup", return_value=True), patch("homeassistant.components.lcn.async_setup", return_value=True),
patch("homeassistant.components.lcn.async_setup_entry", return_value=True), patch("homeassistant.components.lcn.async_setup_entry", return_value=True),
): ):
@ -156,7 +157,7 @@ async def test_step_user_existing_host(
"""Test for user defined host already exists.""" """Test for user defined host already exists."""
entry.add_to_hass(hass) entry.add_to_hass(hass)
with patch("pypck.connection.PchkConnectionManager.async_connect"): with patch("homeassistant.components.lcn.PchkConnectionManager.async_connect"):
config_data = entry.data.copy() config_data = entry.data.copy()
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config_data DOMAIN, context={"source": config_entries.SOURCE_USER}, data=config_data
@ -179,7 +180,8 @@ async def test_step_user_error(
) -> None: ) -> None:
"""Test for error in user step is handled correctly.""" """Test for error in user step is handled correctly."""
with patch( with patch(
"pypck.connection.PchkConnectionManager.async_connect", side_effect=error "homeassistant.components.lcn.PchkConnectionManager.async_connect",
side_effect=error,
): ):
data = CONNECTION_DATA.copy() data = CONNECTION_DATA.copy()
data.update({CONF_HOST: "pchk"}) data.update({CONF_HOST: "pchk"})
@ -197,7 +199,7 @@ async def test_step_reconfigure(hass: HomeAssistant, entry: MockConfigEntry) ->
old_entry_data = entry.data.copy() old_entry_data = entry.data.copy()
with ( with (
patch("pypck.connection.PchkConnectionManager.async_connect"), patch("homeassistant.components.lcn.PchkConnectionManager.async_connect"),
patch("homeassistant.components.lcn.async_setup", return_value=True), patch("homeassistant.components.lcn.async_setup", return_value=True),
patch("homeassistant.components.lcn.async_setup_entry", return_value=True), patch("homeassistant.components.lcn.async_setup_entry", return_value=True),
): ):
@ -235,7 +237,8 @@ async def test_step_reconfigure_error(
"""Test for error in reconfigure step is handled correctly.""" """Test for error in reconfigure step is handled correctly."""
entry.add_to_hass(hass) entry.add_to_hass(hass)
with patch( with patch(
"pypck.connection.PchkConnectionManager.async_connect", side_effect=error "homeassistant.components.lcn.PchkConnectionManager.async_connect",
side_effect=error,
): ):
data = {**CONNECTION_DATA, CONF_HOST: "pchk"} data = {**CONNECTION_DATA, CONF_HOST: "pchk"}
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -256,8 +259,12 @@ async def test_validate_connection() -> None:
data = CONNECTION_DATA.copy() data = CONNECTION_DATA.copy()
with ( with (
patch("pypck.connection.PchkConnectionManager.async_connect") as async_connect, patch(
patch("pypck.connection.PchkConnectionManager.async_close") as async_close, "homeassistant.components.lcn.PchkConnectionManager.async_connect"
) as async_connect,
patch(
"homeassistant.components.lcn.PchkConnectionManager.async_close"
) as async_close,
): ):
result = await validate_connection(data=data) result = await validate_connection(data=data)

View File

@ -5,6 +5,7 @@ from unittest.mock import patch
from pypck.inputs import ModStatusOutput, ModStatusRelays from pypck.inputs import ModStatusOutput, ModStatusRelays
from pypck.lcn_addr import LcnAddr from pypck.lcn_addr import LcnAddr
from pypck.lcn_defs import MotorReverseTime, MotorStateModifier from pypck.lcn_defs import MotorReverseTime, MotorStateModifier
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.cover import DOMAIN as DOMAIN_COVER from homeassistant.components.cover import DOMAIN as DOMAIN_COVER
from homeassistant.components.lcn.helpers import get_device_connection from homeassistant.components.lcn.helpers import get_device_connection
@ -18,50 +19,39 @@ from homeassistant.const import (
STATE_OPEN, STATE_OPEN,
STATE_OPENING, STATE_OPENING,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
Platform,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import MockModuleConnection from .conftest import MockConfigEntry, MockModuleConnection, init_integration
from tests.common import snapshot_platform
COVER_OUTPUTS = "cover.cover_outputs" COVER_OUTPUTS = "cover.cover_outputs"
COVER_RELAYS = "cover.cover_relays" COVER_RELAYS = "cover.cover_relays"
async def test_setup_lcn_cover(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_setup_lcn_cover(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test the setup of cover.""" """Test the setup of cover."""
for entity_id in ( with patch("homeassistant.components.lcn.PLATFORMS", [Platform.COVER]):
COVER_OUTPUTS, await init_integration(hass, entry)
COVER_RELAYS,
): await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_OPEN
async def test_entity_attributes( async def test_outputs_open(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None:
"""Test the attributes of an entity."""
entity_outputs = entity_registry.async_get(COVER_OUTPUTS)
assert entity_outputs
assert entity_outputs.unique_id == f"{entry.entry_id}-m000007-outputs"
assert entity_outputs.original_name == "Cover_Outputs"
entity_relays = entity_registry.async_get(COVER_RELAYS)
assert entity_relays
assert entity_relays.unique_id == f"{entry.entry_id}-m000007-motor1"
assert entity_relays.original_name == "Cover_Relays"
@patch.object(MockModuleConnection, "control_motors_outputs")
async def test_outputs_open(
control_motors_outputs, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the outputs cover opens.""" """Test the outputs cover opens."""
await init_integration(hass, entry)
with patch.object(
MockModuleConnection, "control_motors_outputs"
) as control_motors_outputs:
state = hass.states.get(COVER_OUTPUTS) state = hass.states.get(COVER_OUTPUTS)
state.state = STATE_CLOSED state.state = STATE_CLOSED
@ -74,7 +64,7 @@ async def test_outputs_open(
{ATTR_ENTITY_ID: COVER_OUTPUTS}, {ATTR_ENTITY_ID: COVER_OUTPUTS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_outputs.assert_awaited_with( control_motors_outputs.assert_awaited_with(
MotorStateModifier.UP, MotorReverseTime.RT1200 MotorStateModifier.UP, MotorReverseTime.RT1200
) )
@ -93,7 +83,7 @@ async def test_outputs_open(
{ATTR_ENTITY_ID: COVER_OUTPUTS}, {ATTR_ENTITY_ID: COVER_OUTPUTS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_outputs.assert_awaited_with( control_motors_outputs.assert_awaited_with(
MotorStateModifier.UP, MotorReverseTime.RT1200 MotorStateModifier.UP, MotorReverseTime.RT1200
) )
@ -103,11 +93,13 @@ async def test_outputs_open(
assert state.state == STATE_OPENING assert state.state == STATE_OPENING
@patch.object(MockModuleConnection, "control_motors_outputs") async def test_outputs_close(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_outputs_close(
control_motors_outputs, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the outputs cover closes.""" """Test the outputs cover closes."""
await init_integration(hass, entry)
with patch.object(
MockModuleConnection, "control_motors_outputs"
) as control_motors_outputs:
state = hass.states.get(COVER_OUTPUTS) state = hass.states.get(COVER_OUTPUTS)
state.state = STATE_OPEN state.state = STATE_OPEN
@ -120,7 +112,7 @@ async def test_outputs_close(
{ATTR_ENTITY_ID: COVER_OUTPUTS}, {ATTR_ENTITY_ID: COVER_OUTPUTS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_outputs.assert_awaited_with( control_motors_outputs.assert_awaited_with(
MotorStateModifier.DOWN, MotorReverseTime.RT1200 MotorStateModifier.DOWN, MotorReverseTime.RT1200
) )
@ -139,7 +131,7 @@ async def test_outputs_close(
{ATTR_ENTITY_ID: COVER_OUTPUTS}, {ATTR_ENTITY_ID: COVER_OUTPUTS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_outputs.assert_awaited_with( control_motors_outputs.assert_awaited_with(
MotorStateModifier.DOWN, MotorReverseTime.RT1200 MotorStateModifier.DOWN, MotorReverseTime.RT1200
) )
@ -149,11 +141,13 @@ async def test_outputs_close(
assert state.state == STATE_CLOSING assert state.state == STATE_CLOSING
@patch.object(MockModuleConnection, "control_motors_outputs") async def test_outputs_stop(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_outputs_stop(
control_motors_outputs, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the outputs cover stops.""" """Test the outputs cover stops."""
await init_integration(hass, entry)
with patch.object(
MockModuleConnection, "control_motors_outputs"
) as control_motors_outputs:
state = hass.states.get(COVER_OUTPUTS) state = hass.states.get(COVER_OUTPUTS)
state.state = STATE_CLOSING state.state = STATE_CLOSING
@ -166,7 +160,7 @@ async def test_outputs_stop(
{ATTR_ENTITY_ID: COVER_OUTPUTS}, {ATTR_ENTITY_ID: COVER_OUTPUTS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_outputs.assert_awaited_with(MotorStateModifier.STOP) control_motors_outputs.assert_awaited_with(MotorStateModifier.STOP)
state = hass.states.get(COVER_OUTPUTS) state = hass.states.get(COVER_OUTPUTS)
@ -183,7 +177,7 @@ async def test_outputs_stop(
{ATTR_ENTITY_ID: COVER_OUTPUTS}, {ATTR_ENTITY_ID: COVER_OUTPUTS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_outputs.assert_awaited_with(MotorStateModifier.STOP) control_motors_outputs.assert_awaited_with(MotorStateModifier.STOP)
state = hass.states.get(COVER_OUTPUTS) state = hass.states.get(COVER_OUTPUTS)
@ -191,11 +185,13 @@ async def test_outputs_stop(
assert state.state not in (STATE_CLOSING, STATE_OPENING) assert state.state not in (STATE_CLOSING, STATE_OPENING)
@patch.object(MockModuleConnection, "control_motors_relays") async def test_relays_open(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_relays_open(
control_motors_relays, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the relays cover opens.""" """Test the relays cover opens."""
await init_integration(hass, entry)
with patch.object(
MockModuleConnection, "control_motors_relays"
) as control_motors_relays:
states = [MotorStateModifier.NOCHANGE] * 4 states = [MotorStateModifier.NOCHANGE] * 4
states[0] = MotorStateModifier.UP states[0] = MotorStateModifier.UP
@ -211,7 +207,7 @@ async def test_relays_open(
{ATTR_ENTITY_ID: COVER_RELAYS}, {ATTR_ENTITY_ID: COVER_RELAYS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_relays.assert_awaited_with(states) control_motors_relays.assert_awaited_with(states)
state = hass.states.get(COVER_RELAYS) state = hass.states.get(COVER_RELAYS)
@ -228,7 +224,7 @@ async def test_relays_open(
{ATTR_ENTITY_ID: COVER_RELAYS}, {ATTR_ENTITY_ID: COVER_RELAYS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_relays.assert_awaited_with(states) control_motors_relays.assert_awaited_with(states)
state = hass.states.get(COVER_RELAYS) state = hass.states.get(COVER_RELAYS)
@ -236,11 +232,13 @@ async def test_relays_open(
assert state.state == STATE_OPENING assert state.state == STATE_OPENING
@patch.object(MockModuleConnection, "control_motors_relays") async def test_relays_close(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_relays_close(
control_motors_relays, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the relays cover closes.""" """Test the relays cover closes."""
await init_integration(hass, entry)
with patch.object(
MockModuleConnection, "control_motors_relays"
) as control_motors_relays:
states = [MotorStateModifier.NOCHANGE] * 4 states = [MotorStateModifier.NOCHANGE] * 4
states[0] = MotorStateModifier.DOWN states[0] = MotorStateModifier.DOWN
@ -256,7 +254,7 @@ async def test_relays_close(
{ATTR_ENTITY_ID: COVER_RELAYS}, {ATTR_ENTITY_ID: COVER_RELAYS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_relays.assert_awaited_with(states) control_motors_relays.assert_awaited_with(states)
state = hass.states.get(COVER_RELAYS) state = hass.states.get(COVER_RELAYS)
@ -273,7 +271,7 @@ async def test_relays_close(
{ATTR_ENTITY_ID: COVER_RELAYS}, {ATTR_ENTITY_ID: COVER_RELAYS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_relays.assert_awaited_with(states) control_motors_relays.assert_awaited_with(states)
state = hass.states.get(COVER_RELAYS) state = hass.states.get(COVER_RELAYS)
@ -281,11 +279,13 @@ async def test_relays_close(
assert state.state == STATE_CLOSING assert state.state == STATE_CLOSING
@patch.object(MockModuleConnection, "control_motors_relays") async def test_relays_stop(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_relays_stop(
control_motors_relays, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the relays cover stops.""" """Test the relays cover stops."""
await init_integration(hass, entry)
with patch.object(
MockModuleConnection, "control_motors_relays"
) as control_motors_relays:
states = [MotorStateModifier.NOCHANGE] * 4 states = [MotorStateModifier.NOCHANGE] * 4
states[0] = MotorStateModifier.STOP states[0] = MotorStateModifier.STOP
@ -301,7 +301,7 @@ async def test_relays_stop(
{ATTR_ENTITY_ID: COVER_RELAYS}, {ATTR_ENTITY_ID: COVER_RELAYS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_relays.assert_awaited_with(states) control_motors_relays.assert_awaited_with(states)
state = hass.states.get(COVER_RELAYS) state = hass.states.get(COVER_RELAYS)
@ -318,7 +318,7 @@ async def test_relays_stop(
{ATTR_ENTITY_ID: COVER_RELAYS}, {ATTR_ENTITY_ID: COVER_RELAYS},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_motors_relays.assert_awaited_with(states) control_motors_relays.assert_awaited_with(states)
state = hass.states.get(COVER_RELAYS) state = hass.states.get(COVER_RELAYS)
@ -327,9 +327,11 @@ async def test_relays_stop(
async def test_pushed_outputs_status_change( async def test_pushed_outputs_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the outputs cover changes its state on status received.""" """Test the outputs cover changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
@ -365,9 +367,11 @@ async def test_pushed_outputs_status_change(
async def test_pushed_relays_status_change( async def test_pushed_relays_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the relays cover changes its state on status received.""" """Test the relays cover changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
states = [False] * 8 states = [False] * 8
@ -406,8 +410,10 @@ async def test_pushed_relays_status_change(
assert state.state == STATE_CLOSING assert state.state == STATE_CLOSING
async def test_unload_config_entry(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the cover is removed when the config entry is unloaded.""" """Test the cover is removed when the config entry is unloaded."""
await init_integration(hass, entry)
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
assert hass.states.get(COVER_OUTPUTS).state == STATE_UNAVAILABLE assert hass.states.get(COVER_OUTPUTS).state == STATE_UNAVAILABLE
assert hass.states.get(COVER_RELAYS).state == STATE_UNAVAILABLE assert hass.states.get(COVER_RELAYS).state == STATE_UNAVAILABLE

View File

@ -15,15 +15,17 @@ from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from .conftest import get_device from .conftest import MockConfigEntry, get_device, init_integration
from tests.common import async_get_device_automations from tests.common import async_get_device_automations
async def test_get_triggers_module_device( async def test_get_triggers_module_device(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test we get the expected triggers from a LCN module device.""" """Test we get the expected triggers from a LCN module device."""
await init_integration(hass, entry)
device = get_device(hass, entry, (0, 7, False)) device = get_device(hass, entry, (0, 7, False))
expected_triggers = [ expected_triggers = [
@ -50,9 +52,11 @@ async def test_get_triggers_module_device(
async def test_get_triggers_non_module_device( async def test_get_triggers_non_module_device(
hass: HomeAssistant, device_registry: dr.DeviceRegistry, entry, lcn_connection hass: HomeAssistant, device_registry: dr.DeviceRegistry, entry: MockConfigEntry
) -> None: ) -> None:
"""Test we get the expected triggers from a LCN non-module device.""" """Test we get the expected triggers from a LCN non-module device."""
await init_integration(hass, entry)
not_included_types = ("transmitter", "transponder", "fingerprint", "send_keys") not_included_types = ("transmitter", "transponder", "fingerprint", "send_keys")
host_device = device_registry.async_get_device( host_device = device_registry.async_get_device(
@ -72,9 +76,10 @@ async def test_get_triggers_non_module_device(
async def test_if_fires_on_transponder_event( async def test_if_fires_on_transponder_event(
hass: HomeAssistant, service_calls: list[ServiceCall], entry, lcn_connection hass: HomeAssistant, service_calls: list[ServiceCall], entry: MockConfigEntry
) -> None: ) -> None:
"""Test for transponder event triggers firing.""" """Test for transponder event triggers firing."""
lcn_connection = await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -119,9 +124,10 @@ async def test_if_fires_on_transponder_event(
async def test_if_fires_on_fingerprint_event( async def test_if_fires_on_fingerprint_event(
hass: HomeAssistant, service_calls: list[ServiceCall], entry, lcn_connection hass: HomeAssistant, service_calls: list[ServiceCall], entry: MockConfigEntry
) -> None: ) -> None:
"""Test for fingerprint event triggers firing.""" """Test for fingerprint event triggers firing."""
lcn_connection = await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -166,9 +172,10 @@ async def test_if_fires_on_fingerprint_event(
async def test_if_fires_on_codelock_event( async def test_if_fires_on_codelock_event(
hass: HomeAssistant, service_calls: list[ServiceCall], entry, lcn_connection hass: HomeAssistant, service_calls: list[ServiceCall], entry: MockConfigEntry
) -> None: ) -> None:
"""Test for codelock event triggers firing.""" """Test for codelock event triggers firing."""
lcn_connection = await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -213,9 +220,10 @@ async def test_if_fires_on_codelock_event(
async def test_if_fires_on_transmitter_event( async def test_if_fires_on_transmitter_event(
hass: HomeAssistant, service_calls: list[ServiceCall], entry, lcn_connection hass: HomeAssistant, service_calls: list[ServiceCall], entry: MockConfigEntry
) -> None: ) -> None:
"""Test for transmitter event triggers firing.""" """Test for transmitter event triggers firing."""
lcn_connection = await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -269,9 +277,10 @@ async def test_if_fires_on_transmitter_event(
async def test_if_fires_on_send_keys_event( async def test_if_fires_on_send_keys_event(
hass: HomeAssistant, service_calls: list[ServiceCall], entry, lcn_connection hass: HomeAssistant, service_calls: list[ServiceCall], entry: MockConfigEntry
) -> None: ) -> None:
"""Test for send_keys event triggers firing.""" """Test for send_keys event triggers firing."""
lcn_connection = await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -318,9 +327,10 @@ async def test_if_fires_on_send_keys_event(
async def test_get_transponder_trigger_capabilities( async def test_get_transponder_trigger_capabilities(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test we get the expected capabilities from a transponder device trigger.""" """Test we get the expected capabilities from a transponder device trigger."""
await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -341,9 +351,10 @@ async def test_get_transponder_trigger_capabilities(
async def test_get_fingerprint_trigger_capabilities( async def test_get_fingerprint_trigger_capabilities(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test we get the expected capabilities from a fingerprint device trigger.""" """Test we get the expected capabilities from a fingerprint device trigger."""
await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -364,9 +375,10 @@ async def test_get_fingerprint_trigger_capabilities(
async def test_get_transmitter_trigger_capabilities( async def test_get_transmitter_trigger_capabilities(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test we get the expected capabilities from a transmitter device trigger.""" """Test we get the expected capabilities from a transmitter device trigger."""
await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -397,9 +409,10 @@ async def test_get_transmitter_trigger_capabilities(
async def test_get_send_keys_trigger_capabilities( async def test_get_send_keys_trigger_capabilities(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test we get the expected capabilities from a send_keys device trigger.""" """Test we get the expected capabilities from a send_keys device trigger."""
await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)
@ -435,9 +448,10 @@ async def test_get_send_keys_trigger_capabilities(
async def test_unknown_trigger_capabilities( async def test_unknown_trigger_capabilities(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test we get empty capabilities if trigger is unknown.""" """Test we get empty capabilities if trigger is unknown."""
await init_integration(hass, entry)
address = (0, 7, False) address = (0, 7, False)
device = get_device(hass, entry, address) device = get_device(hass, entry, address)

View File

@ -3,10 +3,11 @@
from pypck.inputs import Input, ModSendKeysHost, ModStatusAccessControl from pypck.inputs import Input, ModSendKeysHost, ModStatusAccessControl
from pypck.lcn_addr import LcnAddr from pypck.lcn_addr import LcnAddr
from pypck.lcn_defs import AccessControlPeriphery, KeyAction, SendKeyCommand from pypck.lcn_defs import AccessControlPeriphery, KeyAction, SendKeyCommand
import pytest
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .conftest import MockConfigEntry, init_integration
from tests.common import async_capture_events from tests.common import async_capture_events
LCN_TRANSPONDER = "lcn_transponder" LCN_TRANSPONDER = "lcn_transponder"
@ -15,8 +16,11 @@ LCN_TRANSMITTER = "lcn_transmitter"
LCN_SEND_KEYS = "lcn_send_keys" LCN_SEND_KEYS = "lcn_send_keys"
async def test_fire_transponder_event(hass: HomeAssistant, lcn_connection) -> None: async def test_fire_transponder_event(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test the transponder event is fired.""" """Test the transponder event is fired."""
lcn_connection = await init_integration(hass, entry)
events = async_capture_events(hass, LCN_TRANSPONDER) events = async_capture_events(hass, LCN_TRANSPONDER)
inp = ModStatusAccessControl( inp = ModStatusAccessControl(
@ -33,8 +37,11 @@ async def test_fire_transponder_event(hass: HomeAssistant, lcn_connection) -> No
assert events[0].data["code"] == "aabbcc" assert events[0].data["code"] == "aabbcc"
async def test_fire_fingerprint_event(hass: HomeAssistant, lcn_connection) -> None: async def test_fire_fingerprint_event(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test the fingerprint event is fired.""" """Test the fingerprint event is fired."""
lcn_connection = await init_integration(hass, entry)
events = async_capture_events(hass, LCN_FINGERPRINT) events = async_capture_events(hass, LCN_FINGERPRINT)
inp = ModStatusAccessControl( inp = ModStatusAccessControl(
@ -51,8 +58,9 @@ async def test_fire_fingerprint_event(hass: HomeAssistant, lcn_connection) -> No
assert events[0].data["code"] == "aabbcc" assert events[0].data["code"] == "aabbcc"
async def test_fire_codelock_event(hass: HomeAssistant, lcn_connection) -> None: async def test_fire_codelock_event(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the codelock event is fired.""" """Test the codelock event is fired."""
lcn_connection = await init_integration(hass, entry)
events = async_capture_events(hass, "lcn_codelock") events = async_capture_events(hass, "lcn_codelock")
inp = ModStatusAccessControl( inp = ModStatusAccessControl(
@ -69,8 +77,11 @@ async def test_fire_codelock_event(hass: HomeAssistant, lcn_connection) -> None:
assert events[0].data["code"] == "aabbcc" assert events[0].data["code"] == "aabbcc"
async def test_fire_transmitter_event(hass: HomeAssistant, lcn_connection) -> None: async def test_fire_transmitter_event(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test the transmitter event is fired.""" """Test the transmitter event is fired."""
lcn_connection = await init_integration(hass, entry)
events = async_capture_events(hass, LCN_TRANSMITTER) events = async_capture_events(hass, LCN_TRANSMITTER)
inp = ModStatusAccessControl( inp = ModStatusAccessControl(
@ -93,8 +104,9 @@ async def test_fire_transmitter_event(hass: HomeAssistant, lcn_connection) -> No
assert events[0].data["action"] == "hit" assert events[0].data["action"] == "hit"
async def test_fire_sendkeys_event(hass: HomeAssistant, lcn_connection) -> None: async def test_fire_sendkeys_event(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the send_keys event is fired.""" """Test the send_keys event is fired."""
lcn_connection = await init_integration(hass, entry)
events = async_capture_events(hass, LCN_SEND_KEYS) events = async_capture_events(hass, LCN_SEND_KEYS)
inp = ModSendKeysHost( inp = ModSendKeysHost(
@ -122,9 +134,10 @@ async def test_fire_sendkeys_event(hass: HomeAssistant, lcn_connection) -> None:
async def test_dont_fire_on_non_module_input( async def test_dont_fire_on_non_module_input(
hass: HomeAssistant, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test for no event is fired if a non-module input is received.""" """Test for no event is fired if a non-module input is received."""
lcn_connection = await init_integration(hass, entry)
inp = Input() inp = Input()
for event_name in ( for event_name in (
@ -139,16 +152,16 @@ async def test_dont_fire_on_non_module_input(
assert len(events) == 0 assert len(events) == 0
# This tests needs to be adjusted to remove lingering tasks async def test_dont_fire_on_unknown_module(
@pytest.mark.parametrize("expected_lingering_tasks", [True]) hass: HomeAssistant, entry: MockConfigEntry
async def test_dont_fire_on_unknown_module(hass: HomeAssistant, lcn_connection) -> None: ) -> None:
"""Test for no event is fired if an input from an unknown module is received.""" """Test for no event is fired if an input from an unknown module is received."""
lcn_connection = await init_integration(hass, entry)
inp = ModStatusAccessControl( inp = ModStatusAccessControl(
LcnAddr(0, 10, False), # unknown module LcnAddr(0, 10, False), # unknown module
periphery=AccessControlPeriphery.FINGERPRINT, periphery=AccessControlPeriphery.FINGERPRINT,
code="aabbcc", code="aabbcc",
) )
events = async_capture_events(hass, LCN_FINGERPRINT) events = async_capture_events(hass, LCN_FINGERPRINT)
await lcn_connection.async_process_input(inp) await lcn_connection.async_process_input(inp)
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -2,11 +2,8 @@
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from pypck.connection import ( from pypck.connection import PchkAuthenticationError, PchkLicenseError
PchkAuthenticationError, import pytest
PchkConnectionManager,
PchkLicenseError,
)
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.lcn.const import DOMAIN from homeassistant.components.lcn.const import DOMAIN
@ -14,11 +11,18 @@ from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from .conftest import MockPchkConnectionManager, setup_component from .conftest import (
MockConfigEntry,
MockPchkConnectionManager,
init_integration,
setup_component,
)
async def test_async_setup_entry(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_async_setup_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test a successful setup entry and unload of entry.""" """Test a successful setup entry and unload of entry."""
await init_integration(hass, entry)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
@ -29,16 +33,16 @@ async def test_async_setup_entry(hass: HomeAssistant, entry, lcn_connection) ->
assert not hass.data.get(DOMAIN) assert not hass.data.get(DOMAIN)
async def test_async_setup_multiple_entries(hass: HomeAssistant, entry, entry2) -> None: async def test_async_setup_multiple_entries(
hass: HomeAssistant, entry: MockConfigEntry, entry2
) -> None:
"""Test a successful setup and unload of multiple entries.""" """Test a successful setup and unload of multiple entries."""
hass.http = Mock() hass.http = Mock()
with patch( with patch(
"homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager "homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager
): ):
for config_entry in (entry, entry2): for config_entry in (entry, entry2):
config_entry.add_to_hass(hass) await init_integration(hass, config_entry)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED assert config_entry.state is ConfigEntryState.LOADED
assert len(hass.config_entries.async_entries(DOMAIN)) == 2 assert len(hass.config_entries.async_entries(DOMAIN)) == 2
@ -56,7 +60,7 @@ async def test_async_setup_entry_update(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
entry, entry: MockConfigEntry,
) -> None: ) -> None:
"""Test a successful setup entry if entry with same id already exists.""" """Test a successful setup entry if entry with same id already exists."""
# setup first entry # setup first entry
@ -79,7 +83,10 @@ async def test_async_setup_entry_update(
assert dummy_device in device_registry.devices.values() assert dummy_device in device_registry.devices.values()
# setup new entry with same data via import step (should cleanup dummy device) # setup new entry with same data via import step (should cleanup dummy device)
with patch("pypck.connection.PchkConnectionManager", MockPchkConnectionManager): with patch(
"homeassistant.components.lcn.config_flow.validate_connection",
return_value=None,
):
await hass.config_entries.flow.async_init( await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=entry.data DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=entry.data
) )
@ -88,12 +95,16 @@ async def test_async_setup_entry_update(
assert dummy_entity not in entity_registry.entities.values() assert dummy_entity not in entity_registry.entities.values()
@pytest.mark.parametrize(
"exception", [PchkAuthenticationError, PchkLicenseError, TimeoutError]
)
async def test_async_setup_entry_raises_authentication_error( async def test_async_setup_entry_raises_authentication_error(
hass: HomeAssistant, entry hass: HomeAssistant, entry: MockConfigEntry, exception: Exception
) -> None: ) -> None:
"""Test that an authentication error is handled properly.""" """Test that an authentication error is handled properly."""
with patch.object( with patch(
PchkConnectionManager, "async_connect", side_effect=PchkAuthenticationError "homeassistant.components.lcn.PchkConnectionManager.async_connect",
side_effect=exception,
): ):
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
@ -102,36 +113,13 @@ async def test_async_setup_entry_raises_authentication_error(
assert entry.state is ConfigEntryState.SETUP_ERROR assert entry.state is ConfigEntryState.SETUP_ERROR
async def test_async_setup_entry_raises_license_error(
hass: HomeAssistant, entry
) -> None:
"""Test that an authentication error is handled properly."""
with patch.object(
PchkConnectionManager, "async_connect", side_effect=PchkLicenseError
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.SETUP_ERROR
async def test_async_setup_entry_raises_timeout_error(
hass: HomeAssistant, entry
) -> None:
"""Test that an authentication error is handled properly."""
with patch.object(PchkConnectionManager, "async_connect", side_effect=TimeoutError):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.SETUP_ERROR
async def test_async_setup_from_configuration_yaml(hass: HomeAssistant) -> None: async def test_async_setup_from_configuration_yaml(hass: HomeAssistant) -> None:
"""Test a successful setup using data from configuration.yaml.""" """Test a successful setup using data from configuration.yaml."""
with ( with (
patch("pypck.connection.PchkConnectionManager", MockPchkConnectionManager), patch(
"homeassistant.components.lcn.config_flow.validate_connection",
return_value=None,
),
patch("homeassistant.components.lcn.async_setup_entry") as async_setup_entry, patch("homeassistant.components.lcn.async_setup_entry") as async_setup_entry,
): ):
await setup_component(hass) await setup_component(hass)

View File

@ -5,80 +5,53 @@ from unittest.mock import patch
from pypck.inputs import ModStatusOutput, ModStatusRelays from pypck.inputs import ModStatusOutput, ModStatusRelays
from pypck.lcn_addr import LcnAddr from pypck.lcn_addr import LcnAddr
from pypck.lcn_defs import RelayStateModifier from pypck.lcn_defs import RelayStateModifier
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.lcn.helpers import get_device_connection from homeassistant.components.lcn.helpers import get_device_connection
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_SUPPORTED_COLOR_MODES,
ATTR_TRANSITION, ATTR_TRANSITION,
DOMAIN as DOMAIN_LIGHT, DOMAIN as DOMAIN_LIGHT,
ColorMode,
LightEntityFeature,
) )
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
Platform,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import MockModuleConnection from .conftest import MockConfigEntry, MockModuleConnection, init_integration
from tests.common import snapshot_platform
LIGHT_OUTPUT1 = "light.light_output1" LIGHT_OUTPUT1 = "light.light_output1"
LIGHT_OUTPUT2 = "light.light_output2" LIGHT_OUTPUT2 = "light.light_output2"
LIGHT_RELAY1 = "light.light_relay1" LIGHT_RELAY1 = "light.light_relay1"
async def test_setup_lcn_light(hass: HomeAssistant, lcn_connection) -> None: async def test_setup_lcn_light(
"""Test the setup of light.""" hass: HomeAssistant,
for entity_id in ( entity_registry: er.EntityRegistry,
LIGHT_OUTPUT1, entry: MockConfigEntry,
LIGHT_OUTPUT2, snapshot: SnapshotAssertion,
LIGHT_RELAY1,
):
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_OFF
async def test_entity_state(hass: HomeAssistant, lcn_connection) -> None:
"""Test state of entity."""
state = hass.states.get(LIGHT_OUTPUT1)
assert state
assert state.attributes[ATTR_SUPPORTED_FEATURES] == LightEntityFeature.TRANSITION
assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.BRIGHTNESS]
state = hass.states.get(LIGHT_OUTPUT2)
assert state
assert state.attributes[ATTR_SUPPORTED_FEATURES] == LightEntityFeature.TRANSITION
assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.ONOFF]
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None: ) -> None:
"""Test the attributes of an entity.""" """Test the setup of light."""
entity_output = entity_registry.async_get(LIGHT_OUTPUT1) with patch("homeassistant.components.lcn.PLATFORMS", [Platform.LIGHT]):
await init_integration(hass, entry)
assert entity_output await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
assert entity_output.unique_id == f"{entry.entry_id}-m000007-output1"
assert entity_output.original_name == "Light_Output1"
entity_relay = entity_registry.async_get(LIGHT_RELAY1)
assert entity_relay
assert entity_relay.unique_id == f"{entry.entry_id}-m000007-relay1"
assert entity_relay.original_name == "Light_Relay1"
@patch.object(MockModuleConnection, "dim_output") async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_output_turn_on(dim_output, hass: HomeAssistant, lcn_connection) -> None:
"""Test the output light turns on.""" """Test the output light turns on."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "dim_output") as dim_output:
# command failed # command failed
dim_output.return_value = False dim_output.return_value = False
@ -88,7 +61,7 @@ async def test_output_turn_on(dim_output, hass: HomeAssistant, lcn_connection) -
{ATTR_ENTITY_ID: LIGHT_OUTPUT1}, {ATTR_ENTITY_ID: LIGHT_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 100, 9) dim_output.assert_awaited_with(0, 100, 9)
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
@ -105,7 +78,7 @@ async def test_output_turn_on(dim_output, hass: HomeAssistant, lcn_connection) -
{ATTR_ENTITY_ID: LIGHT_OUTPUT1}, {ATTR_ENTITY_ID: LIGHT_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 100, 9) dim_output.assert_awaited_with(0, 100, 9)
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
@ -113,11 +86,13 @@ async def test_output_turn_on(dim_output, hass: HomeAssistant, lcn_connection) -
assert state.state == STATE_ON assert state.state == STATE_ON
@patch.object(MockModuleConnection, "dim_output")
async def test_output_turn_on_with_attributes( async def test_output_turn_on_with_attributes(
dim_output, hass: HomeAssistant, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the output light turns on.""" """Test the output light turns on."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "dim_output") as dim_output:
dim_output.return_value = True dim_output.return_value = True
await hass.services.async_call( await hass.services.async_call(
@ -130,7 +105,7 @@ async def test_output_turn_on_with_attributes(
}, },
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 19, 6) dim_output.assert_awaited_with(0, 19, 6)
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
@ -138,9 +113,11 @@ async def test_output_turn_on_with_attributes(
assert state.state == STATE_ON assert state.state == STATE_ON
@patch.object(MockModuleConnection, "dim_output") async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_output_turn_off(dim_output, hass: HomeAssistant, lcn_connection) -> None:
"""Test the output light turns off.""" """Test the output light turns off."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "dim_output") as dim_output:
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
state.state = STATE_ON state.state = STATE_ON
@ -153,7 +130,7 @@ async def test_output_turn_off(dim_output, hass: HomeAssistant, lcn_connection)
{ATTR_ENTITY_ID: LIGHT_OUTPUT1}, {ATTR_ENTITY_ID: LIGHT_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 0, 9) dim_output.assert_awaited_with(0, 0, 9)
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
@ -170,7 +147,7 @@ async def test_output_turn_off(dim_output, hass: HomeAssistant, lcn_connection)
{ATTR_ENTITY_ID: LIGHT_OUTPUT1}, {ATTR_ENTITY_ID: LIGHT_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 0, 9) dim_output.assert_awaited_with(0, 0, 9)
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
@ -178,11 +155,13 @@ async def test_output_turn_off(dim_output, hass: HomeAssistant, lcn_connection)
assert state.state == STATE_OFF assert state.state == STATE_OFF
@patch.object(MockModuleConnection, "dim_output")
async def test_output_turn_off_with_attributes( async def test_output_turn_off_with_attributes(
dim_output, hass: HomeAssistant, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the output light turns off.""" """Test the output light turns off."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "dim_output") as dim_output:
dim_output.return_value = True dim_output.return_value = True
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
@ -197,7 +176,7 @@ async def test_output_turn_off_with_attributes(
}, },
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 0, 6) dim_output.assert_awaited_with(0, 0, 6)
state = hass.states.get(LIGHT_OUTPUT1) state = hass.states.get(LIGHT_OUTPUT1)
@ -205,11 +184,11 @@ async def test_output_turn_off_with_attributes(
assert state.state == STATE_OFF assert state.state == STATE_OFF
@patch.object(MockModuleConnection, "control_relays") async def test_relay_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_relay_turn_on(
control_relays, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the relay light turns on.""" """Test the relay light turns on."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "control_relays") as control_relays:
states = [RelayStateModifier.NOCHANGE] * 8 states = [RelayStateModifier.NOCHANGE] * 8
states[0] = RelayStateModifier.ON states[0] = RelayStateModifier.ON
@ -222,7 +201,7 @@ async def test_relay_turn_on(
{ATTR_ENTITY_ID: LIGHT_RELAY1}, {ATTR_ENTITY_ID: LIGHT_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(LIGHT_RELAY1) state = hass.states.get(LIGHT_RELAY1)
@ -239,7 +218,7 @@ async def test_relay_turn_on(
{ATTR_ENTITY_ID: LIGHT_RELAY1}, {ATTR_ENTITY_ID: LIGHT_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(LIGHT_RELAY1) state = hass.states.get(LIGHT_RELAY1)
@ -247,11 +226,11 @@ async def test_relay_turn_on(
assert state.state == STATE_ON assert state.state == STATE_ON
@patch.object(MockModuleConnection, "control_relays") async def test_relay_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_relay_turn_off(
control_relays, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the relay light turns off.""" """Test the relay light turns off."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "control_relays") as control_relays:
states = [RelayStateModifier.NOCHANGE] * 8 states = [RelayStateModifier.NOCHANGE] * 8
states[0] = RelayStateModifier.OFF states[0] = RelayStateModifier.OFF
@ -267,7 +246,7 @@ async def test_relay_turn_off(
{ATTR_ENTITY_ID: LIGHT_RELAY1}, {ATTR_ENTITY_ID: LIGHT_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(LIGHT_RELAY1) state = hass.states.get(LIGHT_RELAY1)
@ -284,7 +263,7 @@ async def test_relay_turn_off(
{ATTR_ENTITY_ID: LIGHT_RELAY1}, {ATTR_ENTITY_ID: LIGHT_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(LIGHT_RELAY1) state = hass.states.get(LIGHT_RELAY1)
@ -293,9 +272,11 @@ async def test_relay_turn_off(
async def test_pushed_output_status_change( async def test_pushed_output_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the output light changes its state on status received.""" """Test the output light changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
@ -320,9 +301,11 @@ async def test_pushed_output_status_change(
async def test_pushed_relay_status_change( async def test_pushed_relay_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the relay light changes its state on status received.""" """Test the relay light changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
states = [False] * 8 states = [False] * 8
@ -348,7 +331,9 @@ async def test_pushed_relay_status_change(
assert state.state == STATE_OFF assert state.state == STATE_OFF
async def test_unload_config_entry(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the light is removed when the config entry is unloaded.""" """Test the light is removed when the config entry is unloaded."""
await init_integration(hass, entry)
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
assert hass.states.get(LIGHT_OUTPUT1).state == STATE_UNAVAILABLE assert hass.states.get(LIGHT_OUTPUT1).state == STATE_UNAVAILABLE

View File

@ -58,7 +58,7 @@ async def test_scene_activate(
async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None: async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the scene is removed when the config entry is unloaded.""" """Test the scene is removed when the config entry is unloaded."""
await init_integration(hass, entry) await init_integration(hass, entry)
await hass.config_entries.async_forward_entry_unload(entry, DOMAIN_SCENE)
await hass.config_entries.async_unload(entry.entry_id)
state = hass.states.get("scene.romantic") state = hass.states.get("scene.romantic")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE

View File

@ -1,85 +1,46 @@
"""Test for the LCN sensor platform.""" """Test for the LCN sensor platform."""
from unittest.mock import patch
from pypck.inputs import ModStatusLedsAndLogicOps, ModStatusVar from pypck.inputs import ModStatusLedsAndLogicOps, ModStatusVar
from pypck.lcn_addr import LcnAddr from pypck.lcn_addr import LcnAddr
from pypck.lcn_defs import LedStatus, LogicOpStatus, Var, VarValue from pypck.lcn_defs import LedStatus, LogicOpStatus, Var, VarValue
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.lcn.helpers import get_device_connection from homeassistant.components.lcn.helpers import get_device_connection
from homeassistant.const import ( from homeassistant.const import STATE_UNAVAILABLE, Platform
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import MockConfigEntry, init_integration
from tests.common import snapshot_platform
SENSOR_VAR1 = "sensor.sensor_var1" SENSOR_VAR1 = "sensor.sensor_var1"
SENSOR_SETPOINT1 = "sensor.sensor_setpoint1" SENSOR_SETPOINT1 = "sensor.sensor_setpoint1"
SENSOR_LED6 = "sensor.sensor_led6" SENSOR_LED6 = "sensor.sensor_led6"
SENSOR_LOGICOP1 = "sensor.sensor_logicop1" SENSOR_LOGICOP1 = "sensor.sensor_logicop1"
async def test_setup_lcn_sensor(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_setup_lcn_sensor(
"""Test the setup of sensor.""" hass: HomeAssistant,
for entity_id in ( entity_registry: er.EntityRegistry,
SENSOR_VAR1, entry: MockConfigEntry,
SENSOR_SETPOINT1, snapshot: SnapshotAssertion,
SENSOR_LED6,
SENSOR_LOGICOP1,
):
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNKNOWN
async def test_entity_state(hass: HomeAssistant, lcn_connection) -> None:
"""Test state of entity."""
state = hass.states.get(SENSOR_VAR1)
assert state
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
state = hass.states.get(SENSOR_SETPOINT1)
assert state
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTemperature.CELSIUS
state = hass.states.get(SENSOR_LED6)
assert state
state = hass.states.get(SENSOR_LOGICOP1)
assert state
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None: ) -> None:
"""Test the attributes of an entity.""" """Test the setup of sensor."""
with patch("homeassistant.components.lcn.PLATFORMS", [Platform.SENSOR]):
await init_integration(hass, entry)
entity_var1 = entity_registry.async_get(SENSOR_VAR1) await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
assert entity_var1
assert entity_var1.unique_id == f"{entry.entry_id}-m000007-var1"
assert entity_var1.original_name == "Sensor_Var1"
entity_r1varsetpoint = entity_registry.async_get(SENSOR_SETPOINT1)
assert entity_r1varsetpoint
assert entity_r1varsetpoint.unique_id == f"{entry.entry_id}-m000007-r1varsetpoint"
assert entity_r1varsetpoint.original_name == "Sensor_Setpoint1"
entity_led6 = entity_registry.async_get(SENSOR_LED6)
assert entity_led6
assert entity_led6.unique_id == f"{entry.entry_id}-m000007-led6"
assert entity_led6.original_name == "Sensor_Led6"
entity_logicop1 = entity_registry.async_get(SENSOR_LOGICOP1)
assert entity_logicop1
assert entity_logicop1.unique_id == f"{entry.entry_id}-m000007-logicop1"
assert entity_logicop1.original_name == "Sensor_LogicOp1"
async def test_pushed_variable_status_change( async def test_pushed_variable_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the variable sensor changes its state on status received.""" """Test the variable sensor changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
@ -103,9 +64,11 @@ async def test_pushed_variable_status_change(
async def test_pushed_ledlogicop_status_change( async def test_pushed_ledlogicop_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the led and logicop sensor changes its state on status received.""" """Test the led and logicop sensor changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
@ -129,8 +92,10 @@ async def test_pushed_ledlogicop_status_change(
assert state.state == "all" assert state.state == "all"
async def test_unload_config_entry(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the sensor is removed when the config entry is unloaded.""" """Test the sensor is removed when the config entry is unloaded."""
await init_integration(hass, entry)
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
assert hass.states.get(SENSOR_VAR1).state == STATE_UNAVAILABLE assert hass.states.get(SENSOR_VAR1).state == STATE_UNAVAILABLE
assert hass.states.get(SENSOR_SETPOINT1).state == STATE_UNAVAILABLE assert hass.states.get(SENSOR_SETPOINT1).state == STATE_UNAVAILABLE

View File

@ -2,8 +2,8 @@
from unittest.mock import patch from unittest.mock import patch
import pypck
import pytest import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.lcn import DOMAIN from homeassistant.components.lcn import DOMAIN
from homeassistant.components.lcn.const import ( from homeassistant.components.lcn.const import (
@ -41,9 +41,7 @@ from .conftest import (
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_output_abs( async def test_service_output_abs(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test output_abs service.""" """Test output_abs service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -61,13 +59,11 @@ async def test_service_output_abs(
blocking=True, blocking=True,
) )
assert dim_output.await_args.args == snapshot() dim_output.assert_awaited_with(0, 100, 9)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_output_rel( async def test_service_output_rel(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test output_rel service.""" """Test output_rel service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -84,12 +80,12 @@ async def test_service_output_rel(
blocking=True, blocking=True,
) )
assert rel_output.await_args.args == snapshot() rel_output.assert_awaited_with(0, 25)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_output_toggle( async def test_service_output_toggle(
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test output_toggle service.""" """Test output_toggle service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
@ -107,13 +103,11 @@ async def test_service_output_toggle(
blocking=True, blocking=True,
) )
assert toggle_output.await_args.args == snapshot() toggle_output.assert_awaited_with(0, 9)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_relays( async def test_service_relays(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test relays service.""" """Test relays service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -126,13 +120,14 @@ async def test_service_relays(
blocking=True, blocking=True,
) )
assert control_relays.await_args.args == snapshot() states = ["OFF", "OFF", "ON", "ON", "TOGGLE", "TOGGLE", "NOCHANGE", "NOCHANGE"]
relay_states = [pypck.lcn_defs.RelayStateModifier[state] for state in states]
control_relays.assert_awaited_with(relay_states)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_led( async def test_service_led(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test led service.""" """Test led service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -145,13 +140,14 @@ async def test_service_led(
blocking=True, blocking=True,
) )
assert control_led.await_args.args == snapshot() led = pypck.lcn_defs.LedPort["LED6"]
led_state = pypck.lcn_defs.LedStatus["BLINK"]
control_led.assert_awaited_with(led, led_state)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_var_abs( async def test_service_var_abs(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test var_abs service.""" """Test var_abs service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -169,13 +165,13 @@ async def test_service_var_abs(
blocking=True, blocking=True,
) )
assert var_abs.await_args.args == snapshot() var_abs.assert_awaited_with(
pypck.lcn_defs.Var["VAR1"], 75, pypck.lcn_defs.VarUnit.parse("%")
)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_var_rel( async def test_service_var_rel(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test var_rel service.""" """Test var_rel service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -194,13 +190,16 @@ async def test_service_var_rel(
blocking=True, blocking=True,
) )
assert var_rel.await_args.args == snapshot() var_rel.assert_awaited_with(
pypck.lcn_defs.Var["VAR1"],
10,
pypck.lcn_defs.VarUnit.parse("%"),
pypck.lcn_defs.RelVarRef["CURRENT"],
)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_var_reset( async def test_service_var_reset(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test var_reset service.""" """Test var_reset service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -213,12 +212,12 @@ async def test_service_var_reset(
blocking=True, blocking=True,
) )
assert var_reset.await_args.args == snapshot() var_reset.assert_awaited_with(pypck.lcn_defs.Var["VAR1"])
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_lock_regulator( async def test_service_lock_regulator(
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lock_regulator service.""" """Test lock_regulator service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
@ -236,13 +235,11 @@ async def test_service_lock_regulator(
blocking=True, blocking=True,
) )
assert lock_regulator.await_args.args == snapshot() lock_regulator.assert_awaited_with(0, True)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_send_keys( async def test_service_send_keys(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test send_keys service.""" """Test send_keys service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -260,12 +257,12 @@ async def test_service_send_keys(
keys[0][4] = True keys[0][4] = True
keys[3][7] = True keys[3][7] = True
assert send_keys.await_args.args == snapshot() send_keys.assert_awaited_with(keys, pypck.lcn_defs.SendKeyCommand["HIT"])
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_send_keys_hit_deferred( async def test_service_send_keys_hit_deferred(
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test send_keys (hit_deferred) service.""" """Test send_keys (hit_deferred) service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
@ -292,7 +289,9 @@ async def test_service_send_keys_hit_deferred(
blocking=True, blocking=True,
) )
assert send_keys_hit_deferred.await_args.args == snapshot() send_keys_hit_deferred.assert_awaited_with(
keys, 5, pypck.lcn_defs.TimeUnit.parse("S")
)
# wrong key action # wrong key action
with ( with (
@ -316,9 +315,7 @@ async def test_service_send_keys_hit_deferred(
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_lock_keys( async def test_service_lock_keys(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test lock_keys service.""" """Test lock_keys service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -331,12 +328,15 @@ async def test_service_lock_keys(
blocking=True, blocking=True,
) )
assert lock_keys.await_args.args == snapshot() states = ["OFF", "OFF", "ON", "ON", "TOGGLE", "TOGGLE", "NOCHANGE", "NOCHANGE"]
lock_states = [pypck.lcn_defs.KeyLockStateModifier[state] for state in states]
lock_keys.assert_awaited_with(0, lock_states)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_lock_keys_tab_a_temporary( async def test_service_lock_keys_tab_a_temporary(
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lock_keys (tab_a_temporary) service.""" """Test lock_keys (tab_a_temporary) service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
@ -358,7 +358,12 @@ async def test_service_lock_keys_tab_a_temporary(
blocking=True, blocking=True,
) )
assert lock_keys_tab_a_temporary.await_args.args == snapshot() states = ["OFF", "OFF", "ON", "ON", "TOGGLE", "TOGGLE", "NOCHANGE", "NOCHANGE"]
lock_states = [pypck.lcn_defs.KeyLockStateModifier[state] for state in states]
lock_keys_tab_a_temporary.assert_awaited_with(
10, pypck.lcn_defs.TimeUnit.parse("S"), lock_states
)
# wrong table # wrong table
with ( with (
@ -382,9 +387,7 @@ async def test_service_lock_keys_tab_a_temporary(
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_dyn_text( async def test_service_dyn_text(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test dyn_text service.""" """Test dyn_text service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -397,13 +400,11 @@ async def test_service_dyn_text(
blocking=True, blocking=True,
) )
assert dyn_text.await_args.args == snapshot() dyn_text.assert_awaited_with(0, "text in row 1")
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_service_pck( async def test_service_pck(hass: HomeAssistant, entry: MockConfigEntry) -> None:
hass: HomeAssistant, entry: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test pck service.""" """Test pck service."""
await async_setup_component(hass, "persistent_notification", {}) await async_setup_component(hass, "persistent_notification", {})
await init_integration(hass, entry) await init_integration(hass, entry)
@ -416,7 +417,7 @@ async def test_service_pck(
blocking=True, blocking=True,
) )
assert pck.await_args.args == snapshot() pck.assert_awaited_with("PIN4")
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager) @patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)

View File

@ -5,6 +5,7 @@ from unittest.mock import patch
from pypck.inputs import ModStatusOutput, ModStatusRelays from pypck.inputs import ModStatusOutput, ModStatusRelays
from pypck.lcn_addr import LcnAddr from pypck.lcn_addr import LcnAddr
from pypck.lcn_defs import RelayStateModifier from pypck.lcn_defs import RelayStateModifier
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.lcn.helpers import get_device_connection from homeassistant.components.lcn.helpers import get_device_connection
from homeassistant.components.switch import DOMAIN as DOMAIN_SWITCH from homeassistant.components.switch import DOMAIN as DOMAIN_SWITCH
@ -15,11 +16,14 @@ from homeassistant.const import (
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
Platform,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import MockModuleConnection from .conftest import MockConfigEntry, MockModuleConnection, init_integration
from tests.common import snapshot_platform
SWITCH_OUTPUT1 = "switch.switch_output1" SWITCH_OUTPUT1 = "switch.switch_output1"
SWITCH_OUTPUT2 = "switch.switch_output2" SWITCH_OUTPUT2 = "switch.switch_output2"
@ -27,40 +31,24 @@ SWITCH_RELAY1 = "switch.switch_relay1"
SWITCH_RELAY2 = "switch.switch_relay2" SWITCH_RELAY2 = "switch.switch_relay2"
async def test_setup_lcn_switch(hass: HomeAssistant, lcn_connection) -> None: async def test_setup_lcn_switch(
"""Test the setup of switch.""" hass: HomeAssistant,
for entity_id in ( entity_registry: er.EntityRegistry,
SWITCH_OUTPUT1, entry: MockConfigEntry,
SWITCH_OUTPUT2, snapshot: SnapshotAssertion,
SWITCH_RELAY1,
SWITCH_RELAY2,
):
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_OFF
async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, lcn_connection
) -> None: ) -> None:
"""Test the attributes of an entity.""" """Test the setup of switch."""
with patch("homeassistant.components.lcn.PLATFORMS", [Platform.SWITCH]):
await init_integration(hass, entry)
entity_output = entity_registry.async_get(SWITCH_OUTPUT1) await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
assert entity_output
assert entity_output.unique_id == f"{entry.entry_id}-m000007-output1"
assert entity_output.original_name == "Switch_Output1"
entity_relay = entity_registry.async_get(SWITCH_RELAY1)
assert entity_relay
assert entity_relay.unique_id == f"{entry.entry_id}-m000007-relay1"
assert entity_relay.original_name == "Switch_Relay1"
@patch.object(MockModuleConnection, "dim_output") async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_output_turn_on(dim_output, hass: HomeAssistant, lcn_connection) -> None:
"""Test the output switch turns on.""" """Test the output switch turns on."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "dim_output") as dim_output:
# command failed # command failed
dim_output.return_value = False dim_output.return_value = False
@ -70,7 +58,7 @@ async def test_output_turn_on(dim_output, hass: HomeAssistant, lcn_connection) -
{ATTR_ENTITY_ID: SWITCH_OUTPUT1}, {ATTR_ENTITY_ID: SWITCH_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 100, 0) dim_output.assert_awaited_with(0, 100, 0)
state = hass.states.get(SWITCH_OUTPUT1) state = hass.states.get(SWITCH_OUTPUT1)
@ -86,16 +74,18 @@ async def test_output_turn_on(dim_output, hass: HomeAssistant, lcn_connection) -
{ATTR_ENTITY_ID: SWITCH_OUTPUT1}, {ATTR_ENTITY_ID: SWITCH_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 100, 0) dim_output.assert_awaited_with(0, 100, 0)
state = hass.states.get(SWITCH_OUTPUT1) state = hass.states.get(SWITCH_OUTPUT1)
assert state.state == STATE_ON assert state.state == STATE_ON
@patch.object(MockModuleConnection, "dim_output") async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_output_turn_off(dim_output, hass: HomeAssistant, lcn_connection) -> None:
"""Test the output switch turns off.""" """Test the output switch turns off."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "dim_output") as dim_output:
state = hass.states.get(SWITCH_OUTPUT1) state = hass.states.get(SWITCH_OUTPUT1)
state.state = STATE_ON state.state = STATE_ON
@ -108,7 +98,7 @@ async def test_output_turn_off(dim_output, hass: HomeAssistant, lcn_connection)
{ATTR_ENTITY_ID: SWITCH_OUTPUT1}, {ATTR_ENTITY_ID: SWITCH_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 0, 0) dim_output.assert_awaited_with(0, 0, 0)
state = hass.states.get(SWITCH_OUTPUT1) state = hass.states.get(SWITCH_OUTPUT1)
@ -124,18 +114,18 @@ async def test_output_turn_off(dim_output, hass: HomeAssistant, lcn_connection)
{ATTR_ENTITY_ID: SWITCH_OUTPUT1}, {ATTR_ENTITY_ID: SWITCH_OUTPUT1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
dim_output.assert_awaited_with(0, 0, 0) dim_output.assert_awaited_with(0, 0, 0)
state = hass.states.get(SWITCH_OUTPUT1) state = hass.states.get(SWITCH_OUTPUT1)
assert state.state == STATE_OFF assert state.state == STATE_OFF
@patch.object(MockModuleConnection, "control_relays") async def test_relay_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_relay_turn_on(
control_relays, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the relay switch turns on.""" """Test the relay switch turns on."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "control_relays") as control_relays:
states = [RelayStateModifier.NOCHANGE] * 8 states = [RelayStateModifier.NOCHANGE] * 8
states[0] = RelayStateModifier.ON states[0] = RelayStateModifier.ON
@ -148,7 +138,7 @@ async def test_relay_turn_on(
{ATTR_ENTITY_ID: SWITCH_RELAY1}, {ATTR_ENTITY_ID: SWITCH_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(SWITCH_RELAY1) state = hass.states.get(SWITCH_RELAY1)
@ -164,18 +154,18 @@ async def test_relay_turn_on(
{ATTR_ENTITY_ID: SWITCH_RELAY1}, {ATTR_ENTITY_ID: SWITCH_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(SWITCH_RELAY1) state = hass.states.get(SWITCH_RELAY1)
assert state.state == STATE_ON assert state.state == STATE_ON
@patch.object(MockModuleConnection, "control_relays") async def test_relay_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> None:
async def test_relay_turn_off(
control_relays, hass: HomeAssistant, lcn_connection
) -> None:
"""Test the relay switch turns off.""" """Test the relay switch turns off."""
await init_integration(hass, entry)
with patch.object(MockModuleConnection, "control_relays") as control_relays:
states = [RelayStateModifier.NOCHANGE] * 8 states = [RelayStateModifier.NOCHANGE] * 8
states[0] = RelayStateModifier.OFF states[0] = RelayStateModifier.OFF
@ -191,7 +181,7 @@ async def test_relay_turn_off(
{ATTR_ENTITY_ID: SWITCH_RELAY1}, {ATTR_ENTITY_ID: SWITCH_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(SWITCH_RELAY1) state = hass.states.get(SWITCH_RELAY1)
@ -207,7 +197,7 @@ async def test_relay_turn_off(
{ATTR_ENTITY_ID: SWITCH_RELAY1}, {ATTR_ENTITY_ID: SWITCH_RELAY1},
blocking=True, blocking=True,
) )
await hass.async_block_till_done()
control_relays.assert_awaited_with(states) control_relays.assert_awaited_with(states)
state = hass.states.get(SWITCH_RELAY1) state = hass.states.get(SWITCH_RELAY1)
@ -215,9 +205,11 @@ async def test_relay_turn_off(
async def test_pushed_output_status_change( async def test_pushed_output_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the output switch changes its state on status received.""" """Test the output switch changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
@ -239,9 +231,11 @@ async def test_pushed_output_status_change(
async def test_pushed_relay_status_change( async def test_pushed_relay_status_change(
hass: HomeAssistant, entry, lcn_connection hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test the relay switch changes its state on status received.""" """Test the relay switch changes its state on status received."""
await init_integration(hass, entry)
device_connection = get_device_connection(hass, (0, 7, False), entry) device_connection = get_device_connection(hass, (0, 7, False), entry)
address = LcnAddr(0, 7, False) address = LcnAddr(0, 7, False)
states = [False] * 8 states = [False] * 8
@ -265,7 +259,9 @@ async def test_pushed_relay_status_change(
assert state.state == STATE_OFF assert state.state == STATE_OFF
async def test_unload_config_entry(hass: HomeAssistant, entry, lcn_connection) -> None: async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test the switch is removed when the config entry is unloaded.""" """Test the switch is removed when the config entry is unloaded."""
await init_integration(hass, entry)
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
assert hass.states.get(SWITCH_OUTPUT1).state == STATE_UNAVAILABLE assert hass.states.get(SWITCH_OUTPUT1).state == STATE_UNAVAILABLE

View File

@ -1,8 +1,11 @@
"""LCN Websocket Tests.""" """LCN Websocket Tests."""
from typing import Any
from pypck.lcn_addr import LcnAddr from pypck.lcn_addr import LcnAddr
import pytest import pytest
from homeassistant.components.lcn import AddressType
from homeassistant.components.lcn.const import CONF_DOMAIN_DATA from homeassistant.components.lcn.const import CONF_DOMAIN_DATA
from homeassistant.components.lcn.helpers import get_device_config, get_resource from homeassistant.components.lcn.helpers import get_device_config, get_resource
from homeassistant.const import ( from homeassistant.const import (
@ -16,6 +19,8 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .conftest import MockConfigEntry, init_integration
from tests.typing import WebSocketGenerator from tests.typing import WebSocketGenerator
DEVICES_PAYLOAD = {CONF_TYPE: "lcn/devices", "entry_id": ""} DEVICES_PAYLOAD = {CONF_TYPE: "lcn/devices", "entry_id": ""}
@ -52,11 +57,12 @@ ENTITIES_DELETE_PAYLOAD = {
async def test_lcn_devices_command( async def test_lcn_devices_command(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry, lcn_connection hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lcn/devices command.""" """Test lcn/devices command."""
client = await hass_ws_client(hass) await init_integration(hass, entry)
client = await hass_ws_client(hass)
await client.send_json_auto_id({**DEVICES_PAYLOAD, "entry_id": entry.entry_id}) await client.send_json_auto_id({**DEVICES_PAYLOAD, "entry_id": entry.entry_id})
res = await client.receive_json() res = await client.receive_json()
@ -79,11 +85,12 @@ async def test_lcn_devices_command(
async def test_lcn_entities_command( async def test_lcn_entities_command(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
entry, entry: MockConfigEntry,
lcn_connection,
payload, payload,
) -> None: ) -> None:
"""Test lcn/entities command.""" """Test lcn/entities command."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json_auto_id( await client.send_json_auto_id(
{ {
@ -107,10 +114,11 @@ async def test_lcn_entities_command(
async def test_lcn_devices_scan_command( async def test_lcn_devices_scan_command(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry, lcn_connection hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lcn/devices/scan command.""" """Test lcn/devices/scan command."""
# add new module which is not stored in config_entry # add new module which is not stored in config_entry
lcn_connection = await init_integration(hass, entry)
lcn_connection.get_address_conn(LcnAddr(0, 10, False)) lcn_connection.get_address_conn(LcnAddr(0, 10, False))
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
@ -129,9 +137,11 @@ async def test_lcn_devices_scan_command(
async def test_lcn_devices_add_command( async def test_lcn_devices_add_command(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry, lcn_connection hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lcn/devices/add command.""" """Test lcn/devices/add command."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
assert get_device_config((0, 10, False), entry) is None assert get_device_config((0, 10, False), entry) is None
@ -144,9 +154,11 @@ async def test_lcn_devices_add_command(
async def test_lcn_devices_delete_command( async def test_lcn_devices_delete_command(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry, lcn_connection hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lcn/devices/delete command.""" """Test lcn/devices/delete command."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
assert get_device_config((0, 7, False), entry) assert get_device_config((0, 7, False), entry)
@ -160,9 +172,11 @@ async def test_lcn_devices_delete_command(
async def test_lcn_entities_add_command( async def test_lcn_entities_add_command(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry, lcn_connection hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lcn/entities/add command.""" """Test lcn/entities/add command."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
entity_config = { entity_config = {
@ -185,9 +199,11 @@ async def test_lcn_entities_add_command(
async def test_lcn_entities_delete_command( async def test_lcn_entities_delete_command(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry, lcn_connection hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry: MockConfigEntry
) -> None: ) -> None:
"""Test lcn/entities/delete command.""" """Test lcn/entities/delete command."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
assert ( assert (
@ -239,12 +255,14 @@ async def test_lcn_entities_delete_command(
async def test_lcn_command_host_error( async def test_lcn_command_host_error(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
lcn_connection, entry: MockConfigEntry,
payload, payload: dict[str, str],
entity_id, entity_id: str,
result, result: bool,
) -> None: ) -> None:
"""Test lcn commands for unknown host.""" """Test lcn commands for unknown host."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json_auto_id({**payload, "entry_id": entity_id}) await client.send_json_auto_id({**payload, "entry_id": entity_id})
@ -265,13 +283,14 @@ async def test_lcn_command_host_error(
async def test_lcn_command_address_error( async def test_lcn_command_address_error(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
entry, entry: MockConfigEntry,
lcn_connection, payload: dict[str, Any],
payload, address: AddressType,
address, result: bool,
result,
) -> None: ) -> None:
"""Test lcn commands for address error.""" """Test lcn commands for address error."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json_auto_id( await client.send_json_auto_id(
{**payload, "entry_id": entry.entry_id, CONF_ADDRESS: address} {**payload, "entry_id": entry.entry_id, CONF_ADDRESS: address}
@ -285,10 +304,11 @@ async def test_lcn_command_address_error(
async def test_lcn_entities_add_existing_error( async def test_lcn_entities_add_existing_error(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
entry, entry: MockConfigEntry,
lcn_connection,
) -> None: ) -> None:
"""Test lcn commands for address error.""" """Test lcn commands for address error."""
await init_integration(hass, entry)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
await client.send_json_auto_id( await client.send_json_auto_id(
{ {