mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Use runtime_data in blebox (#129070)
This commit is contained in:
parent
3e62c6ae2f
commit
add8db0186
@ -17,9 +17,11 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
from .const import DEFAULT_SETUP_TIMEOUT, DOMAIN, PRODUCT
|
from .const import DEFAULT_SETUP_TIMEOUT
|
||||||
from .helpers import get_maybe_authenticated_session
|
from .helpers import get_maybe_authenticated_session
|
||||||
|
|
||||||
|
type BleBoxConfigEntry = ConfigEntry[Box]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
@ -35,7 +37,7 @@ PLATFORMS = [
|
|||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: BleBoxConfigEntry) -> bool:
|
||||||
"""Set up BleBox devices from a config entry."""
|
"""Set up BleBox devices from a config entry."""
|
||||||
host = entry.data[CONF_HOST]
|
host = entry.data[CONF_HOST]
|
||||||
port = entry.data[CONF_PORT]
|
port = entry.data[CONF_PORT]
|
||||||
@ -55,20 +57,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
_LOGGER.error("Identify failed at %s:%d (%s)", api_host.host, api_host.port, ex)
|
_LOGGER.error("Identify failed at %s:%d (%s)", api_host.host, api_host.port, ex)
|
||||||
raise ConfigEntryNotReady from ex
|
raise ConfigEntryNotReady from ex
|
||||||
|
|
||||||
domain = hass.data.setdefault(DOMAIN, {})
|
entry.runtime_data = product
|
||||||
domain_entry = domain.setdefault(entry.entry_id, {})
|
|
||||||
product = domain_entry.setdefault(PRODUCT, product)
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: BleBoxConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
@ -1,18 +1,16 @@
|
|||||||
"""BleBox binary sensor entities."""
|
"""BleBox binary sensor entities."""
|
||||||
|
|
||||||
from blebox_uniapi.binary_sensor import BinarySensor as BinarySensorFeature
|
from blebox_uniapi.binary_sensor import BinarySensor as BinarySensorFeature
|
||||||
from blebox_uniapi.box import Box
|
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
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, PRODUCT
|
from . import BleBoxConfigEntry
|
||||||
from .entity import BleBoxEntity
|
from .entity import BleBoxEntity
|
||||||
|
|
||||||
BINARY_SENSOR_TYPES = (
|
BINARY_SENSOR_TYPES = (
|
||||||
@ -25,15 +23,13 @@ BINARY_SENSOR_TYPES = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: BleBoxConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a BleBox entry."""
|
"""Set up a BleBox entry."""
|
||||||
|
|
||||||
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
||||||
entities = [
|
entities = [
|
||||||
BleBoxBinarySensorEntity(feature, description)
|
BleBoxBinarySensorEntity(feature, description)
|
||||||
for feature in product.features.get("binary_sensors", [])
|
for feature in config_entry.runtime_data.features.get("binary_sensors", [])
|
||||||
for description in BINARY_SENSOR_TYPES
|
for description in BINARY_SENSOR_TYPES
|
||||||
if description.key == feature.device_class
|
if description.key == feature.device_class
|
||||||
]
|
]
|
||||||
|
@ -2,28 +2,25 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from blebox_uniapi.box import Box
|
|
||||||
import blebox_uniapi.button
|
import blebox_uniapi.button
|
||||||
|
|
||||||
from homeassistant.components.button import ButtonEntity
|
from homeassistant.components.button import ButtonEntity
|
||||||
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, PRODUCT
|
from . import BleBoxConfigEntry
|
||||||
from .entity import BleBoxEntity
|
from .entity import BleBoxEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: BleBoxConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a BleBox button entry."""
|
"""Set up a BleBox button entry."""
|
||||||
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
BleBoxButtonEntity(feature) for feature in product.features.get("buttons", [])
|
BleBoxButtonEntity(feature)
|
||||||
|
for feature in config_entry.runtime_data.features.get("buttons", [])
|
||||||
]
|
]
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from blebox_uniapi.box import Box
|
|
||||||
import blebox_uniapi.climate
|
import blebox_uniapi.climate
|
||||||
|
|
||||||
from homeassistant.components.climate import (
|
from homeassistant.components.climate import (
|
||||||
@ -12,12 +11,11 @@ from homeassistant.components.climate import (
|
|||||||
HVACAction,
|
HVACAction,
|
||||||
HVACMode,
|
HVACMode,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
|
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
|
||||||
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, PRODUCT
|
from . import BleBoxConfigEntry
|
||||||
from .entity import BleBoxEntity
|
from .entity import BleBoxEntity
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=5)
|
SCAN_INTERVAL = timedelta(seconds=5)
|
||||||
@ -39,14 +37,13 @@ BLEBOX_TO_HVACACTION = {
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: BleBoxConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a BleBox climate entity."""
|
"""Set up a BleBox climate entity."""
|
||||||
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
||||||
|
|
||||||
entities = [
|
entities = [
|
||||||
BleBoxClimateEntity(feature) for feature in product.features.get("climates", [])
|
BleBoxClimateEntity(feature)
|
||||||
|
for feature in config_entry.runtime_data.features.get("climates", [])
|
||||||
]
|
]
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""Constants for the BleBox devices integration."""
|
"""Constants for the BleBox devices integration."""
|
||||||
|
|
||||||
DOMAIN = "blebox"
|
DOMAIN = "blebox"
|
||||||
PRODUCT = "product"
|
|
||||||
|
|
||||||
DEFAULT_SETUP_TIMEOUT = 10
|
DEFAULT_SETUP_TIMEOUT = 10
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from blebox_uniapi.box import Box
|
|
||||||
import blebox_uniapi.cover
|
import blebox_uniapi.cover
|
||||||
from blebox_uniapi.cover import BleboxCoverState
|
from blebox_uniapi.cover import BleboxCoverState
|
||||||
|
|
||||||
@ -16,11 +15,10 @@ from homeassistant.components.cover import (
|
|||||||
CoverEntityFeature,
|
CoverEntityFeature,
|
||||||
CoverState,
|
CoverState,
|
||||||
)
|
)
|
||||||
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, PRODUCT
|
from . import BleBoxConfigEntry
|
||||||
from .entity import BleBoxEntity
|
from .entity import BleBoxEntity
|
||||||
|
|
||||||
BLEBOX_TO_COVER_DEVICE_CLASSES = {
|
BLEBOX_TO_COVER_DEVICE_CLASSES = {
|
||||||
@ -46,13 +44,13 @@ BLEBOX_TO_HASS_COVER_STATES = {
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: BleBoxConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a BleBox entry."""
|
"""Set up a BleBox entry."""
|
||||||
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
||||||
entities = [
|
entities = [
|
||||||
BleBoxCoverEntity(feature) for feature in product.features.get("covers", [])
|
BleBoxCoverEntity(feature)
|
||||||
|
for feature in config_entry.runtime_data.features.get("covers", [])
|
||||||
]
|
]
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ from datetime import timedelta
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from blebox_uniapi.box import Box
|
|
||||||
import blebox_uniapi.light
|
import blebox_uniapi.light
|
||||||
from blebox_uniapi.light import BleboxColorMode
|
from blebox_uniapi.light import BleboxColorMode
|
||||||
|
|
||||||
@ -21,11 +20,10 @@ from homeassistant.components.light import (
|
|||||||
LightEntity,
|
LightEntity,
|
||||||
LightEntityFeature,
|
LightEntityFeature,
|
||||||
)
|
)
|
||||||
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, PRODUCT
|
from . import BleBoxConfigEntry
|
||||||
from .entity import BleBoxEntity
|
from .entity import BleBoxEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -35,13 +33,13 @@ SCAN_INTERVAL = timedelta(seconds=5)
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: BleBoxConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a BleBox entry."""
|
"""Set up a BleBox entry."""
|
||||||
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
||||||
entities = [
|
entities = [
|
||||||
BleBoxLightEntity(feature) for feature in product.features.get("lights", [])
|
BleBoxLightEntity(feature)
|
||||||
|
for feature in config_entry.runtime_data.features.get("lights", [])
|
||||||
]
|
]
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
"""BleBox sensor entities."""
|
"""BleBox sensor entities."""
|
||||||
|
|
||||||
from blebox_uniapi.box import Box
|
|
||||||
import blebox_uniapi.sensor
|
import blebox_uniapi.sensor
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
@ -9,7 +8,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||||
LIGHT_LUX,
|
LIGHT_LUX,
|
||||||
@ -27,7 +25,7 @@ from homeassistant.const import (
|
|||||||
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, PRODUCT
|
from . import BleBoxConfigEntry
|
||||||
from .entity import BleBoxEntity
|
from .entity import BleBoxEntity
|
||||||
|
|
||||||
SENSOR_TYPES = (
|
SENSOR_TYPES = (
|
||||||
@ -117,14 +115,13 @@ SENSOR_TYPES = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: BleBoxConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a BleBox entry."""
|
"""Set up a BleBox entry."""
|
||||||
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
||||||
entities = [
|
entities = [
|
||||||
BleBoxSensorEntity(feature, description)
|
BleBoxSensorEntity(feature, description)
|
||||||
for feature in product.features.get("sensors", [])
|
for feature in config_entry.runtime_data.features.get("sensors", [])
|
||||||
for description in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
if description.key == feature.device_class
|
if description.key == feature.device_class
|
||||||
]
|
]
|
||||||
|
@ -3,15 +3,13 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from blebox_uniapi.box import Box
|
|
||||||
import blebox_uniapi.switch
|
import blebox_uniapi.switch
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
||||||
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, PRODUCT
|
from . import BleBoxConfigEntry
|
||||||
from .entity import BleBoxEntity
|
from .entity import BleBoxEntity
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=5)
|
SCAN_INTERVAL = timedelta(seconds=5)
|
||||||
@ -19,13 +17,13 @@ SCAN_INTERVAL = timedelta(seconds=5)
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: BleBoxConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a BleBox switch entity."""
|
"""Set up a BleBox switch entity."""
|
||||||
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
||||||
entities = [
|
entities = [
|
||||||
BleBoxSwitchEntity(feature) for feature in product.features.get("switches", [])
|
BleBoxSwitchEntity(feature)
|
||||||
|
for feature in config_entry.runtime_data.features.get("switches", [])
|
||||||
]
|
]
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import logging
|
|||||||
import blebox_uniapi
|
import blebox_uniapi
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.blebox.const import DOMAIN
|
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
@ -57,10 +56,10 @@ async def test_unload_config_entry(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert hass.data[DOMAIN]
|
assert hasattr(entry, "runtime_data")
|
||||||
|
|
||||||
await hass.config_entries.async_unload(entry.entry_id)
|
await hass.config_entries.async_unload(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert not hass.data.get(DOMAIN)
|
assert not hasattr(entry, "runtime_data")
|
||||||
|
|
||||||
assert entry.state is ConfigEntryState.NOT_LOADED
|
assert entry.state is ConfigEntryState.NOT_LOADED
|
||||||
|
Loading…
x
Reference in New Issue
Block a user