Migrate yalexs_ble to use config entry runtime_data (#117082)

This commit is contained in:
J. Nick Koston 2024-05-08 16:43:25 -05:00 committed by GitHub
parent 6eeeafa8b8
commit 8464c95fb4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 27 deletions

View file

@ -25,15 +25,17 @@ from .const import (
CONF_LOCAL_NAME,
CONF_SLOT,
DEVICE_TIMEOUT,
DOMAIN,
)
from .models import YaleXSBLEData
from .util import async_find_existing_service_info, bluetooth_callback_matcher
YALEXSBLEConfigEntry = ConfigEntry[YaleXSBLEData]
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.LOCK, Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: YALEXSBLEConfigEntry) -> bool:
"""Set up Yale Access Bluetooth from a config entry."""
local_name = entry.data[CONF_LOCAL_NAME]
address = entry.data[CONF_ADDRESS]
@ -98,9 +100,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
f"{ex}; Try moving the Bluetooth adapter closer to {local_name}"
) from ex
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = YaleXSBLEData(
entry.title, push_lock, always_connected
)
entry.runtime_data = YaleXSBLEData(entry.title, push_lock, always_connected)
@callback
def _async_device_unavailable(
@ -132,18 +132,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def _async_update_listener(
hass: HomeAssistant, entry: YALEXSBLEConfigEntry
) -> None:
"""Handle options update."""
data: YaleXSBLEData = hass.data[DOMAIN][entry.entry_id]
data = entry.runtime_data
if entry.title != data.title or data.always_connected != entry.options.get(
CONF_ALWAYS_CONNECTED
):
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: YALEXSBLEConfigEntry) -> bool:
"""Unload a config 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)

View file

@ -4,7 +4,6 @@ from __future__ import annotations
from yalexs_ble import ConnectionInfo, DoorStatus, LockInfo, LockState
from homeassistant import config_entries
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
@ -12,18 +11,17 @@ from homeassistant.components.binary_sensor import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from . import YALEXSBLEConfigEntry
from .entity import YALEXSBLEEntity
from .models import YaleXSBLEData
async def async_setup_entry(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,
entry: YALEXSBLEConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up YALE XS binary sensors."""
data: YaleXSBLEData = hass.data[DOMAIN][entry.entry_id]
data = entry.runtime_data
lock = data.lock
if lock.lock_info and lock.lock_info.door_sense:
async_add_entities([YaleXSBLEDoorSensor(data)])

View file

@ -7,23 +7,20 @@ from typing import Any
from yalexs_ble import ConnectionInfo, LockInfo, LockState, LockStatus
from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from . import YALEXSBLEConfigEntry
from .entity import YALEXSBLEEntity
from .models import YaleXSBLEData
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: YALEXSBLEConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up locks."""
data: YaleXSBLEData = hass.data[DOMAIN][entry.entry_id]
async_add_entities([YaleXSBLELock(data)])
async_add_entities([YaleXSBLELock(entry.runtime_data)])
class YaleXSBLELock(YALEXSBLEEntity, LockEntity):

View file

@ -7,7 +7,6 @@ from dataclasses import dataclass
from yalexs_ble import ConnectionInfo, LockInfo, LockState
from homeassistant import config_entries
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
@ -23,7 +22,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from . import YALEXSBLEConfigEntry
from .entity import YALEXSBLEEntity
from .models import YaleXSBLEData
@ -75,11 +74,11 @@ SENSORS: tuple[YaleXSBLESensorEntityDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant,
entry: config_entries.ConfigEntry,
entry: YALEXSBLEConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up YALE XS Bluetooth sensors."""
data: YaleXSBLEData = hass.data[DOMAIN][entry.entry_id]
data = entry.runtime_data
async_add_entities(YaleXSBLESensor(description, data) for description in SENSORS)