1 """Read status of SunWEG inverters."""
3 from __future__
import annotations
6 from types
import MappingProxyType
9 from sunweg.api
import APIHelper
10 from sunweg.device
import Inverter
11 from sunweg.plant
import Plant
20 from ..
import SunWEGData
21 from ..const
import CONF_PLANT_ID, DEFAULT_PLANT_ID, DOMAIN, DeviceType
22 from .inverter
import INVERTER_SENSOR_TYPES
23 from .phase
import PHASE_SENSOR_TYPES
24 from .sensor_entity_description
import SunWEGSensorEntityDescription
25 from .string
import STRING_SENSOR_TYPES
26 from .total
import TOTAL_SENSOR_TYPES
28 _LOGGER = logging.getLogger(__name__)
32 api: APIHelper, config: MappingProxyType[str, Any]
33 ) -> tuple[list[Inverter], int]:
34 """Retrieve the device list for the selected plant."""
35 plant_id =
int(config[CONF_PLANT_ID])
37 if plant_id == DEFAULT_PLANT_ID:
38 plant_info: list[Plant] = api.listPlants()
39 plant_id = plant_info[0].id
41 devices: list[Inverter] = []
43 for inverter
in api.plant(plant_id).inverters:
44 api.complete_inverter(inverter)
45 devices.append(inverter)
46 return (devices, plant_id)
51 config_entry: ConfigEntry,
52 async_add_entities: AddEntitiesCallback,
54 """Set up the SunWEG sensor."""
55 name = config_entry.data[CONF_NAME]
57 probe: SunWEGData = hass.data[DOMAIN][config_entry.entry_id]
59 devices, plant_id = await hass.async_add_executor_job(
60 get_device_list, probe.api, config_entry.data
67 unique_id=f
"{plant_id}-{description.key}",
68 description=description,
69 device_type=DeviceType.TOTAL,
71 for description
in TOTAL_SENSOR_TYPES
79 name=f
"{device.name}",
80 unique_id=f
"{device.sn}-{description.key}",
81 description=description,
82 device_type=DeviceType.INVERTER,
83 inverter_id=device.id,
86 for description
in INVERTER_SENSOR_TYPES
94 name=f
"{device.name} {phase.name}",
95 unique_id=f
"{device.sn}-{phase.name}-{description.key}",
96 description=description,
97 inverter_id=device.id,
98 device_type=DeviceType.PHASE,
101 for device
in devices
102 for phase
in device.phases
103 for description
in PHASE_SENSOR_TYPES
111 name=f
"{device.name} {string.name}",
112 unique_id=f
"{device.sn}-{string.name}-{description.key}",
113 description=description,
114 inverter_id=device.id,
115 device_type=DeviceType.STRING,
116 deep_name=string.name,
118 for device
in devices
119 for mppt
in device.mppts
120 for string
in mppt.strings
121 for description
in STRING_SENSOR_TYPES
129 """Representation of a SunWEG Sensor."""
131 entity_description: SunWEGSensorEntityDescription
138 description: SunWEGSensorEntityDescription,
139 device_type: DeviceType,
140 inverter_id: int = 0,
141 deep_name: str |
None =
None,
143 """Initialize a sensor."""
153 description.icon
if description.icon
is not None else "mdi:solar-power"
157 identifiers={(DOMAIN,
str(probe.plant_id))},
158 manufacturer=
"SunWEG",
163 """Get the latest data from the Sun WEG API and updates the state."""
166 self._attr_native_value,
167 self._attr_native_unit_of_measurement,
168 ) = self.
probeprobe.get_data(
177 previous_value_drop_threshold=self.
entity_descriptionentity_description.previous_value_drop_threshold,
str|None native_unit_of_measurement(self)
None __init__(self, SunWEGData probe, str name, str unique_id, SunWEGSensorEntityDescription description, DeviceType device_type, int inverter_id=0, str|None deep_name=None)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
tuple[list[Inverter], int] get_device_list(APIHelper api, MappingProxyType[str, Any] config)