Skip to content

Commit 62d9586

Browse files
authored
Migrate Thermo Beacon to config entry runtime data (home-assistant#170226)
1 parent b2dad41 commit 62d9586

2 files changed

Lines changed: 18 additions & 25 deletions

File tree

homeassistant/components/thermobeacon/__init__.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
from homeassistant.const import Platform
1313
from homeassistant.core import HomeAssistant
1414

15-
from .const import DOMAIN
16-
1715
PLATFORMS: list[Platform] = [Platform.SENSOR]
1816

1917
_LOGGER = logging.getLogger(__name__)
2018

19+
type ThermoBeaconConfigEntry = ConfigEntry[PassiveBluetoothProcessorCoordinator]
20+
2121

22-
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
22+
async def async_setup_entry(
23+
hass: HomeAssistant, entry: ThermoBeaconConfigEntry
24+
) -> bool:
2325
"""Set up ThermoBeacon BLE device from a config entry."""
2426
address = entry.unique_id
2527
assert address is not None
2628
data = ThermoBeaconBluetoothDeviceData()
27-
coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = (
28-
PassiveBluetoothProcessorCoordinator(
29-
hass,
30-
_LOGGER,
31-
address=address,
32-
mode=BluetoothScanningMode.PASSIVE,
33-
update_method=data.update,
34-
)
29+
entry.runtime_data = coordinator = PassiveBluetoothProcessorCoordinator(
30+
hass,
31+
_LOGGER,
32+
address=address,
33+
mode=BluetoothScanningMode.PASSIVE,
34+
update_method=data.update,
3535
)
3636
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
3737
entry.async_on_unload(
@@ -40,9 +40,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
4040
return True
4141

4242

43-
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
43+
async def async_unload_entry(
44+
hass: HomeAssistant, entry: ThermoBeaconConfigEntry
45+
) -> bool:
4446
"""Unload a config entry."""
45-
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
46-
hass.data[DOMAIN].pop(entry.entry_id)
47-
48-
return unload_ok
47+
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

homeassistant/components/thermobeacon/sensor.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
Units,
77
)
88

9-
from homeassistant import config_entries
109
from homeassistant.components.bluetooth.passive_update_processor import (
1110
PassiveBluetoothDataProcessor,
1211
PassiveBluetoothDataUpdate,
13-
PassiveBluetoothProcessorCoordinator,
1412
PassiveBluetoothProcessorEntity,
1513
)
1614
from homeassistant.components.sensor import (
@@ -30,7 +28,7 @@
3028
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
3129
from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
3230

33-
from .const import DOMAIN
31+
from . import ThermoBeaconConfigEntry
3432
from .device import device_key_to_bluetooth_entity_key
3533

3634
SENSOR_DESCRIPTIONS = {
@@ -110,15 +108,11 @@ def sensor_update_to_bluetooth_data_update(
110108

111109
async def async_setup_entry(
112110
hass: HomeAssistant,
113-
entry: config_entries.ConfigEntry,
111+
entry: ThermoBeaconConfigEntry,
114112
async_add_entities: AddConfigEntryEntitiesCallback,
115113
) -> None:
116114
"""Set up the ThermoBeacon BLE sensors."""
117-
# Uses legacy hass.data[DOMAIN] pattern
118-
# pylint: disable-next=hass-use-runtime-data
119-
coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
120-
entry.entry_id
121-
]
115+
coordinator = entry.runtime_data
122116
processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
123117
entry.async_on_unload(
124118
processor.async_add_entities_listener(

0 commit comments

Comments
 (0)