mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Migrate keymitt_ble to use runtime_data (#147179)
This commit is contained in:
parent
e23cac8bef
commit
d0e77eb1e2
@ -2,26 +2,20 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from microbot import MicroBotApiClient
|
||||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_ADDRESS, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MicroBotDataUpdateCoordinator
|
||||
from .coordinator import MicroBotConfigEntry, MicroBotDataUpdateCoordinator
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__package__)
|
||||
PLATFORMS: list[str] = [Platform.SWITCH]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: MicroBotConfigEntry) -> bool:
|
||||
"""Set up this integration using UI."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
token: str = entry.data[CONF_ACCESS_TOKEN]
|
||||
bdaddr: str = entry.data[CONF_ADDRESS]
|
||||
ble_device = bluetooth.async_ble_device_from_address(hass, bdaddr)
|
||||
@ -35,7 +29,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
hass, client=client, ble_device=ble_device
|
||||
)
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
entry.async_on_unload(coordinator.async_start())
|
||||
@ -43,9 +37,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: MicroBotConfigEntry) -> bool:
|
||||
"""Handle removal of an entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -11,14 +11,15 @@ from homeassistant.components import bluetooth
|
||||
from homeassistant.components.bluetooth.passive_update_coordinator import (
|
||||
PassiveBluetoothDataUpdateCoordinator,
|
||||
)
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from bleak.backends.device import BLEDevice
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__package__)
|
||||
PLATFORMS: list[str] = [Platform.SWITCH]
|
||||
|
||||
type MicroBotConfigEntry = ConfigEntry[MicroBotDataUpdateCoordinator]
|
||||
|
||||
|
||||
class MicroBotDataUpdateCoordinator(PassiveBluetoothDataUpdateCoordinator):
|
||||
@ -31,7 +32,7 @@ class MicroBotDataUpdateCoordinator(PassiveBluetoothDataUpdateCoordinator):
|
||||
ble_device: BLEDevice,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
self.api: MicroBotApiClient = client
|
||||
self.api = client
|
||||
self.data: dict[str, Any] = {}
|
||||
self.ble_device = ble_device
|
||||
super().__init__(
|
||||
|
@ -19,7 +19,7 @@ class MicroBotEntity(PassiveBluetoothCoordinatorEntity[MicroBotDataUpdateCoordin
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(self, coordinator, config_entry):
|
||||
def __init__(self, coordinator: MicroBotDataUpdateCoordinator) -> None:
|
||||
"""Initialise the entity."""
|
||||
super().__init__(coordinator)
|
||||
self._address = self.coordinator.ble_device.address
|
||||
|
@ -7,7 +7,6 @@ from typing import Any
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import (
|
||||
@ -16,8 +15,7 @@ from homeassistant.helpers.entity_platform import (
|
||||
)
|
||||
from homeassistant.helpers.typing import VolDictType
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MicroBotDataUpdateCoordinator
|
||||
from .coordinator import MicroBotConfigEntry
|
||||
from .entity import MicroBotEntity
|
||||
|
||||
CALIBRATE = "calibrate"
|
||||
@ -30,12 +28,11 @@ CALIBRATE_SCHEMA: VolDictType = {
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: MicroBotConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up MicroBot based on a config entry."""
|
||||
coordinator: MicroBotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities([MicroBotBinarySwitch(coordinator, entry)])
|
||||
async_add_entities([MicroBotBinarySwitch(entry.runtime_data)])
|
||||
platform = async_get_current_platform()
|
||||
platform.async_register_entity_service(
|
||||
CALIBRATE,
|
||||
|
Loading…
x
Reference in New Issue
Block a user