Skip to content

Commit 37eef2f

Browse files
committed
Optimize - remove storing of version
1 parent 82aac91 commit 37eef2f

File tree

7 files changed

+14
-36
lines changed

7 files changed

+14
-36
lines changed

custom_components/plugwise/__init__.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
callback,
1616
)
1717
from homeassistant.helpers import device_registry as dr, entity_registry as er
18-
from packaging import version
1918

2019
from .const import (
2120
CONF_REFRESH_INTERVAL, # pw-beta options
22-
CONF_VERSION,
23-
DEFAULT_TIMEOUT,
2421
DOMAIN,
2522
LOGGER,
2623
PLATFORMS,
2724
SERVICE_DELETE, # pw-beta delete_notifications
2825
)
2926
from .coordinator import PlugwiseDataUpdateCoordinator
27+
from .util import get_timeout_for_version
3028

3129
type PlugwiseConfigEntry = ConfigEntry[PlugwiseDataUpdateCoordinator]
3230

@@ -159,13 +157,7 @@ async def async_migrate_plugwise_entry(
159157

160158
if entry.version == 1 and entry.minor_version < 2:
161159
new_data = {**entry.data}
162-
new_data[CONF_VERSION] = coordinator.api.smile_version
163-
timeout = DEFAULT_TIMEOUT
164-
if version.parse(new_data[CONF_VERSION]) >= version.parse("3.2.0"):
165-
timeout = 10
166-
167-
new_data[CONF_TIMEOUT] = timeout
168-
160+
new_data[CONF_TIMEOUT] = get_timeout_for_version(coordinator.api.smile_version)
169161
LOGGER.debug(
170162
"Migration to version %s.%s successful",
171163
entry.version,

custom_components/plugwise/config_flow.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@
4343
from homeassistant.core import HomeAssistant, callback
4444
from homeassistant.helpers import config_validation as cv
4545
from homeassistant.helpers.aiohttp_client import async_get_clientsession
46-
from packaging import version
4746

4847
from .const import (
4948
ANNA_WITH_ADAM,
5049
CONF_HOMEKIT_EMULATION, # pw-beta option
5150
CONF_REFRESH_INTERVAL, # pw-beta option
52-
CONF_VERSION,
5351
CONTEXT,
5452
DEFAULT_PORT,
5553
DEFAULT_SCAN_INTERVAL, # pw-beta option
@@ -74,6 +72,7 @@
7472

7573
# Upstream
7674
from .coordinator import PlugwiseDataUpdateCoordinator
75+
from .util import get_timeout_for_version
7776

7877
type PlugwiseConfigEntry = ConfigEntry[PlugwiseDataUpdateCoordinator]
7978

@@ -110,13 +109,6 @@ def _base_schema(
110109
return vol.Schema({vol.Required(CONF_PASSWORD): str})
111110

112111

113-
def get_timeout_for_version(version_str: str) -> int:
114-
"""Determine timeout value based on gateway version."""
115-
if version.parse(version_str) >= version.parse("3.2.0"):
116-
return 10
117-
return DEFAULT_TIMEOUT
118-
119-
120112
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> Smile:
121113
"""Validate whether the user input allows us to connect to the gateway.
122114
@@ -217,7 +209,6 @@ async def async_step_zeroconf(
217209
CONF_PORT: discovery_info.port,
218210
CONF_TIMEOUT: self._timeout,
219211
CONF_USERNAME: self._username,
220-
CONF_VERSION: _version,
221212
},
222213
ATTR_CONFIGURATION_URL: (
223214
f"http://{discovery_info.host}:{discovery_info.port}"
@@ -268,8 +259,6 @@ async def async_step_user(
268259
errors=errors,
269260
)
270261

271-
user_input[CONF_VERSION] = api.smile_version
272-
273262
await self.async_set_unique_id(
274263
api.smile_hostname or api.gateway_id, raise_on_progress=False
275264
)

custom_components/plugwise/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
from __future__ import annotations
44

55
from collections.abc import Awaitable, Callable, Coroutine
6+
from packaging import version
67
from typing import Any, Concatenate
78

89
from plugwise.exceptions import PlugwiseException
910

1011
from homeassistant.exceptions import HomeAssistantError
1112

13+
from .const import DEFAULT_TIMEOUT
1214
from .entity import PlugwiseEntity
1315

1416
# For reference:
@@ -17,6 +19,13 @@
1719
# _P = ParamSpec("_P")
1820

1921

22+
def get_timeout_for_version(version_str: str) -> int:
23+
"""Determine timeout value based on gateway version."""
24+
if version.parse(version_str) >= version.parse("3.2.0"):
25+
return 10
26+
return DEFAULT_TIMEOUT
27+
28+
2029
def plugwise_command[_PlugwiseEntityT: PlugwiseEntity, **_P, _R](
2130
func: Callable[Concatenate[_PlugwiseEntityT, _P], Awaitable[_R]],
2231
) -> Callable[Concatenate[_PlugwiseEntityT, _P], Coroutine[Any, Any, _R]]:

tests/components/plugwise/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from plugwise import PlugwiseData
1010
import pytest
1111

12-
from homeassistant.components.plugwise.const import CONF_VERSION, DOMAIN
12+
from homeassistant.components.plugwise.const import DOMAIN
1313
from homeassistant.const import (
1414
CONF_HOST,
1515
CONF_MAC,
@@ -42,7 +42,6 @@ def mock_config_entry() -> MockConfigEntry:
4242
CONF_PORT: 80,
4343
CONF_TIMEOUT: 30,
4444
CONF_USERNAME: "smile",
45-
CONF_VERSION: "4.3.2"
4645
},
4746
minor_version=2,
4847
version=1,

tests/components/plugwise/snapshots/test_init.ambr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
'port': 80,
99
'timeout': 10,
1010
'username': 'smile',
11-
'version': '4.0.15',
1211
}),
1312
'disabled_by': None,
1413
'domain': 'plugwise',

tests/components/plugwise/test_config_flow.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from homeassistant.components.plugwise.const import (
1717
CONF_HOMEKIT_EMULATION,
1818
CONF_REFRESH_INTERVAL,
19-
CONF_VERSION,
2019
DEFAULT_PORT,
2120
DOMAIN,
2221
)
@@ -46,9 +45,6 @@
4645
TEST_TIMEOUT = 10
4746
TEST_USERNAME = "smile"
4847
TEST_USERNAME2 = "stretch"
49-
TEST_VERSION = "4.3.2"
50-
TEST_VERSION_LEGACY = "2.1.0"
51-
5248
TEST_DISCOVERY = zeroconf.ZeroconfServiceInfo(
5349
ip_address=TEST_HOST,
5450
ip_addresses=[TEST_HOST],
@@ -150,7 +146,6 @@ async def test_form(
150146
CONF_PORT: DEFAULT_PORT,
151147
CONF_USERNAME: TEST_USERNAME,
152148
CONF_TIMEOUT: TEST_TIMEOUT_LEGACY,
153-
CONF_VERSION: TEST_VERSION,
154149
}
155150

156151
assert len(mock_setup_entry.mock_calls) == 1
@@ -197,7 +192,6 @@ async def test_zeroconf_form(
197192
CONF_PORT: DEFAULT_PORT,
198193
CONF_USERNAME: username,
199194
CONF_TIMEOUT: timeout,
200-
CONF_VERSION: TEST_VERSION,
201195
}
202196

203197
assert len(mock_setup_entry.mock_calls) == 1
@@ -234,7 +228,6 @@ async def test_zeroconf_stretch_form(
234228
CONF_PORT: DEFAULT_PORT,
235229
CONF_USERNAME: TEST_USERNAME2,
236230
CONF_TIMEOUT: TEST_TIMEOUT_LEGACY,
237-
CONF_VERSION: TEST_VERSION,
238231
}
239232

240233
assert len(mock_setup_entry.mock_calls) == 1
@@ -397,7 +390,6 @@ async def test_flow_errors(
397390
CONF_PORT: DEFAULT_PORT,
398391
CONF_USERNAME: TEST_USERNAME,
399392
CONF_TIMEOUT: TEST_TIMEOUT_LEGACY,
400-
CONF_VERSION: TEST_VERSION,
401393
}
402394

403395
assert len(mock_setup_entry.mock_calls) == 1
@@ -440,7 +432,6 @@ async def test_options_flow_thermo(
440432
CONF_HOST: TEST_HOST,
441433
CONF_PASSWORD: TEST_PASSWORD,
442434
CONF_TIMEOUT: TEST_TIMEOUT,
443-
CONF_VERSION: TEST_VERSION,
444435
},
445436
minor_version=2,
446437
options={

tests/components/plugwise/test_init.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest
1515

1616
from freezegun.api import FrozenDateTimeFactory
17-
from homeassistant.components.plugwise.const import CONF_VERSION, DOMAIN
17+
from homeassistant.components.plugwise.const import DOMAIN
1818
from homeassistant.config_entries import ConfigEntryState
1919
from homeassistant.const import (
2020
CONF_HOST,
@@ -251,7 +251,6 @@ async def test_entry_migration(
251251
CONF_PASSWORD: "test-password",
252252
CONF_PORT: 80,
253253
CONF_USERNAME: "smile",
254-
CONF_VERSION: "4.3.2",
255254
},
256255
minor_version=1,
257256
version=1,

0 commit comments

Comments
 (0)