Home Assistant Unofficial Reference 2024.12.1
config.py
Go to the documentation of this file.
1 """UniFi Network config entry abstraction."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from datetime import timedelta
7 import ssl
8 from typing import Literal, Self
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import (
12  CONF_HOST,
13  CONF_PASSWORD,
14  CONF_PORT,
15  CONF_USERNAME,
16  CONF_VERIFY_SSL,
17 )
18 
19 from ..const import (
20  CONF_ALLOW_BANDWIDTH_SENSORS,
21  CONF_ALLOW_UPTIME_SENSORS,
22  CONF_BLOCK_CLIENT,
23  CONF_CLIENT_SOURCE,
24  CONF_DETECTION_TIME,
25  CONF_DPI_RESTRICTIONS,
26  CONF_IGNORE_WIRED_BUG,
27  CONF_SITE_ID,
28  CONF_SSID_FILTER,
29  CONF_TRACK_CLIENTS,
30  CONF_TRACK_DEVICES,
31  CONF_TRACK_WIRED_CLIENTS,
32  DEFAULT_ALLOW_BANDWIDTH_SENSORS,
33  DEFAULT_ALLOW_UPTIME_SENSORS,
34  DEFAULT_DETECTION_TIME,
35  DEFAULT_DPI_RESTRICTIONS,
36  DEFAULT_IGNORE_WIRED_BUG,
37  DEFAULT_TRACK_CLIENTS,
38  DEFAULT_TRACK_DEVICES,
39  DEFAULT_TRACK_WIRED_CLIENTS,
40 )
41 
42 
43 @dataclass
45  """Represent a UniFi config entry."""
46 
47  entry: ConfigEntry
48 
49  host: str
50  port: int
51  username: str
52  password: str
53  site: str
54  ssl_context: ssl.SSLContext | Literal[False]
55 
56  option_supported_clients: list[str]
57  """Allow creating entities from clients."""
58 
59  # Device tracker options
60 
61  option_track_clients: list[str]
62  """Config entry option to not track clients."""
63  option_track_wired_clients: list[str]
64  """Config entry option to not track wired clients."""
65  option_track_devices: bool
66  """Config entry option to not track devices."""
67  option_ssid_filter: set[str]
68  """Config entry option listing what SSIDs are being used to track clients."""
69  option_detection_time: timedelta
70  """Config entry option defining number of seconds from last seen to away"""
71  option_ignore_wired_bug: bool
72  """Config entry option to ignore wired bug."""
73 
74  # Client control options
75 
76  option_block_clients: list[str]
77  """Config entry option with list of clients to control network access."""
78  option_dpi_restrictions: bool
79  """Config entry option to control DPI restriction groups."""
80 
81  # Statistics sensor options
82 
83  option_allow_bandwidth_sensors: bool
84  """Config entry option to allow bandwidth sensors."""
85  option_allow_uptime_sensors: bool
86  """Config entry option to allow uptime sensors."""
87 
88  @classmethod
89  def from_config_entry(cls, config_entry: ConfigEntry) -> Self:
90  """Create object from config entry."""
91  config = config_entry.data
92  options = config_entry.options
93  return cls(
94  entry=config_entry,
95  host=config[CONF_HOST],
96  username=config[CONF_USERNAME],
97  password=config[CONF_PASSWORD],
98  port=config[CONF_PORT],
99  site=config[CONF_SITE_ID],
100  ssl_context=config.get(CONF_VERIFY_SSL, False),
101  option_supported_clients=options.get(CONF_CLIENT_SOURCE, []),
102  option_track_clients=options.get(CONF_TRACK_CLIENTS, DEFAULT_TRACK_CLIENTS),
103  option_track_wired_clients=options.get(
104  CONF_TRACK_WIRED_CLIENTS, DEFAULT_TRACK_WIRED_CLIENTS
105  ),
106  option_track_devices=options.get(CONF_TRACK_DEVICES, DEFAULT_TRACK_DEVICES),
107  option_ssid_filter=set(options.get(CONF_SSID_FILTER, [])),
108  option_detection_time=timedelta(
109  seconds=options.get(CONF_DETECTION_TIME, DEFAULT_DETECTION_TIME)
110  ),
111  option_ignore_wired_bug=options.get(
112  CONF_IGNORE_WIRED_BUG, DEFAULT_IGNORE_WIRED_BUG
113  ),
114  option_block_clients=options.get(CONF_BLOCK_CLIENT, []),
115  option_dpi_restrictions=options.get(
116  CONF_DPI_RESTRICTIONS, DEFAULT_DPI_RESTRICTIONS
117  ),
118  option_allow_bandwidth_sensors=options.get(
119  CONF_ALLOW_BANDWIDTH_SENSORS, DEFAULT_ALLOW_BANDWIDTH_SENSORS
120  ),
121  option_allow_uptime_sensors=options.get(
122  CONF_ALLOW_UPTIME_SENSORS, DEFAULT_ALLOW_UPTIME_SENSORS
123  ),
124  )
Self from_config_entry(cls, ConfigEntry config_entry)
Definition: config.py:89