mirror of
https://github.com/home-assistant/core
synced 2024-09-03 08:14:07 +02:00
Switch bond data to use a dataclass (#73514)
This commit is contained in:
parent
7a792b093f
commit
0a27211356
@ -20,7 +20,8 @@ from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.entity import SLOW_UPDATE_WARNING
|
||||
|
||||
from .const import BPUP_SUBS, BRIDGE_MAKE, DOMAIN, HUB
|
||||
from .const import BRIDGE_MAKE, DOMAIN
|
||||
from .models import BondData
|
||||
from .utils import BondHub
|
||||
|
||||
PLATFORMS = [
|
||||
@ -69,11 +70,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
entry.async_on_unload(
|
||||
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_stop_event)
|
||||
)
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = {
|
||||
HUB: hub,
|
||||
BPUP_SUBS: bpup_subs,
|
||||
}
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = BondData(hub, bpup_subs)
|
||||
|
||||
if not entry.unique_id:
|
||||
hass.config_entries.async_update_entry(entry, unique_id=hub.bond_id)
|
||||
@ -102,8 +99,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return unload_ok
|
||||
|
||||
@ -125,7 +121,8 @@ async def async_remove_config_entry_device(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry
|
||||
) -> bool:
|
||||
"""Remove bond config entry from a device."""
|
||||
hub: BondHub = hass.data[DOMAIN][config_entry.entry_id][HUB]
|
||||
data: BondData = hass.data[DOMAIN][config_entry.entry_id]
|
||||
hub = data.hub
|
||||
for identifier in device_entry.identifiers:
|
||||
if identifier[0] != DOMAIN or len(identifier) != 3:
|
||||
continue
|
||||
|
@ -2,7 +2,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from bond_async import Action, BPUPSubscriptions
|
||||
@ -12,12 +11,11 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import BPUP_SUBS, DOMAIN, HUB
|
||||
from .const import DOMAIN
|
||||
from .entity import BondEntity
|
||||
from .models import BondData
|
||||
from .utils import BondDevice, BondHub
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# The api requires a step size even though it does not
|
||||
# seem to matter what is is as the underlying device is likely
|
||||
# getting an increase/decrease signal only
|
||||
@ -246,9 +244,9 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Bond button devices."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
hub: BondHub = data[HUB]
|
||||
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
||||
hub = data.hub
|
||||
bpup_subs = data.bpup_subs
|
||||
entities: list[BondButtonEntity] = []
|
||||
|
||||
for device in hub.devices:
|
||||
|
@ -7,9 +7,6 @@ DOMAIN = "bond"
|
||||
CONF_BOND_ID: str = "bond_id"
|
||||
|
||||
|
||||
HUB = "hub"
|
||||
BPUP_SUBS = "bpup_subs"
|
||||
|
||||
SERVICE_SET_FAN_SPEED_TRACKED_STATE = "set_fan_speed_tracked_state"
|
||||
SERVICE_SET_POWER_TRACKED_STATE = "set_switch_power_tracked_state"
|
||||
SERVICE_SET_LIGHT_POWER_TRACKED_STATE = "set_light_power_tracked_state"
|
||||
|
@ -15,8 +15,9 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import BPUP_SUBS, DOMAIN, HUB
|
||||
from .const import DOMAIN
|
||||
from .entity import BondEntity
|
||||
from .models import BondData
|
||||
from .utils import BondDevice, BondHub
|
||||
|
||||
|
||||
@ -36,9 +37,10 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Bond cover devices."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
hub: BondHub = data[HUB]
|
||||
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
||||
hub = data.hub
|
||||
bpup_subs = data.bpup_subs
|
||||
|
||||
async_add_entities(
|
||||
BondCover(hub, device, bpup_subs)
|
||||
for device in hub.devices
|
||||
|
@ -7,8 +7,8 @@ from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN, HUB
|
||||
from .utils import BondHub
|
||||
from .const import DOMAIN
|
||||
from .models import BondData
|
||||
|
||||
TO_REDACT = {"access_token"}
|
||||
|
||||
@ -17,8 +17,8 @@ async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
hub: BondHub = data[HUB]
|
||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
||||
hub = data.hub
|
||||
return {
|
||||
"entry": {
|
||||
"title": entry.title,
|
||||
|
@ -26,8 +26,9 @@ from homeassistant.util.percentage import (
|
||||
ranged_value_to_percentage,
|
||||
)
|
||||
|
||||
from .const import BPUP_SUBS, DOMAIN, HUB, SERVICE_SET_FAN_SPEED_TRACKED_STATE
|
||||
from .const import DOMAIN, SERVICE_SET_FAN_SPEED_TRACKED_STATE
|
||||
from .entity import BondEntity
|
||||
from .models import BondData
|
||||
from .utils import BondDevice, BondHub
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -41,9 +42,9 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Bond fan devices."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
hub: BondHub = data[HUB]
|
||||
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
||||
hub = data.hub
|
||||
bpup_subs = data.bpup_subs
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_SET_FAN_SPEED_TRACKED_STATE,
|
||||
|
@ -18,13 +18,12 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import (
|
||||
ATTR_POWER_STATE,
|
||||
BPUP_SUBS,
|
||||
DOMAIN,
|
||||
HUB,
|
||||
SERVICE_SET_LIGHT_BRIGHTNESS_TRACKED_STATE,
|
||||
SERVICE_SET_LIGHT_POWER_TRACKED_STATE,
|
||||
)
|
||||
from .entity import BondEntity
|
||||
from .models import BondData
|
||||
from .utils import BondDevice, BondHub
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -46,9 +45,9 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Bond light devices."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
hub: BondHub = data[HUB]
|
||||
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
||||
hub = data.hub
|
||||
bpup_subs = data.bpup_subs
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
16
homeassistant/components/bond/models.py
Normal file
16
homeassistant/components/bond/models.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""The bond integration models."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from bond_async import BPUPSubscriptions
|
||||
|
||||
from .utils import BondHub
|
||||
|
||||
|
||||
@dataclass
|
||||
class BondData:
|
||||
"""Data for the bond integration."""
|
||||
|
||||
hub: BondHub
|
||||
bpup_subs: BPUPSubscriptions
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
from aiohttp.client_exceptions import ClientResponseError
|
||||
from bond_async import Action, BPUPSubscriptions, DeviceType
|
||||
from bond_async import Action, DeviceType
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
@ -14,15 +14,9 @@ from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import (
|
||||
ATTR_POWER_STATE,
|
||||
BPUP_SUBS,
|
||||
DOMAIN,
|
||||
HUB,
|
||||
SERVICE_SET_POWER_TRACKED_STATE,
|
||||
)
|
||||
from .const import ATTR_POWER_STATE, DOMAIN, SERVICE_SET_POWER_TRACKED_STATE
|
||||
from .entity import BondEntity
|
||||
from .utils import BondHub
|
||||
from .models import BondData
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@ -31,9 +25,9 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Bond generic devices."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
hub: BondHub = data[HUB]
|
||||
bpup_subs: BPUPSubscriptions = data[BPUP_SUBS]
|
||||
data: BondData = hass.data[DOMAIN][entry.entry_id]
|
||||
hub = data.hub
|
||||
bpup_subs = data.bpup_subs
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_SET_POWER_TRACKED_STATE,
|
||||
|
Loading…
Reference in New Issue
Block a user