Home Assistant Unofficial Reference 2024.12.1
connectivity.py
Go to the documentation of this file.
1 """Support for Obihai Connectivity."""
2 
3 from __future__ import annotations
4 
5 from pyobihai import PyObihai
6 
7 from .const import DEFAULT_PASSWORD, DEFAULT_USERNAME, LOGGER
8 
9 
11  host: str,
12  username: str,
13  password: str,
14 ) -> PyObihai:
15  """Retrieve an authenticated PyObihai."""
16 
17  return PyObihai(host, username, password)
18 
19 
21  host: str,
22  username: str,
23  password: str,
24 ) -> PyObihai | None:
25  """Test if the given setting works as expected."""
26 
27  obi = get_pyobihai(host, username, password)
28 
29  login = obi.check_account()
30  if not login:
31  LOGGER.debug("Invalid credentials")
32  return None
33 
34  return obi
35 
36 
38  """Contains a list of Obihai Sensors."""
39 
40  def __init__(
41  self,
42  host: str,
43  username: str = DEFAULT_USERNAME,
44  password: str = DEFAULT_PASSWORD,
45  ) -> None:
46  """Store configuration."""
47  self.sensors: list = []
48  self.hosthost = host
49  self.usernameusername = username
50  self.passwordpassword = password
51  self.serialserial: str
52  self.servicesservices: list = []
53  self.line_servicesline_services: list = []
54  self.call_directioncall_direction: list = []
55  self.pyobihaipyobihai: PyObihai = None
56  self.available: bool = True
57 
58  def update(self) -> bool:
59  """Validate connection and retrieve a list of sensors."""
60 
61  if not self.pyobihaipyobihai:
62  self.pyobihaipyobihai = validate_auth(self.hosthost, self.usernameusername, self.passwordpassword)
63 
64  if not self.pyobihaipyobihai:
65  return False
66 
67  self.serialserial = self.pyobihaipyobihai.get_device_serial()
68  self.servicesservices = self.pyobihaipyobihai.get_state()
69  self.line_servicesline_services = self.pyobihaipyobihai.get_line_state()
70  self.call_directioncall_direction = self.pyobihaipyobihai.get_call_direction()
71 
72  return True
None __init__(self, str host, str username=DEFAULT_USERNAME, str password=DEFAULT_PASSWORD)
Definition: connectivity.py:45
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26
PyObihai|None validate_auth(str host, str username, str password)
Definition: connectivity.py:24
PyObihai get_pyobihai(str host, str username, str password)
Definition: connectivity.py:14
str|None get_device_serial(PyViCareDevice device)
Definition: utils.py:35