|
5 | 5 | from homeassistant.helpers.entity import EntityCategory |
6 | 6 |
|
7 | 7 |
|
8 | | -from .const import DOMAIN, INVERTER_MODEL_MAP |
| 8 | +from .const import DOMAIN, INVERTER_MODEL_MAP, ECU_MODEL_MAP |
9 | 9 |
|
10 | 10 |
|
11 | 11 | async def async_setup_entry(hass, config_entry, async_add_entities): |
12 | 12 | """Set up the number platform.""" |
13 | 13 | coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"] |
14 | 14 | ecu = hass.data[DOMAIN][config_entry.entry_id]["ecu"] |
15 | 15 |
|
16 | | - # Only compatible with ECU-C and ECU-R-Pro |
| 16 | + entities = [] |
| 17 | + |
| 18 | + # ECU Power Limit Number - only compatible with ECU-C |
| 19 | + if ecu.ecu.ecu_id.startswith("215"): |
| 20 | + entities.append(ECUPowerLimitNumber(coordinator, ecu)) |
| 21 | + |
| 22 | + # Inverter Max Power Numbers - only compatible with ECU-C and ECU-R-Pro |
17 | 23 | if ecu.ecu.ecu_id.startswith(("215", "2162")): |
18 | | - entities = [] |
19 | 24 | for inverter_id, inverter_data in coordinator.data.get("inverters", {}).items(): |
20 | 25 | entities.append( |
21 | 26 | InverterMaxPwrNumber(coordinator, ecu, inverter_id, inverter_data) |
22 | 27 | ) |
| 28 | + |
| 29 | + if entities: |
23 | 30 | async_add_entities(entities, True) |
24 | 31 |
|
25 | 32 |
|
@@ -67,6 +74,70 @@ async def async_set_native_value(self, value: float): |
67 | 74 | self._attr_native_value = value |
68 | 75 | self.async_write_ha_state() |
69 | 76 |
|
| 77 | + def set_native_value(self, value: float): |
| 78 | + """Set the value synchronously (required by NumberEntity).""" |
| 79 | + # This is called by Home Assistant framework, but we use async version |
| 80 | + self._attr_native_value = value |
| 81 | + |
| 82 | + async def async_added_to_hass(self): |
| 83 | + """Handle entity which value will be restored.""" |
| 84 | + await super().async_added_to_hass() |
| 85 | + if (last_state := await self.async_get_last_state()) is not None: |
| 86 | + self._attr_native_value = float(last_state.state) |
| 87 | + |
| 88 | + |
| 89 | +class ECUPowerLimitNumber(CoordinatorEntity, RestoreNumber): |
| 90 | + """Representation of an ECU Power Limit Number entity.""" |
| 91 | + |
| 92 | + def __init__(self, coordinator, ecu): |
| 93 | + """Initialize the number entity.""" |
| 94 | + super().__init__(coordinator) |
| 95 | + self._ecu = ecu |
| 96 | + self._attr_name = f"ECU {ecu.ecu.ecu_id} Power Limit" |
| 97 | + self._attr_unique_id = f"{ecu.ecu.ecu_id}_power_limit" |
| 98 | + self._attr_native_min_value = 0 |
| 99 | + self._attr_native_max_value = 3 |
| 100 | + self._attr_native_step = 0.1 |
| 101 | + self._attr_native_value = 0 |
| 102 | + self._attr_device_class = "power" |
| 103 | + self._attr_native_unit_of_measurement = "kW" |
| 104 | + self._attr_mode = "slider" |
| 105 | + |
| 106 | + @property |
| 107 | + def device_info(self): |
| 108 | + """Return the device info.""" |
| 109 | + return { |
| 110 | + "identifiers": { |
| 111 | + (DOMAIN, f"ecu_{self._ecu.ecu.ecu_id}"), |
| 112 | + }, |
| 113 | + "name": f"ECU {self._ecu.ecu.ecu_id}", |
| 114 | + "manufacturer": "APsystems", |
| 115 | + "model": ECU_MODEL_MAP.get(self._ecu.ecu.ecu_id[:4], "Unknown Model"), |
| 116 | + } |
| 117 | + |
| 118 | + @property |
| 119 | + def entity_category(self): |
| 120 | + """Return the category of the entity.""" |
| 121 | + return EntityCategory.CONFIG |
| 122 | + |
| 123 | + @property |
| 124 | + def suggested_display_precision(self): |
| 125 | + """Return the suggested number of decimal places for display.""" |
| 126 | + return 1 |
| 127 | + |
| 128 | + async def async_set_native_value(self, value: float): |
| 129 | + """Update the current value.""" |
| 130 | + # Convert from kW to watts for the ECU API |
| 131 | + power_limit_watts = int(value * 1000) |
| 132 | + await self._ecu.set_power_limit(power_limit_watts) |
| 133 | + self._attr_native_value = value |
| 134 | + self.async_write_ha_state() |
| 135 | + |
| 136 | + def set_native_value(self, value: float): |
| 137 | + """Set the value synchronously (required by NumberEntity).""" |
| 138 | + # This is called by Home Assistant framework, but we use async version |
| 139 | + self._attr_native_value = value |
| 140 | + |
70 | 141 | async def async_added_to_hass(self): |
71 | 142 | """Handle entity which value will be restored.""" |
72 | 143 | await super().async_added_to_hass() |
|
0 commit comments