Skip to content

Commit 93b34d7

Browse files
committed
Fix typings
1 parent 45ab8f5 commit 93b34d7

File tree

9 files changed

+245
-178
lines changed

9 files changed

+245
-178
lines changed

custom_components/amshan/__init__.py

+46-35
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
"""The AMS HAN meter integration."""
2+
23
from __future__ import annotations
34

45
import asyncio
6+
import logging
57
from dataclasses import dataclass
6-
import datetime as dt
78
from enum import Enum
8-
import logging
9-
from typing import Callable, Mapping, cast
9+
from typing import TYPE_CHECKING, cast
1010

11-
from han import common as han_type, meter_connection, obis_map
11+
from han import common as han_type
12+
from han import meter_connection, obis_map
1213
from homeassistant import const as ha_const
13-
from homeassistant.const import Platform, UnitOfReactivePower
1414
from homeassistant.components import sensor as ha_sensor
1515
from homeassistant.config_entries import ConfigEntry
16-
from homeassistant.core import CALLBACK_TYPE, callback, HomeAssistant, Event
16+
from homeassistant.const import Platform, UnitOfReactivePower
17+
from homeassistant.core import CALLBACK_TYPE, Event, HomeAssistant, callback
1718
from homeassistant.helpers import entity_registry
18-
from homeassistant.helpers.typing import ConfigType
1919

2020
from .const import (
2121
CONF_CONNECTION_CONFIG,
@@ -25,13 +25,23 @@
2525
)
2626
from .metercon import async_setup_meter_mqtt_subscriptions, setup_meter_connection
2727

28+
if TYPE_CHECKING:
29+
import datetime as dt
30+
from collections.abc import Callable, Mapping
31+
32+
CFG_VERSION_1 = 1
33+
CFG_VERSION_2 = 2
34+
CFG_VERSION_3 = 1
35+
2836
_LOGGER: logging.Logger = logging.getLogger(__name__)
2937

3038
type AmsHanConfigEntry = ConfigEntry[AmsHanData]
3139

3240

3341
@dataclass
3442
class AmsHanData:
43+
"""Integration runtime data."""
44+
3545
integration: AmsHanIntegration
3646

3747

@@ -59,7 +69,7 @@ async def async_setup_receiver(
5969
) -> None:
6070
"""Set up MQTT or serial/tcp-ip receiver."""
6171
connection_type = ConnectionType(config_data[CONF_CONNECTION_TYPE])
62-
if ConnectionType.MQTT == connection_type:
72+
if connection_type == ConnectionType.MQTT:
6373
self._mqtt_unsubscribe = await async_setup_meter_mqtt_subscriptions(
6474
hass,
6575
config_data[CONF_CONNECTION_CONFIG],
@@ -110,12 +120,9 @@ def stop_receive(self) -> None:
110120
self._mqtt_unsubscribe = None
111121

112122

113-
async def async_setup(hass: HomeAssistant, _: ConfigType) -> bool:
114-
"""Set up the amshan component."""
115-
return True
116-
117-
118-
async def async_setup_entry(hass: HomeAssistant, config_entry: AmsHanConfigEntry) -> bool:
123+
async def async_setup_entry(
124+
hass: HomeAssistant, config_entry: AmsHanConfigEntry
125+
) -> bool:
119126
"""Set up amshan from a config entry."""
120127
integration = AmsHanIntegration()
121128

@@ -138,26 +145,28 @@ async def on_hass_stop(event: Event) -> None:
138145

139146
config_entry.runtime_data = AmsHanData(integration)
140147

141-
await hass.config_entries.async_forward_entry_setups(config_entry, [Platform.SENSOR])
148+
await hass.config_entries.async_forward_entry_setups(
149+
config_entry, [Platform.SENSOR]
150+
)
142151

143152
_LOGGER.debug("async_setup_entry complete.")
144153

145154
return True
146155

147156

148157
async def async_migrate_config_entry(
149-
hass: HomeAssistant, config_entry: ConfigEntry
158+
hass: HomeAssistant, config_entry: AmsHanConfigEntry
150159
) -> bool:
151160
"""Migrate config when ConfigFlow version has changed."""
152161
initial_version = config_entry.version
153162
current_data = config_entry.data
154163
_LOGGER.debug("Check for config entry migration of version %d", initial_version)
155164

156-
if config_entry.version == 1:
157-
await _async_migrate_entries(
165+
if config_entry.version == CFG_VERSION_1:
166+
_migrate_entries(
158167
hass, config_entry.entry_id, _migrate_entity_entry_from_v1_to_v2
159168
)
160-
config_entry.version = 2
169+
config_entry.version = CFG_VERSION_2
161170
current_data = {
162171
CONF_CONNECTION_TYPE: (
163172
ConnectionType.MQTT
@@ -170,9 +179,9 @@ async def async_migrate_config_entry(
170179
}
171180
_LOGGER.debug("Config entry migrated to version 2")
172181

173-
if config_entry.version == 2:
174-
config_entry.version = 3
175-
await _async_migrate_entries(
182+
if config_entry.version == CFG_VERSION_2:
183+
config_entry.version = CFG_VERSION_3
184+
_migrate_entries(
176185
hass, config_entry.entry_id, _migrate_entity_entry_from_v2_to_v3
177186
)
178187
_LOGGER.debug("Config entry migrated to version 3")
@@ -204,15 +213,15 @@ async def async_unload_entry(
204213

205214
@callback
206215
async def async_config_entry_changed(
207-
hass: HomeAssistant, config_entry: ConfigEntry
208-
):
216+
hass: HomeAssistant, config_entry: AmsHanConfigEntry
217+
) -> None:
209218
"""Handle config entry changed callback."""
210219
_LOGGER.info("Config entry has changed. Reload integration.")
211220
await hass.config_entries.async_reload(config_entry.entry_id)
212221

213222

214-
def _migrate_entity_entry_from_v1_to_v2(entity: entity_registry.RegistryEntry):
215-
def replace_ending(source, old, new):
223+
def _migrate_entity_entry_from_v1_to_v2(entity: entity_registry.RegistryEntry) -> dict:
224+
def replace_ending(source: str, old: str, new: str) -> str:
216225
if source.endswith(old):
217226
return source[: -len(old)] + new
218227
return source
@@ -225,7 +234,7 @@ def replace_ending(source, old, new):
225234
return update
226235

227236

228-
def _migrate_entity_entry_from_v2_to_v3(entity: entity_registry.RegistryEntry):
237+
def _migrate_entity_entry_from_v2_to_v3(entity: entity_registry.RegistryEntry) -> dict:
229238
update = {}
230239

231240
v3_migrate_fields = [
@@ -279,23 +288,25 @@ def _migrate_entity_entry_from_v2_to_v3(entity: entity_registry.RegistryEntry):
279288
return update
280289

281290

282-
async def _async_migrate_entries(
291+
def _migrate_entries(
283292
hass: HomeAssistant,
284293
config_entry_id: str,
285294
entry_callback: Callable[[entity_registry.RegistryEntry], dict | None],
286295
) -> None:
287-
ent_reg = await entity_registry.async_get_registry(hass)
296+
ent_reg = entity_registry.async_get(hass)
288297

289298
# Workaround:
290299
# entity_registry.async_migrate_entries fails with:
291-
# "RuntimeError: dictionary keys changed during iteration"
300+
# RuntimeError: dictionary keys changed during iteration"
292301
# Try to get all entries from the dictionary before working on them.
293-
# The migration dows not directly change any keys of the registry. Concurrency problem in HA?
302+
# The migration dows not directly change any keys of the registry.
303+
# Concurrency problem in HA?
294304

295-
entries = []
296-
for entry in ent_reg.entities.values():
297-
if entry.config_entry_id == config_entry_id:
298-
entries.append(entry)
305+
entries = [
306+
entry
307+
for entry in ent_reg.entities.values()
308+
if entry.config_entry_id == config_entry_id
309+
]
299310

300311
for entry in entries:
301312
updates = entry_callback(entry)

0 commit comments

Comments
 (0)