mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Migrate switchbot to use entry.runtime_data (#122530)
This commit is contained in:
parent
3b01a57de3
commit
a89853da9d
@ -24,11 +24,10 @@ from .const import (
|
|||||||
CONF_RETRY_COUNT,
|
CONF_RETRY_COUNT,
|
||||||
CONNECTABLE_SUPPORTED_MODEL_TYPES,
|
CONNECTABLE_SUPPORTED_MODEL_TYPES,
|
||||||
DEFAULT_RETRY_COUNT,
|
DEFAULT_RETRY_COUNT,
|
||||||
DOMAIN,
|
|
||||||
HASS_SENSOR_TYPE_TO_SWITCHBOT_MODEL,
|
HASS_SENSOR_TYPE_TO_SWITCHBOT_MODEL,
|
||||||
SupportedModels,
|
SupportedModels,
|
||||||
)
|
)
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS_BY_TYPE = {
|
PLATFORMS_BY_TYPE = {
|
||||||
SupportedModels.BULB.value: [Platform.SENSOR, Platform.LIGHT],
|
SupportedModels.BULB.value: [Platform.SENSOR, Platform.LIGHT],
|
||||||
@ -79,10 +78,9 @@ CLASS_BY_DEVICE = {
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: SwitchbotConfigEntry) -> bool:
|
||||||
"""Set up Switchbot from a config entry."""
|
"""Set up Switchbot from a config entry."""
|
||||||
assert entry.unique_id is not None
|
assert entry.unique_id is not None
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
if CONF_ADDRESS not in entry.data and CONF_MAC in entry.data:
|
if CONF_ADDRESS not in entry.data and CONF_MAC in entry.data:
|
||||||
# Bleak uses addresses not mac addresses which are actually
|
# Bleak uses addresses not mac addresses which are actually
|
||||||
# UUIDs on some platforms (MacOS).
|
# UUIDs on some platforms (MacOS).
|
||||||
@ -137,7 +135,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
retry_count=entry.options[CONF_RETRY_COUNT],
|
retry_count=entry.options[CONF_RETRY_COUNT],
|
||||||
)
|
)
|
||||||
|
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id] = SwitchbotDataUpdateCoordinator(
|
coordinator = entry.runtime_data = SwitchbotDataUpdateCoordinator(
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
ble_device,
|
ble_device,
|
||||||
@ -167,13 +165,6 @@ async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> Non
|
|||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
sensor_type = entry.data[CONF_SENSOR_TYPE]
|
sensor_type = entry.data[CONF_SENSOR_TYPE]
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
return await hass.config_entries.async_unload_platforms(
|
||||||
entry, PLATFORMS_BY_TYPE[sensor_type]
|
entry, PLATFORMS_BY_TYPE[sensor_type]
|
||||||
)
|
)
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
if not hass.config_entries.async_entries(DOMAIN):
|
|
||||||
hass.data.pop(DOMAIN)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
@ -7,13 +7,11 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
||||||
from .entity import SwitchbotEntity
|
from .entity import SwitchbotEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
@ -70,10 +68,12 @@ BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SwitchbotConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Switchbot curtain based on a config entry."""
|
"""Set up Switchbot curtain based on a config entry."""
|
||||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SwitchBotBinarySensor(coordinator, binary_sensor)
|
SwitchBotBinarySensor(coordinator, binary_sensor)
|
||||||
for binary_sensor in coordinator.device.parsed_data
|
for binary_sensor in coordinator.device.parsed_data
|
||||||
|
@ -14,6 +14,7 @@ from homeassistant.components import bluetooth
|
|||||||
from homeassistant.components.bluetooth.active_update_coordinator import (
|
from homeassistant.components.bluetooth.active_update_coordinator import (
|
||||||
ActiveBluetoothDataUpdateCoordinator,
|
ActiveBluetoothDataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import CoreState, HomeAssistant, callback
|
from homeassistant.core import CoreState, HomeAssistant, callback
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -24,6 +25,8 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
DEVICE_STARTUP_TIMEOUT = 30
|
DEVICE_STARTUP_TIMEOUT = 30
|
||||||
|
|
||||||
|
type SwitchbotConfigEntry = ConfigEntry[SwitchbotDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class SwitchbotDataUpdateCoordinator(ActiveBluetoothDataUpdateCoordinator[None]):
|
class SwitchbotDataUpdateCoordinator(ActiveBluetoothDataUpdateCoordinator[None]):
|
||||||
"""Class to manage fetching switchbot data."""
|
"""Class to manage fetching switchbot data."""
|
||||||
|
@ -16,13 +16,11 @@ from homeassistant.components.cover import (
|
|||||||
CoverEntity,
|
CoverEntity,
|
||||||
CoverEntityFeature,
|
CoverEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
||||||
from .entity import SwitchbotEntity
|
from .entity import SwitchbotEntity
|
||||||
|
|
||||||
# Initialize the logger
|
# Initialize the logger
|
||||||
@ -31,10 +29,12 @@ PARALLEL_UPDATES = 0
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SwitchbotConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Switchbot curtain based on a config entry."""
|
"""Set up Switchbot curtain based on a config entry."""
|
||||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
if isinstance(coordinator.device, switchbot.SwitchbotBlindTilt):
|
if isinstance(coordinator.device, switchbot.SwitchbotBlindTilt):
|
||||||
async_add_entities([SwitchBotBlindTiltEntity(coordinator)])
|
async_add_entities([SwitchBotBlindTiltEntity(coordinator)])
|
||||||
else:
|
else:
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
import switchbot
|
import switchbot
|
||||||
|
|
||||||
from homeassistant.components.humidifier import (
|
from homeassistant.components.humidifier import (
|
||||||
@ -13,24 +11,22 @@ from homeassistant.components.humidifier import (
|
|||||||
HumidifierEntity,
|
HumidifierEntity,
|
||||||
HumidifierEntityFeature,
|
HumidifierEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SwitchbotConfigEntry
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
||||||
from .entity import SwitchbotSwitchedEntity
|
from .entity import SwitchbotSwitchedEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SwitchbotConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Switchbot based on a config entry."""
|
"""Set up Switchbot based on a config entry."""
|
||||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
async_add_entities([SwitchBotHumidifier(entry.runtime_data)])
|
||||||
async_add_entities([SwitchBotHumidifier(coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class SwitchBotHumidifier(SwitchbotSwitchedEntity, HumidifierEntity):
|
class SwitchBotHumidifier(SwitchbotSwitchedEntity, HumidifierEntity):
|
||||||
|
@ -13,7 +13,6 @@ from homeassistant.components.light import (
|
|||||||
ColorMode,
|
ColorMode,
|
||||||
LightEntity,
|
LightEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.color import (
|
from homeassistant.util.color import (
|
||||||
@ -21,8 +20,7 @@ from homeassistant.util.color import (
|
|||||||
color_temperature_mired_to_kelvin,
|
color_temperature_mired_to_kelvin,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
||||||
from .entity import SwitchbotEntity
|
from .entity import SwitchbotEntity
|
||||||
|
|
||||||
SWITCHBOT_COLOR_MODE_TO_HASS = {
|
SWITCHBOT_COLOR_MODE_TO_HASS = {
|
||||||
@ -35,12 +33,11 @@ PARALLEL_UPDATES = 0
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: SwitchbotConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the switchbot light."""
|
"""Set up the switchbot light."""
|
||||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
async_add_entities([SwitchbotLightEntity(entry.runtime_data)])
|
||||||
async_add_entities([SwitchbotLightEntity(coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class SwitchbotLightEntity(SwitchbotEntity, LightEntity):
|
class SwitchbotLightEntity(SwitchbotEntity, LightEntity):
|
||||||
|
@ -6,21 +6,20 @@ import switchbot
|
|||||||
from switchbot.const import LockStatus
|
from switchbot.const import LockStatus
|
||||||
|
|
||||||
from homeassistant.components.lock import LockEntity, LockEntityFeature
|
from homeassistant.components.lock import LockEntity, LockEntityFeature
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
||||||
from .entity import SwitchbotEntity
|
from .entity import SwitchbotEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SwitchbotConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Switchbot lock based on a config entry."""
|
"""Set up Switchbot lock based on a config entry."""
|
||||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
async_add_entities([(SwitchBotLock(entry.runtime_data))])
|
||||||
async_add_entities([(SwitchBotLock(coordinator))])
|
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyAbstractClass
|
# noinspection PyAbstractClass
|
||||||
|
@ -9,7 +9,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
@ -20,8 +19,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
||||||
from .entity import SwitchbotEntity
|
from .entity import SwitchbotEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
@ -81,10 +79,12 @@ SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SwitchbotConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Switchbot sensor based on a config entry."""
|
"""Set up Switchbot sensor based on a config entry."""
|
||||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
entities = [
|
entities = [
|
||||||
SwitchBotSensor(coordinator, sensor)
|
SwitchBotSensor(coordinator, sensor)
|
||||||
for sensor in coordinator.device.parsed_data
|
for sensor in coordinator.device.parsed_data
|
||||||
|
@ -2,33 +2,29 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import switchbot
|
import switchbot
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import STATE_ON
|
from homeassistant.const import STATE_ON
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
|
||||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
||||||
from .entity import SwitchbotSwitchedEntity
|
from .entity import SwitchbotSwitchedEntity
|
||||||
|
|
||||||
# Initialize the logger
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: SwitchbotConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Switchbot based on a config entry."""
|
"""Set up Switchbot based on a config entry."""
|
||||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
async_add_entities([SwitchBotSwitch(entry.runtime_data)])
|
||||||
async_add_entities([SwitchBotSwitch(coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class SwitchBotSwitch(SwitchbotSwitchedEntity, SwitchEntity, RestoreEntity):
|
class SwitchBotSwitch(SwitchbotSwitchedEntity, SwitchEntity, RestoreEntity):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user