Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Utility functions for the Reolink component."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from homeassistant import config_entries
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers import device_registry as dr
10 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
11 
12 from .const import DOMAIN
13 from .host import ReolinkHost
14 
15 type ReolinkConfigEntry = config_entries.ConfigEntry[ReolinkData]
16 
17 
18 @dataclass
20  """Data for the Reolink integration."""
21 
22  host: ReolinkHost
23  device_coordinator: DataUpdateCoordinator[None]
24  firmware_coordinator: DataUpdateCoordinator[None]
25 
26 
27 def is_connected(hass: HomeAssistant, config_entry: config_entries.ConfigEntry) -> bool:
28  """Check if an existing entry has a proper connection."""
29  return (
30  hasattr(config_entry, "runtime_data")
31  and config_entry.state == config_entries.ConfigEntryState.LOADED
32  and config_entry.runtime_data.device_coordinator.last_update_success
33  )
34 
35 
37  device: dr.DeviceEntry, host: ReolinkHost
38 ) -> tuple[list[str], int | None, bool]:
39  """Get the channel and the split device_uid from a reolink DeviceEntry."""
40  device_uid = [
41  dev_id[1].split("_") for dev_id in device.identifiers if dev_id[0] == DOMAIN
42  ][0]
43 
44  is_chime = False
45  if len(device_uid) < 2:
46  # NVR itself
47  ch = None
48  elif device_uid[1].startswith("ch") and len(device_uid[1]) <= 5:
49  ch = int(device_uid[1][2:])
50  elif device_uid[1].startswith("chime"):
51  ch = int(device_uid[1][5:])
52  is_chime = True
53  else:
54  ch = host.api.channel_for_uid(device_uid[1])
55  return (device_uid, ch, is_chime)