mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 00:20:30 +01:00
Use runtime_data in obihai integration (#167037)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user