1 """Code to handle the Plenticore API."""
3 from __future__
import annotations
5 from collections.abc
import Callable
8 from pykoplenti
import ApiClient, ApiException
10 _KNOWN_HOSTNAME_IDS = (
"Network:Hostname",
"Hostname")
14 """Provides method to format values of process or settings data."""
32 16:
"ImproperDcVoltage",
39 2:
"Emergency Battery Charge",
41 8:
"Winter Mode Step 1",
42 16:
"Winter Mode Step 2",
47 """Return a callable formatter of the given name."""
48 return getattr(cls, name)
52 """Return the given state value as rounded integer."""
54 return round(
float(state))
55 except (TypeError, ValueError):
60 """Return a rounded integer value from a float."""
62 if isinstance(value, float)
and value.is_integer():
63 int_value =
int(value)
64 elif isinstance(value, int):
67 int_value = round(value)
70 except (TypeError, ValueError):
75 """Return the given state value as float rounded to three decimal places."""
77 return round(
float(state), 3)
78 except (TypeError, ValueError):
83 """Return the given state value as energy value, scaled to kWh."""
85 return round(
float(state) / 1000, 1)
86 except (TypeError, ValueError):
91 """Return a readable string of the inverter state."""
94 except (TypeError, ValueError):
97 return PlenticoreDataFormatter.INVERTER_STATES.get(value)
101 """Return a readable state of the energy manager."""
104 except (TypeError, ValueError):
107 return PlenticoreDataFormatter.EM_STATES.get(value)
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:
116 raise ApiException(
"Hostname identifier not found in KNOWN_HOSTNAME_IDS")
str get_hostname_id(ApiClient client)