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