Use runtime_data in bosch_shc (#136356)

This commit is contained in:
epenet 2025-01-23 18:52:10 +01:00 committed by GitHub
parent 8dba4affa9
commit 29c528ee54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 48 deletions

View File

@ -12,13 +12,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from .const import (
CONF_SSL_CERTIFICATE,
CONF_SSL_KEY,
DATA_POLLING_HANDLER,
DATA_SESSION,
DOMAIN,
)
from .const import CONF_SSL_CERTIFICATE, CONF_SSL_KEY, DOMAIN
PLATFORMS = [
Platform.BINARY_SENSOR,
@ -30,7 +24,10 @@ PLATFORMS = [
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
type BoschConfigEntry = ConfigEntry[SHCSession]
async def async_setup_entry(hass: HomeAssistant, entry: BoschConfigEntry) -> bool:
"""Set up Bosch SHC from a config entry."""
data = entry.data
@ -53,10 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if shc_info.updateState.name == "UPDATE_AVAILABLE":
_LOGGER.warning("Please check for software updates in the Bosch Smart Home App")
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {
DATA_SESSION: session,
}
entry.runtime_data = session
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
@ -76,23 +70,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.async_add_executor_job(session.stop_polling)
await hass.async_add_executor_job(session.start_polling)
hass.data[DOMAIN][entry.entry_id][DATA_POLLING_HANDLER] = (
entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_polling)
)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: BoschConfigEntry) -> bool:
"""Unload a config entry."""
session: SHCSession = hass.data[DOMAIN][entry.entry_id][DATA_SESSION]
await hass.async_add_executor_job(entry.runtime_data.stop_polling)
hass.data[DOMAIN][entry.entry_id][DATA_POLLING_HANDLER]()
hass.data[DOMAIN][entry.entry_id].pop(DATA_POLLING_HANDLER)
await hass.async_add_executor_job(session.stop_polling)
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -2,28 +2,27 @@
from __future__ import annotations
from boschshcpy import SHCBatteryDevice, SHCSession, SHCShutterContact
from boschshcpy import SHCBatteryDevice, SHCShutterContact
from boschshcpy.device import SHCDevice
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_SESSION, DOMAIN
from . import BoschConfigEntry
from .entity import SHCEntity
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: BoschConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the SHC binary sensor platform."""
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
session = config_entry.runtime_data
entities: list[BinarySensorEntity] = [
ShutterContactSensor(

View File

@ -6,7 +6,4 @@ CONF_SHC_KEY = "bosch_shc-key.pem"
CONF_SSL_CERTIFICATE = "ssl_certificate"
CONF_SSL_KEY = "ssl_key"
DATA_SESSION = "session"
DATA_POLLING_HANDLER = "polling_handler"
DOMAIN = "bosch_shc"

View File

@ -2,7 +2,7 @@
from typing import Any
from boschshcpy import SHCSession, SHCShutterControl
from boschshcpy import SHCShutterControl
from homeassistant.components.cover import (
ATTR_POSITION,
@ -10,22 +10,20 @@ from homeassistant.components.cover import (
CoverEntity,
CoverEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_SESSION, DOMAIN
from . import BoschConfigEntry
from .entity import SHCEntity
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: BoschConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the SHC cover platform."""
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
session = config_entry.runtime_data
async_add_entities(
ShutterControlCover(

View File

@ -6,7 +6,6 @@ from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from boschshcpy import SHCSession
from boschshcpy.device import SHCDevice
from homeassistant.components.sensor import (
@ -15,7 +14,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
@ -27,7 +25,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from .const import DATA_SESSION, DOMAIN
from . import BoschConfigEntry
from .entity import SHCEntity
@ -127,11 +125,11 @@ SENSOR_DESCRIPTIONS: dict[str, SHCSensorEntityDescription] = {
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: BoschConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the SHC sensor platform."""
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
session = config_entry.runtime_data
entities: list[SensorEntity] = [
SHCSensor(

View File

@ -9,7 +9,6 @@ from boschshcpy import (
SHCCamera360,
SHCCameraEyes,
SHCLightSwitch,
SHCSession,
SHCSmartPlug,
SHCSmartPlugCompact,
)
@ -20,13 +19,12 @@ from homeassistant.components.switch import (
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from .const import DATA_SESSION, DOMAIN
from . import BoschConfigEntry
from .entity import SHCEntity
@ -80,11 +78,11 @@ SWITCH_TYPES: dict[str, SHCSwitchEntityDescription] = {
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: BoschConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the SHC switch platform."""
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
session = config_entry.runtime_data
entities: list[SwitchEntity] = [
SHCSwitch(