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

View File

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

View File

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

View File

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