From 6dc391e16916c7620ddefcf535ebfba6a25b0fa5 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 1 Apr 2026 09:47:03 +0200 Subject: [PATCH] Use runtime_data in obihai integration (#167037) Co-authored-by: Claude Opus 4.6 (1M context) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- homeassistant/components/obihai/__init__.py | 14 ++++++++------ homeassistant/components/obihai/button.py | 10 +++++----- homeassistant/components/obihai/sensor.py | 8 ++++---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/obihai/__init__.py b/homeassistant/components/obihai/__init__.py index 43fd3e3426b..6262661d315 100644 --- a/homeassistant/components/obihai/__init__.py +++ b/homeassistant/components/obihai/__init__.py @@ -6,10 +6,12 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import format_mac from .connectivity import ObihaiConnection -from .const import DOMAIN, LOGGER, PLATFORMS +from .const import LOGGER, PLATFORMS + +type ObihaiConfigEntry = ConfigEntry[ObihaiConnection] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: ObihaiConfigEntry) -> bool: """Set up from a config entry.""" requester = ObihaiConnection( @@ -18,20 +20,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: password=entry.data[CONF_PASSWORD], ) await hass.async_add_executor_job(requester.update) - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = requester + entry.runtime_data = requester await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True -async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_migrate_entry(hass: HomeAssistant, entry: ObihaiConfigEntry) -> bool: """Migrate old entry.""" version = entry.version LOGGER.debug("Migrating from version %s", version) if version != 2: - requester: ObihaiConnection = hass.data[DOMAIN][entry.entry_id] + requester = entry.runtime_data device_mac = await hass.async_add_executor_job( requester.pyobihai.get_device_mac @@ -45,6 +47,6 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: ObihaiConfigEntry) -> bool: """Unload a config entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/obihai/button.py b/homeassistant/components/obihai/button.py index 9cef92d3fce..f1a244fee42 100644 --- a/homeassistant/components/obihai/button.py +++ b/homeassistant/components/obihai/button.py @@ -7,14 +7,14 @@ from homeassistant.components.button import ( ButtonEntity, ButtonEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_platform +from . import ObihaiConfigEntry from .connectivity import ObihaiConnection -from .const import DOMAIN, OBIHAI +from .const import OBIHAI BUTTON_DESCRIPTION = ButtonEntityDescription( key="reboot", @@ -26,12 +26,12 @@ BUTTON_DESCRIPTION = ButtonEntityDescription( async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: ObihaiConfigEntry, async_add_entities: entity_platform.AddConfigEntryEntitiesCallback, ) -> None: - """Set up the Obihai sensor entries.""" + """Set up the Obihai button entries.""" - requester: ObihaiConnection = hass.data[DOMAIN][entry.entry_id] + requester = entry.runtime_data buttons = [ObihaiButton(requester)] async_add_entities(buttons, update_before_add=True) diff --git a/homeassistant/components/obihai/sensor.py b/homeassistant/components/obihai/sensor.py index ec29238201a..03a11c14001 100644 --- a/homeassistant/components/obihai/sensor.py +++ b/homeassistant/components/obihai/sensor.py @@ -7,24 +7,24 @@ import datetime from requests.exceptions import RequestException from homeassistant.components.sensor import SensorDeviceClass, SensorEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from . import ObihaiConfigEntry from .connectivity import ObihaiConnection -from .const import DOMAIN, LOGGER, OBIHAI +from .const import LOGGER, OBIHAI SCAN_INTERVAL = datetime.timedelta(seconds=5) async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: ObihaiConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the Obihai sensor entries.""" - requester: ObihaiConnection = hass.data[DOMAIN][entry.entry_id] + requester = entry.runtime_data sensors = [ObihaiServiceSensors(requester, key) for key in requester.services]