1 """The Sun WEG inverter sensor integration."""
7 from sunweg.api
import APIHelper
8 from sunweg.plant
import Plant
10 from homeassistant
import config_entries
18 from .const
import CONF_PLANT_ID, DOMAIN, PLATFORMS, DeviceType
20 SCAN_INTERVAL = datetime.timedelta(minutes=5)
22 _LOGGER = logging.getLogger(__name__)
28 """Load the saved entities."""
29 api = APIHelper(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD])
30 if not await hass.async_add_executor_job(api.authenticate):
32 hass.data.setdefault(DOMAIN, {})[entry.entry_id] =
SunWEGData(
33 api, entry.data[CONF_PLANT_ID]
35 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
40 """Unload a config entry."""
41 hass.data[DOMAIN].pop(entry.entry_id)
42 if len(hass.data[DOMAIN]) == 0:
44 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
48 """The class for handling data retrieval."""
55 """Initialize the probe."""
59 self.
datadata: Plant =
None
60 self.previous_values: dict = {}
62 @Throttle(SCAN_INTERVAL)
64 """Update probe data."""
65 _LOGGER.debug(
"Updating data for plant %s", self.
plant_idplant_id)
68 for inverter
in self.
datadata.inverters:
69 self.
apiapi.complete_inverter(inverter)
70 except json.decoder.JSONDecodeError:
71 _LOGGER.error(
"Unable to fetch data from SunWEG server")
72 _LOGGER.debug(
"Finished updating data for plant %s", self.
plant_idplant_id)
77 device_type: DeviceType,
79 deep_name: str |
None =
None,
81 """Retrieve from a Plant the desired variable value."""
82 if device_type == DeviceType.TOTAL:
83 return self.
datadata.__dict__.get(variable)
85 inverter_list = [i
for i
in self.
datadata.inverters
if i.id == inverter_id]
86 if len(inverter_list) == 0:
88 inverter = inverter_list[0]
90 if device_type == DeviceType.INVERTER:
91 return inverter.__dict__.get(variable)
92 if device_type == DeviceType.PHASE:
93 for phase
in inverter.phases:
94 if phase.name == deep_name:
95 return phase.__dict__.get(variable)
96 elif device_type == DeviceType.STRING:
97 for mppt
in inverter.mppts:
98 for string
in mppt.strings:
99 if string.name == deep_name:
100 return string.__dict__.get(variable)
106 api_variable_key: str,
107 api_variable_unit: str |
None,
108 deep_name: str |
None,
109 device_type: DeviceType,
111 name: str | UndefinedType |
None,
112 native_unit_of_measurement: str |
None,
114 previous_value_drop_threshold: float |
None,
115 ) -> tuple[StateType | datetime.datetime, str |
None]:
118 "Data request for: %s",
121 variable = api_variable_key
122 previous_unit = native_unit_of_measurement
123 api_value = self.
get_api_valueget_api_value(variable, device_type, inverter_id, deep_name)
124 previous_value = self.previous_values.
get(variable)
125 return_value = api_value
126 if api_variable_unit
is not None:
127 native_unit_of_measurement = self.
get_api_valueget_api_value(
136 previous_value_drop_threshold
is not None
137 and previous_value
is not None
138 and api_value
is not None
139 and previous_unit == native_unit_of_measurement
143 "%s - Drop threshold specified (%s), checking for drop... API"
144 " Value: %s, Previous Value: %s"
147 previous_value_drop_threshold,
151 diff =
float(api_value) -
float(previous_value)
158 if -(previous_value_drop_threshold) <= diff < 0:
161 "Diff is negative, but only by a small amount therefore not a"
162 " nightly reset, using previous value (%s) instead of api value"
168 return_value = previous_value
170 _LOGGER.debug(
"%s - No drop detected, using API value", name)
185 if never_resets
and api_value == 0
and previous_value:
188 "API value is 0, but this value should never reset, returning"
189 " previous value (%s) instead"
193 return_value = previous_value
195 self.previous_values[variable] = return_value
197 return (return_value, native_unit_of_measurement)
tuple[StateType|datetime.datetime, str|None] get_data(self, *str api_variable_key, str|None api_variable_unit, str|None deep_name, DeviceType device_type, int inverter_id, str|UndefinedType|None name, str|None native_unit_of_measurement, bool never_resets, float|None previous_value_drop_threshold)
def get_api_value(self, str variable, DeviceType device_type, int inverter_id=0, str|None deep_name=None)
None __init__(self, APIHelper api, int plant_id)
web.Response get(self, web.Request request, str config_key)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry)