1 """The Bond integration."""
3 from http
import HTTPStatus
7 from aiohttp
import ClientError, ClientResponseError, ClientTimeout
8 from bond_async
import Bond, BPUPSubscriptions, start_bpup
14 EVENT_HOMEASSISTANT_STOP,
23 from .const
import BRIDGE_MAKE, DOMAIN
24 from .models
import BondData
25 from .utils
import BondHub
34 _API_TIMEOUT = SLOW_UPDATE_WARNING - 1
36 _LOGGER = logging.getLogger(__name__)
38 type BondConfigEntry = ConfigEntry[BondData]
42 """Set up Bond from a config entry."""
43 host = entry.data[CONF_HOST]
44 token = entry.data[CONF_ACCESS_TOKEN]
45 config_entry_id = entry.entry_id
50 timeout=ClientTimeout(total=_API_TIMEOUT),
56 except ClientResponseError
as ex:
57 if ex.status == HTTPStatus.UNAUTHORIZED:
58 _LOGGER.error(
"Bond token no longer valid: %s", ex)
60 raise ConfigEntryNotReady
from ex
61 except (ClientError, TimeoutError, OSError)
as error:
62 raise ConfigEntryNotReady
from error
64 bpup_subs = BPUPSubscriptions()
65 stop_bpup = await start_bpup(host, bpup_subs)
68 def _async_stop_event(*_: Any) ->
None:
71 entry.async_on_unload(_async_stop_event)
72 entry.async_on_unload(
73 hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_stop_event)
75 entry.runtime_data =
BondData(hub, bpup_subs)
77 if not entry.unique_id:
78 hass.config_entries.async_update_entry(entry, unique_id=hub.bond_id)
80 assert hub.bond_id
is not None
81 hub_name = hub.name
or hub.bond_id
82 device_registry = dr.async_get(hass)
83 device_registry.async_get_or_create(
84 config_entry_id=config_entry_id,
85 identifiers={(DOMAIN, hub.bond_id)},
86 manufacturer=BRIDGE_MAKE,
89 sw_version=hub.fw_ver,
90 hw_version=hub.mcu_ver,
91 suggested_area=hub.location,
92 configuration_url=f
"http://{host}",
97 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
103 """Unload a config entry."""
104 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
109 config_entry_id: str, device_registry: dr.DeviceRegistry, hub: BondHub
111 """Remove the non-unique device registry entries."""
112 for device
in hub.devices:
113 dev = device_registry.async_get_device(identifiers={(DOMAIN, device.device_id)})
116 if config_entry_id
in dev.config_entries:
117 device_registry.async_remove_device(dev.id)
121 hass: HomeAssistant, config_entry: BondConfigEntry, device_entry: dr.DeviceEntry
123 """Remove bond config entry from a device."""
124 data = config_entry.runtime_data
126 for identifier
in device_entry.identifiers:
127 if identifier[0] != DOMAIN
or len(identifier) != 3:
129 bond_id: str = identifier[1]
132 device_id: str = identifier[2]
135 if hub.bond_id != bond_id
or not any(
136 device_id == device.device_id
for device
in hub.devices
bool async_unload_entry(HomeAssistant hass, BondConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, BondConfigEntry entry)
bool async_remove_config_entry_device(HomeAssistant hass, BondConfigEntry config_entry, dr.DeviceEntry device_entry)
None _async_remove_old_device_identifiers(str config_entry_id, dr.DeviceRegistry device_registry, BondHub hub)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)