Use runtime_data for bond (#116611)
This commit is contained in:
parent
6bb2ab519f
commit
ef242f2883
8 changed files with 30 additions and 44 deletions
|
@ -35,8 +35,10 @@ _API_TIMEOUT = SLOW_UPDATE_WARNING - 1
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
BondConfigEntry = ConfigEntry[BondData]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: BondConfigEntry) -> bool:
|
||||||
"""Set up Bond from a config entry."""
|
"""Set up Bond from a config entry."""
|
||||||
host = entry.data[CONF_HOST]
|
host = entry.data[CONF_HOST]
|
||||||
token = entry.data[CONF_ACCESS_TOKEN]
|
token = entry.data[CONF_ACCESS_TOKEN]
|
||||||
|
@ -70,7 +72,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
entry.async_on_unload(
|
entry.async_on_unload(
|
||||||
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_stop_event)
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_stop_event)
|
||||||
)
|
)
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = BondData(hub, bpup_subs)
|
entry.runtime_data = BondData(hub, bpup_subs)
|
||||||
|
|
||||||
if not entry.unique_id:
|
if not entry.unique_id:
|
||||||
hass.config_entries.async_update_entry(entry, unique_id=hub.bond_id)
|
hass.config_entries.async_update_entry(entry, unique_id=hub.bond_id)
|
||||||
|
@ -97,11 +99,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: BondConfigEntry) -> 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
|
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -118,10 +118,10 @@ def _async_remove_old_device_identifiers(
|
||||||
|
|
||||||
|
|
||||||
async def async_remove_config_entry_device(
|
async def async_remove_config_entry_device(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry
|
hass: HomeAssistant, config_entry: BondConfigEntry, device_entry: dr.DeviceEntry
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Remove bond config entry from a device."""
|
"""Remove bond config entry from a device."""
|
||||||
data: BondData = hass.data[DOMAIN][config_entry.entry_id]
|
data = config_entry.runtime_data
|
||||||
hub = data.hub
|
hub = data.hub
|
||||||
for identifier in device_entry.identifiers:
|
for identifier in device_entry.identifiers:
|
||||||
if identifier[0] != DOMAIN or len(identifier) != 3:
|
if identifier[0] != DOMAIN or len(identifier) != 3:
|
||||||
|
|
|
@ -7,13 +7,11 @@ from dataclasses import dataclass
|
||||||
from bond_async import Action, BPUPSubscriptions
|
from bond_async import Action, BPUPSubscriptions
|
||||||
|
|
||||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import BondConfigEntry
|
||||||
from .entity import BondEntity
|
from .entity import BondEntity
|
||||||
from .models import BondData
|
|
||||||
from .utils import BondDevice, BondHub
|
from .utils import BondDevice, BondHub
|
||||||
|
|
||||||
# The api requires a step size even though it does not
|
# The api requires a step size even though it does not
|
||||||
|
@ -243,11 +241,11 @@ BUTTONS: tuple[BondButtonEntityDescription, ...] = (
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: BondConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Bond button devices."""
|
"""Set up Bond button devices."""
|
||||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
data = entry.runtime_data
|
||||||
hub = data.hub
|
hub = data.hub
|
||||||
bpup_subs = data.bpup_subs
|
bpup_subs = data.bpup_subs
|
||||||
entities: list[BondButtonEntity] = []
|
entities: list[BondButtonEntity] = []
|
||||||
|
|
|
@ -12,13 +12,11 @@ from homeassistant.components.cover import (
|
||||||
CoverEntity,
|
CoverEntity,
|
||||||
CoverEntityFeature,
|
CoverEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import BondConfigEntry
|
||||||
from .entity import BondEntity
|
from .entity import BondEntity
|
||||||
from .models import BondData
|
|
||||||
from .utils import BondDevice, BondHub
|
from .utils import BondDevice, BondHub
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,11 +32,11 @@ def _hass_to_bond_position(hass_position: int) -> int:
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: BondConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Bond cover devices."""
|
"""Set up Bond cover devices."""
|
||||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
data = entry.runtime_data
|
||||||
hub = data.hub
|
hub = data.hub
|
||||||
bpup_subs = data.bpup_subs
|
bpup_subs = data.bpup_subs
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,18 @@ from __future__ import annotations
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import BondConfigEntry
|
||||||
from .models import BondData
|
|
||||||
|
|
||||||
TO_REDACT = {"access_token"}
|
TO_REDACT = {"access_token"}
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: BondConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
data = entry.runtime_data
|
||||||
hub = data.hub
|
hub = data.hub
|
||||||
return {
|
return {
|
||||||
"entry": {
|
"entry": {
|
||||||
|
|
|
@ -16,7 +16,6 @@ from homeassistant.components.fan import (
|
||||||
FanEntity,
|
FanEntity,
|
||||||
FanEntityFeature,
|
FanEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
|
@ -27,9 +26,9 @@ from homeassistant.util.percentage import (
|
||||||
)
|
)
|
||||||
from homeassistant.util.scaling import int_states_in_range
|
from homeassistant.util.scaling import int_states_in_range
|
||||||
|
|
||||||
from .const import DOMAIN, SERVICE_SET_FAN_SPEED_TRACKED_STATE
|
from . import BondConfigEntry
|
||||||
|
from .const import SERVICE_SET_FAN_SPEED_TRACKED_STATE
|
||||||
from .entity import BondEntity
|
from .entity import BondEntity
|
||||||
from .models import BondData
|
|
||||||
from .utils import BondDevice, BondHub
|
from .utils import BondDevice, BondHub
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -39,11 +38,11 @@ PRESET_MODE_BREEZE = "Breeze"
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: BondConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Bond fan devices."""
|
"""Set up Bond fan devices."""
|
||||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
data = entry.runtime_data
|
||||||
hub = data.hub
|
hub = data.hub
|
||||||
bpup_subs = data.bpup_subs
|
bpup_subs = data.bpup_subs
|
||||||
platform = entity_platform.async_get_current_platform()
|
platform = entity_platform.async_get_current_platform()
|
||||||
|
|
|
@ -10,21 +10,19 @@ from bond_async import Action, BPUPSubscriptions, DeviceType
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
|
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import BondConfigEntry
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_POWER_STATE,
|
ATTR_POWER_STATE,
|
||||||
DOMAIN,
|
|
||||||
SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE,
|
SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE,
|
||||||
SERVICE_SET_LIGHT_POWER_TRACKED_STATE,
|
SERVICE_SET_LIGHT_POWER_TRACKED_STATE,
|
||||||
)
|
)
|
||||||
from .entity import BondEntity
|
from .entity import BondEntity
|
||||||
from .models import BondData
|
|
||||||
from .utils import BondDevice, BondHub
|
from .utils import BondDevice, BondHub
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -42,11 +40,11 @@ ENTITY_SERVICES = [
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: BondConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Bond light devices."""
|
"""Set up Bond light devices."""
|
||||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
data = entry.runtime_data
|
||||||
hub = data.hub
|
hub = data.hub
|
||||||
bpup_subs = data.bpup_subs
|
bpup_subs = data.bpup_subs
|
||||||
platform = entity_platform.async_get_current_platform()
|
platform = entity_platform.async_get_current_platform()
|
||||||
|
|
|
@ -9,24 +9,23 @@ from bond_async import Action, DeviceType
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import ATTR_POWER_STATE, DOMAIN, SERVICE_SET_POWER_TRACKED_STATE
|
from . import BondConfigEntry
|
||||||
|
from .const import ATTR_POWER_STATE, SERVICE_SET_POWER_TRACKED_STATE
|
||||||
from .entity import BondEntity
|
from .entity import BondEntity
|
||||||
from .models import BondData
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: BondConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Bond generic devices."""
|
"""Set up Bond generic devices."""
|
||||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
data = entry.runtime_data
|
||||||
hub = data.hub
|
hub = data.hub
|
||||||
bpup_subs = data.bpup_subs
|
bpup_subs = data.bpup_subs
|
||||||
platform = entity_platform.async_get_current_platform()
|
platform = entity_platform.async_get_current_platform()
|
||||||
|
|
|
@ -6,7 +6,7 @@ from aiohttp import ClientConnectionError, ClientResponseError
|
||||||
from bond_async import DeviceType
|
from bond_async import DeviceType
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.bond.const import DOMAIN
|
from homeassistant.components.bond import DOMAIN, BondData
|
||||||
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
|
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import ATTR_ASSUMED_STATE, CONF_ACCESS_TOKEN, CONF_HOST
|
from homeassistant.const import ATTR_ASSUMED_STATE, CONF_ACCESS_TOKEN, CONF_HOST
|
||||||
|
@ -107,7 +107,7 @@ async def test_async_setup_entry_sets_up_hub_and_supported_domains(
|
||||||
assert result is True
|
assert result is True
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert config_entry.entry_id in hass.data[DOMAIN]
|
assert isinstance(config_entry.runtime_data, BondData)
|
||||||
assert config_entry.state is ConfigEntryState.LOADED
|
assert config_entry.state is ConfigEntryState.LOADED
|
||||||
assert config_entry.unique_id == "ZXXX12345"
|
assert config_entry.unique_id == "ZXXX12345"
|
||||||
|
|
||||||
|
@ -148,7 +148,6 @@ async def test_unload_config_entry(hass: HomeAssistant) -> None:
|
||||||
await hass.config_entries.async_unload(config_entry.entry_id)
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert config_entry.entry_id not in hass.data[DOMAIN]
|
|
||||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||||
|
|
||||||
|
|
||||||
|
@ -194,7 +193,6 @@ async def test_old_identifiers_are_removed(
|
||||||
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
|
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert config_entry.entry_id in hass.data[DOMAIN]
|
|
||||||
assert config_entry.state is ConfigEntryState.LOADED
|
assert config_entry.state is ConfigEntryState.LOADED
|
||||||
assert config_entry.unique_id == "ZXXX12345"
|
assert config_entry.unique_id == "ZXXX12345"
|
||||||
|
|
||||||
|
@ -238,7 +236,6 @@ async def test_smart_by_bond_device_suggested_area(
|
||||||
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
|
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert config_entry.entry_id in hass.data[DOMAIN]
|
|
||||||
assert config_entry.state is ConfigEntryState.LOADED
|
assert config_entry.state is ConfigEntryState.LOADED
|
||||||
assert config_entry.unique_id == "KXXX12345"
|
assert config_entry.unique_id == "KXXX12345"
|
||||||
|
|
||||||
|
@ -287,7 +284,6 @@ async def test_bridge_device_suggested_area(
|
||||||
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
|
assert await hass.config_entries.async_setup(config_entry.entry_id) is True
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert config_entry.entry_id in hass.data[DOMAIN]
|
|
||||||
assert config_entry.state is ConfigEntryState.LOADED
|
assert config_entry.state is ConfigEntryState.LOADED
|
||||||
assert config_entry.unique_id == "ZXXX12345"
|
assert config_entry.unique_id == "ZXXX12345"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue