Home Assistant Unofficial Reference 2024.12.1
helper.py
Go to the documentation of this file.
1 """Code to handle the Plenticore API."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from typing import Any
7 
8 from pykoplenti import ApiClient, ApiException
9 
10 _KNOWN_HOSTNAME_IDS = ("Network:Hostname", "Hostname")
11 
12 
14  """Provides method to format values of process or settings data."""
15 
16  INVERTER_STATES = {
17  0: "Off",
18  1: "Init",
19  2: "IsoMEas",
20  3: "GridCheck",
21  4: "StartUp",
22  6: "FeedIn",
23  7: "Throttled",
24  8: "ExtSwitchOff",
25  9: "Update",
26  10: "Standby",
27  11: "GridSync",
28  12: "GridPreCheck",
29  13: "GridSwitchOff",
30  14: "Overheating",
31  15: "Shutdown",
32  16: "ImproperDcVoltage",
33  17: "ESB",
34  }
35 
36  EM_STATES = {
37  0: "Idle",
38  1: "n/a",
39  2: "Emergency Battery Charge",
40  4: "n/a",
41  8: "Winter Mode Step 1",
42  16: "Winter Mode Step 2",
43  }
44 
45  @classmethod
46  def get_method(cls, name: str) -> Callable[[Any], Any]:
47  """Return a callable formatter of the given name."""
48  return getattr(cls, name)
49 
50  @staticmethod
51  def format_round(state: str) -> int | str:
52  """Return the given state value as rounded integer."""
53  try:
54  return round(float(state))
55  except (TypeError, ValueError):
56  return state
57 
58  @staticmethod
59  def format_round_back(value: float) -> str:
60  """Return a rounded integer value from a float."""
61  try:
62  if isinstance(value, float) and value.is_integer():
63  int_value = int(value)
64  elif isinstance(value, int):
65  int_value = value
66  else:
67  int_value = round(value)
68 
69  return str(int_value)
70  except (TypeError, ValueError):
71  return ""
72 
73  @staticmethod
74  def format_float(state: str) -> float | str:
75  """Return the given state value as float rounded to three decimal places."""
76  try:
77  return round(float(state), 3)
78  except (TypeError, ValueError):
79  return state
80 
81  @staticmethod
82  def format_energy(state: str) -> float | str:
83  """Return the given state value as energy value, scaled to kWh."""
84  try:
85  return round(float(state) / 1000, 1)
86  except (TypeError, ValueError):
87  return state
88 
89  @staticmethod
90  def format_inverter_state(state: str) -> str | None:
91  """Return a readable string of the inverter state."""
92  try:
93  value = int(state)
94  except (TypeError, ValueError):
95  return state
96 
97  return PlenticoreDataFormatter.INVERTER_STATES.get(value)
98 
99  @staticmethod
100  def format_em_manager_state(state: str) -> str | None:
101  """Return a readable state of the energy manager."""
102  try:
103  value = int(state)
104  except (TypeError, ValueError):
105  return state
106 
107  return PlenticoreDataFormatter.EM_STATES.get(value)
108 
109 
110 async def get_hostname_id(client: ApiClient) -> str:
111  """Check for known existing hostname ids."""
112  all_settings = await client.get_settings()
113  for entry in all_settings["scb:network"]:
114  if entry.id in _KNOWN_HOSTNAME_IDS:
115  return entry.id
116  raise ApiException("Hostname identifier not found in KNOWN_HOSTNAME_IDS")