Skip to content

Commit 0dfa037

Browse files
authored
Refactor device classes for LCN (home-assistant#156791)
1 parent c32a471 commit 0dfa037

File tree

15 files changed

+73
-82
lines changed

15 files changed

+73
-82
lines changed

homeassistant/components/lcn/entity.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from collections.abc import Callable
44

5+
from pypck.device import DeviceConnection
6+
57
from homeassistant.const import CONF_ADDRESS, CONF_DOMAIN, CONF_NAME
68
from homeassistant.helpers.device_registry import DeviceInfo
79
from homeassistant.helpers.entity import Entity
@@ -10,7 +12,6 @@
1012
from .const import CONF_DOMAIN_DATA, DOMAIN
1113
from .helpers import (
1214
AddressType,
13-
DeviceConnectionType,
1415
InputType,
1516
LcnConfigEntry,
1617
generate_unique_id,
@@ -23,7 +24,7 @@ class LcnEntity(Entity):
2324
"""Parent class for all entities associated with the LCN component."""
2425

2526
_attr_has_entity_name = True
26-
device_connection: DeviceConnectionType
27+
device_connection: DeviceConnection
2728

2829
def __init__(
2930
self,

homeassistant/components/lcn/helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pypck
1313
from pypck.connection import PchkConnectionManager
14+
from pypck.device import DeviceConnection
1415

1516
from homeassistant.config_entries import ConfigEntry
1617
from homeassistant.const import (
@@ -48,7 +49,7 @@ class LcnRuntimeData:
4849
connection: PchkConnectionManager
4950
"""Connection to PCHK host."""
5051

51-
device_connections: dict[str, DeviceConnectionType]
52+
device_connections: dict[str, DeviceConnection]
5253
"""Logical addresses of devices connected to the host."""
5354

5455
add_entities_callbacks: dict[str, Callable[[Iterable[ConfigType]], None]]
@@ -59,7 +60,6 @@ class LcnRuntimeData:
5960
type LcnConfigEntry = ConfigEntry[LcnRuntimeData]
6061

6162
type AddressType = tuple[int, int, bool]
62-
type DeviceConnectionType = pypck.module.ModuleConnection | pypck.module.GroupConnection
6363

6464
type InputType = type[pypck.inputs.Input]
6565

@@ -82,11 +82,11 @@ class LcnRuntimeData:
8282

8383
def get_device_connection(
8484
hass: HomeAssistant, address: AddressType, config_entry: LcnConfigEntry
85-
) -> DeviceConnectionType:
85+
) -> DeviceConnection:
8686
"""Return a lcn device_connection."""
8787
host_connection = config_entry.runtime_data.connection
8888
addr = pypck.lcn_addr.LcnAddr(*address)
89-
return host_connection.get_address_conn(addr)
89+
return host_connection.get_device_connection(addr)
9090

9191

9292
def get_resource(domain_name: str, domain_data: ConfigType) -> str:
@@ -246,7 +246,7 @@ def register_lcn_address_devices(
246246

247247

248248
async def async_update_device_config(
249-
device_connection: DeviceConnectionType, device_config: ConfigType
249+
device_connection: DeviceConnection, device_config: ConfigType
250250
) -> None:
251251
"""Fill missing values in device_config with infos from LCN bus."""
252252
# fetch serial info if device is module

homeassistant/components/lcn/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"iot_class": "local_polling",
1010
"loggers": ["pypck"],
1111
"quality_scale": "bronze",
12-
"requirements": ["pypck==0.9.2", "lcn-frontend==0.2.7"]
12+
"requirements": ["pypck==0.9.3", "lcn-frontend==0.2.7"]
1313
}

homeassistant/components/lcn/services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from enum import StrEnum, auto
44

55
import pypck
6+
from pypck.device import DeviceConnection
67
import voluptuous as vol
78

89
from homeassistant.const import (
@@ -48,7 +49,7 @@
4849
VAR_UNITS,
4950
VARIABLES,
5051
)
51-
from .helpers import DeviceConnectionType, LcnConfigEntry, is_states_string
52+
from .helpers import LcnConfigEntry, is_states_string
5253

5354

5455
class LcnServiceCall:
@@ -65,7 +66,7 @@ def __init__(self, hass: HomeAssistant) -> None:
6566
"""Initialize service call."""
6667
self.hass = hass
6768

68-
def get_device_connection(self, service: ServiceCall) -> DeviceConnectionType:
69+
def get_device_connection(self, service: ServiceCall) -> DeviceConnection:
6970
"""Get address connection object."""
7071
entries: list[LcnConfigEntry] = self.hass.config_entries.async_loaded_entries(
7172
DOMAIN

homeassistant/components/lcn/websocket.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Any, Final
88

99
import lcn_frontend as lcn_panel
10+
from pypck.device import DeviceConnection
1011
import voluptuous as vol
1112

1213
from homeassistant.components import panel_custom, websocket_api
@@ -37,7 +38,6 @@
3738
DOMAIN,
3839
)
3940
from .helpers import (
40-
DeviceConnectionType,
4141
LcnConfigEntry,
4242
async_update_device_config,
4343
generate_unique_id,
@@ -182,7 +182,7 @@ async def websocket_scan_devices(
182182
host_connection = config_entry.runtime_data.connection
183183
await host_connection.scan_modules()
184184

185-
for device_connection in host_connection.address_conns.values():
185+
for device_connection in host_connection.device_connections.values():
186186
if not device_connection.is_group:
187187
await async_create_or_update_device_in_config_entry(
188188
hass, device_connection, config_entry
@@ -421,7 +421,7 @@ async def websocket_delete_entity(
421421

422422
async def async_create_or_update_device_in_config_entry(
423423
hass: HomeAssistant,
424-
device_connection: DeviceConnectionType,
424+
device_connection: DeviceConnection,
425425
config_entry: LcnConfigEntry,
426426
) -> None:
427427
"""Create or update device in config_entry according to given device_connection."""

requirements_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements_test_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/components/lcn/conftest.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pypck
88
from pypck import lcn_defs
9-
from pypck.module import GroupConnection, ModuleConnection, Serials
9+
from pypck.device import DeviceConnection, Serials
1010
import pytest
1111

1212
from homeassistant.components.lcn import PchkConnectionManager
@@ -22,7 +22,7 @@
2222
LATEST_CONFIG_ENTRY_VERSION = (LcnFlowHandler.VERSION, LcnFlowHandler.MINOR_VERSION)
2323

2424

25-
class MockModuleConnection(ModuleConnection):
25+
class MockDeviceConnection(DeviceConnection):
2626
"""Fake a LCN module connection."""
2727

2828
request_name = AsyncMock(return_value="TestModule")
@@ -49,12 +49,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
4949
self._serials_known.set()
5050

5151

52-
class MockGroupConnection(GroupConnection):
53-
"""Fake a LCN group connection."""
54-
55-
send_command = AsyncMock(return_value=True)
56-
57-
5852
class MockPchkConnectionManager(PchkConnectionManager):
5953
"""Fake connection handler."""
6054

@@ -67,15 +61,10 @@ async def async_connect(self, timeout: int = 30) -> None:
6761
async def async_close(self) -> None:
6862
"""Mock closing a connection to PCHK."""
6963

70-
@patch.object(pypck.connection, "ModuleConnection", MockModuleConnection)
71-
def get_module_conn(self, addr):
72-
"""Get LCN module connection."""
73-
return super().get_module_conn(addr)
74-
75-
@patch.object(pypck.connection, "GroupConnection", MockGroupConnection)
76-
def get_group_conn(self, addr):
77-
"""Get LCN group connection."""
78-
return super().get_group_conn(addr)
64+
@patch.object(pypck.connection, "DeviceConnection", MockDeviceConnection)
65+
def get_device_connection(self, addr):
66+
"""Get LCN device connection."""
67+
return super().get_device_connection(addr)
7968

8069
scan_modules = AsyncMock()
8170
send_command = AsyncMock()

tests/components/lcn/test_climate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from homeassistant.exceptions import ServiceValidationError
3030
from homeassistant.helpers import entity_registry as er
3131

32-
from .conftest import MockConfigEntry, MockModuleConnection, init_integration
32+
from .conftest import MockConfigEntry, MockDeviceConnection, init_integration
3333

3434
from tests.common import snapshot_platform
3535

@@ -51,7 +51,7 @@ async def test_set_hvac_mode_heat(hass: HomeAssistant, entry: MockConfigEntry) -
5151
"""Test the hvac mode is set to heat."""
5252
await init_integration(hass, entry)
5353

54-
with patch.object(MockModuleConnection, "lock_regulator") as lock_regulator:
54+
with patch.object(MockDeviceConnection, "lock_regulator") as lock_regulator:
5555
await hass.services.async_call(
5656
DOMAIN_CLIMATE,
5757
SERVICE_SET_HVAC_MODE,
@@ -106,7 +106,7 @@ async def test_set_hvac_mode_off(hass: HomeAssistant, entry: MockConfigEntry) ->
106106
"""Test the hvac mode is set off."""
107107
await init_integration(hass, entry)
108108

109-
with patch.object(MockModuleConnection, "lock_regulator") as lock_regulator:
109+
with patch.object(MockDeviceConnection, "lock_regulator") as lock_regulator:
110110
state = hass.states.get("climate.testmodule_climate1")
111111
state.state = HVACMode.HEAT
112112

@@ -154,7 +154,7 @@ async def test_set_temperature(hass: HomeAssistant, entry: MockConfigEntry) -> N
154154
"""Test the temperature is set."""
155155
await init_integration(hass, entry)
156156

157-
with patch.object(MockModuleConnection, "var_abs") as var_abs:
157+
with patch.object(MockDeviceConnection, "var_abs") as var_abs:
158158
state = hass.states.get("climate.testmodule_climate1")
159159
state.state = HVACMode.HEAT
160160

tests/components/lcn/test_cover.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from homeassistant.core import HomeAssistant
3333
from homeassistant.helpers import entity_registry as er
3434

35-
from .conftest import MockConfigEntry, MockModuleConnection, init_integration
35+
from .conftest import MockConfigEntry, MockDeviceConnection, init_integration
3636

3737
from tests.common import snapshot_platform
3838

@@ -60,7 +60,7 @@ async def test_outputs_open(hass: HomeAssistant, entry: MockConfigEntry) -> None
6060
await init_integration(hass, entry)
6161

6262
with patch.object(
63-
MockModuleConnection, "control_motor_outputs"
63+
MockDeviceConnection, "control_motor_outputs"
6464
) as control_motor_outputs:
6565
state = hass.states.get(COVER_OUTPUTS)
6666
assert state is not None
@@ -109,7 +109,7 @@ async def test_outputs_close(hass: HomeAssistant, entry: MockConfigEntry) -> Non
109109
await init_integration(hass, entry)
110110

111111
with patch.object(
112-
MockModuleConnection, "control_motor_outputs"
112+
MockDeviceConnection, "control_motor_outputs"
113113
) as control_motor_outputs:
114114
await hass.services.async_call(
115115
DOMAIN_COVER,
@@ -161,7 +161,7 @@ async def test_outputs_stop(hass: HomeAssistant, entry: MockConfigEntry) -> None
161161
await init_integration(hass, entry)
162162

163163
with patch.object(
164-
MockModuleConnection, "control_motor_outputs"
164+
MockDeviceConnection, "control_motor_outputs"
165165
) as control_motor_outputs:
166166
await hass.services.async_call(
167167
DOMAIN_COVER,
@@ -209,7 +209,7 @@ async def test_relays_open(hass: HomeAssistant, entry: MockConfigEntry) -> None:
209209
await init_integration(hass, entry)
210210

211211
with patch.object(
212-
MockModuleConnection, "control_motor_relays"
212+
MockDeviceConnection, "control_motor_relays"
213213
) as control_motor_relays:
214214
state = hass.states.get(COVER_RELAYS)
215215
assert state is not None
@@ -258,7 +258,7 @@ async def test_relays_close(hass: HomeAssistant, entry: MockConfigEntry) -> None
258258
await init_integration(hass, entry)
259259

260260
with patch.object(
261-
MockModuleConnection, "control_motor_relays"
261+
MockDeviceConnection, "control_motor_relays"
262262
) as control_motor_relays:
263263
await hass.services.async_call(
264264
DOMAIN_COVER,
@@ -310,7 +310,7 @@ async def test_relays_stop(hass: HomeAssistant, entry: MockConfigEntry) -> None:
310310
await init_integration(hass, entry)
311311

312312
with patch.object(
313-
MockModuleConnection, "control_motor_relays"
313+
MockDeviceConnection, "control_motor_relays"
314314
) as control_motor_relays:
315315
await hass.services.async_call(
316316
DOMAIN_COVER,
@@ -375,7 +375,7 @@ async def test_relays_set_position(
375375
await init_integration(hass, entry)
376376

377377
with patch.object(
378-
MockModuleConnection, "control_motor_relays_position"
378+
MockDeviceConnection, "control_motor_relays_position"
379379
) as control_motor_relays_position:
380380
state = hass.states.get(entity_id)
381381
assert state is not None

0 commit comments

Comments
 (0)