Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Coordinate data for powerview devices."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 from typing import Any
7 
8 from aiopvapi.helpers.aiorequest import AioRequest
9 from aiopvapi.helpers.constants import ATTR_ID
10 from aiopvapi.hub import Hub
11 
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .model import PowerviewAPI, PowerviewDeviceInfo
16 
17 
18 @callback
19 def async_map_data_by_id(data: Iterable[dict[str | int, Any]]):
20  """Return a dict with the key being the id for a list of entries."""
21  return {entry[ATTR_ID]: entry for entry in data}
22 
23 
25  hass: HomeAssistant, address: str, api_version: int | None = None
26 ) -> PowerviewAPI:
27  """Create the hub and fetch the device info address."""
28  websession = async_get_clientsession(hass)
29  pv_request = AioRequest(
30  address, loop=hass.loop, websession=websession, api_version=api_version
31  )
32  hub = Hub(pv_request)
33  await hub.query_firmware()
34  info = PowerviewDeviceInfo(
35  name=hub.name,
36  mac_address=hub.mac_address,
37  serial_number=hub.serial_number,
38  firmware=hub.firmware,
39  model=hub.model,
40  hub_address=hub.ip,
41  )
42  return PowerviewAPI(hub, pv_request, info)
def async_map_data_by_id(Iterable[dict[str|int, Any]] data)
Definition: util.py:19
PowerviewAPI async_connect_hub(HomeAssistant hass, str address, int|None api_version=None)
Definition: util.py:26
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)