Home Assistant Unofficial Reference 2024.12.1
data.py
Go to the documentation of this file.
1 """Rainforest data."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 import logging
7 
8 import aioeagle
9 import aiohttp
10 from eagle100 import Eagle as Eagle100Reader
11 from requests.exceptions import ConnectionError as ConnectError, HTTPError, Timeout
12 
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers import aiohttp_client
15 
16 from .const import TYPE_EAGLE_100, TYPE_EAGLE_200
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 UPDATE_100_ERRORS = (ConnectError, HTTPError, Timeout)
21 
22 
24  """Base error."""
25 
26 
28  """Error to indicate a request failed."""
29 
30 
32  """Error to indicate bad auth."""
33 
34 
35 async def async_get_type(hass, cloud_id, install_code, host):
36  """Try API call 'get_network_info' to see if target device is Eagle-100 or Eagle-200."""
37  # For EAGLE-200, fetch the hardware address of the meter too.
38  hub = aioeagle.EagleHub(
39  aiohttp_client.async_get_clientsession(hass), cloud_id, install_code, host=host
40  )
41 
42  try:
43  async with asyncio.timeout(30):
44  meters = await hub.get_device_list()
45  except aioeagle.BadAuth as err:
46  raise InvalidAuth from err
47  except (KeyError, aiohttp.ClientError):
48  # This can happen if it's an eagle-100
49  meters = None
50 
51  if meters is not None:
52  if meters:
53  hardware_address = meters[0].hardware_address
54  else:
55  hardware_address = None
56 
57  return TYPE_EAGLE_200, hardware_address
58 
59  reader = Eagle100Reader(cloud_id, install_code, host)
60 
61  try:
62  response = await hass.async_add_executor_job(reader.get_network_info)
63  except ValueError as err:
64  # This could be invalid auth because it doesn't check 401 and tries to read JSON.
65  raise InvalidAuth from err
66  except UPDATE_100_ERRORS as error:
67  _LOGGER.error("Failed to connect during setup: %s", error)
68  raise CannotConnect from error
69 
70  # Branch to test if target is Legacy Model
71  if (
72  "NetworkInfo" in response
73  and response["NetworkInfo"].get("ModelId") == "Z109-EAGLE"
74  ):
75  return TYPE_EAGLE_100, None
76 
77  return None, None
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
def async_get_type(hass, cloud_id, install_code, host)
Definition: data.py:35