|
| 1 | +# Conditional Entity System for GridBoss SmartLoad Settings |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the implementation of a conditional entity system that dynamically enables/disables GridBoss SmartLoad settings based on two key configurations: |
| 6 | +1. **SmartSOCVoltBits**: Controls SOC/Volt vs Time mode |
| 7 | +2. **SmartLoad Bits**: Controls which SmartLoads are enabled |
| 8 | + |
| 9 | +This solves the challenge of having dependent settings where some values can only be set when other settings have specific values. |
| 10 | + |
| 11 | +## Problem Statement |
| 12 | + |
| 13 | +In GridBoss systems, certain settings have dependencies: |
| 14 | + |
| 15 | +### SmartLoad Enable/Disable |
| 16 | +- **Disabled SmartLoads**: When `SmartLoadX_Enable` is `false`, all related settings should be unavailable |
| 17 | +- **Enabled SmartLoads**: When `SmartLoadX_Enable` is `true`, related settings become available |
| 18 | + |
| 19 | +### SOC/Volt vs Time Mode |
| 20 | +- **SOC/Volt Mode**: When `SmartLoadX_SOC_Volt` is `true`, only SOC and voltage-based settings are available |
| 21 | +- **Time Mode**: When `SmartLoadX_SOC_Volt` is `false`, only time-based settings are available |
| 22 | + |
| 23 | +### Always Available Settings |
| 24 | +- **Enable/Disable Switches**: Always available regardless of other settings |
| 25 | + |
| 26 | +Home Assistant doesn't provide built-in popup alerts or dynamic entity enabling/disabling, so we needed a creative solution. |
| 27 | + |
| 28 | +## Solution Architecture |
| 29 | + |
| 30 | +### 1. SmartSOCVoltBits and SmartLoad Bits Tracking |
| 31 | + |
| 32 | +The coordinator now tracks both configuration types for each dongle: |
| 33 | + |
| 34 | +```python |
| 35 | +self._smart_soc_volt_bits = {} # Track SmartSOCVoltBits for each dongle |
| 36 | +self._smartload_bits = {} # Track SmartLoad Bits for each dongle |
| 37 | +``` |
| 38 | + |
| 39 | +### 2. Entity Availability System |
| 40 | + |
| 41 | +Entities use the `available` property to dynamically show/hide based on dependency settings: |
| 42 | + |
| 43 | +```python |
| 44 | +@property |
| 45 | +def available(self) -> bool: |
| 46 | + """Return if entity is available.""" |
| 47 | + if not self.coordinator.last_update_success: |
| 48 | + return False |
| 49 | + |
| 50 | + # Check if entity should be available based on SmartLoad SOC/Volt settings |
| 51 | + return self.coordinator.is_entity_available_for_smartload(self._dongle_id, self._entity_type) |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. Validation and User Feedback |
| 55 | + |
| 56 | +When users try to set invalid values, the system: |
| 57 | +1. Prevents the setting from being applied |
| 58 | +2. Shows a persistent notification explaining why the setting is unavailable |
| 59 | +3. Logs a warning message |
| 60 | + |
| 61 | +## Implementation Details |
| 62 | + |
| 63 | +### Coordinator Methods |
| 64 | + |
| 65 | +#### `update_smart_soc_volt_bits(dongle_id, smart_soc_volt_bits)` |
| 66 | +Updates the SmartSOCVoltBits settings and triggers entity availability updates. |
| 67 | + |
| 68 | +#### `update_smartload_bits(dongle_id, smartload_bits)` |
| 69 | +Updates the SmartLoad Bits settings and triggers entity availability updates. |
| 70 | + |
| 71 | +#### `is_entity_available_for_smartload(dongle_id, entity_unique_id)` |
| 72 | +Determines if an entity should be available based on: |
| 73 | +- Whether it's a GridBoss dongle |
| 74 | +- The SmartLoad number (1-4) |
| 75 | +- Whether the SmartLoad is enabled |
| 76 | +- Whether it's a SOC/Volt or Time entity |
| 77 | +- The current SmartSOCVoltBits setting |
| 78 | + |
| 79 | +#### `is_smartload_enabled(dongle_id, smartload_number)` |
| 80 | +Checks if a specific SmartLoad is enabled. |
| 81 | + |
| 82 | +#### `is_entity_available_for_smartload_enable(dongle_id, entity_unique_id)` |
| 83 | +Checks availability based only on SmartLoad enable state (used for Enable/Disable switches). |
| 84 | + |
| 85 | +#### `_trigger_entity_availability_update(dongle_id)` |
| 86 | +Triggers async updates to entity availability when SmartSOCVoltBits change. |
| 87 | + |
| 88 | +### Entity Classification |
| 89 | + |
| 90 | +The system classifies entities into categories: |
| 91 | + |
| 92 | +**SOC/Volt Entities** (available when `SmartLoadX_SOC_Volt` is `true`): |
| 93 | +- `StartSOC`, `EndSOC` |
| 94 | +- `StartVolt`, `EndVolt` |
| 95 | +- `SheddingStartSOC`, `SheddingEndSOC` |
| 96 | +- `SheddingStartVolt`, `SheddingEndVolt` |
| 97 | + |
| 98 | +**Time Entities** (available when `SmartLoadX_SOC_Volt` is `false`): |
| 99 | +- `Start0`, `End0` |
| 100 | +- `Start1`, `End1` |
| 101 | +- `Start2`, `End2` |
| 102 | + |
| 103 | +### User Experience |
| 104 | + |
| 105 | +1. **Visual Feedback**: Unavailable entities appear grayed out in Home Assistant |
| 106 | +2. **Prevention**: Users cannot set values on unavailable entities |
| 107 | +3. **Notifications**: Clear explanations when users try to access unavailable settings |
| 108 | +4. **Dynamic Updates**: Entities become available/unavailable automatically when SmartSOCVoltBits change |
| 109 | + |
| 110 | +## Example Usage |
| 111 | + |
| 112 | +### Complete SmartLoad Configuration |
| 113 | + |
| 114 | +```json |
| 115 | +"SmartLoad": { |
| 116 | + "Bits": { |
| 117 | + "SmartLoad1_Enable": true, |
| 118 | + "SmartLoad2_Enable": true, |
| 119 | + "SmartLoad3_Enable": false, |
| 120 | + "SmartLoad4_Enable": false |
| 121 | + } |
| 122 | +}, |
| 123 | +"SmartSOCVoltBits": { |
| 124 | + "SmartLoad1_SOC_Volt": true, |
| 125 | + "SmartLoad2_SOC_Volt": false, |
| 126 | + "SmartLoad3_SOC_Volt": true, |
| 127 | + "SmartLoad4_SOC_Volt": false |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +**Result**: |
| 132 | +- **SmartLoad1**: ✅ Enabled + SOC/Volt mode → SOC/Volt entities available, Time entities unavailable |
| 133 | +- **SmartLoad2**: ✅ Enabled + Time mode → Time entities available, SOC/Volt entities unavailable |
| 134 | +- **SmartLoad3**: ❌ Disabled → All entities unavailable (except Enable switch) |
| 135 | +- **SmartLoad4**: ❌ Disabled → All entities unavailable (except Enable switch) |
| 136 | + |
| 137 | +### User Interaction Examples |
| 138 | + |
| 139 | +#### Example 1: Disabled SmartLoad |
| 140 | +1. User tries to set `SmartLoad3StartSOC` when `SmartLoad3_Enable` is `false` |
| 141 | +2. System shows notification: "The setting 'SmartLoad3 Start SOC' is not available because SmartLoad3 is disabled. Please enable SmartLoad3 first." |
| 142 | +3. Entity remains grayed out until SmartLoad3 is enabled |
| 143 | + |
| 144 | +#### Example 2: Wrong Mode |
| 145 | +1. User tries to set `SmartLoad1Start0` (time entity) when `SmartLoad1_SOC_Volt` is `true` |
| 146 | +2. System shows notification: "The setting 'SmartLoad1 Start0' is not available because SmartLoad1 is configured for Time mode. Please change to SOC/Volt mode first." |
| 147 | +3. Entity remains grayed out until SmartLoad mode changes |
| 148 | + |
| 149 | +#### Example 3: Always Available |
| 150 | +1. User can always access `SmartLoad1_Enable` switch regardless of other settings |
| 151 | +2. This allows users to enable/disable SmartLoads as needed |
| 152 | + |
| 153 | +## Benefits |
| 154 | + |
| 155 | +1. **Prevents Invalid Configurations**: Users cannot set conflicting settings |
| 156 | +2. **Clear User Feedback**: Persistent notifications explain why settings are unavailable |
| 157 | +3. **Dynamic Behavior**: Entities automatically update when dependencies change |
| 158 | +4. **No Breaking Changes**: Existing functionality remains intact |
| 159 | +5. **Extensible**: System can be extended for other conditional settings |
| 160 | + |
| 161 | +## Technical Notes |
| 162 | + |
| 163 | +- The system uses Home Assistant's built-in `available` property |
| 164 | +- Persistent notifications provide user feedback without popups |
| 165 | +- Entity availability updates are triggered asynchronously |
| 166 | +- The system gracefully handles missing or incomplete SmartSOCVoltBits data |
| 167 | +- All changes are backward compatible |
| 168 | + |
| 169 | +## Future Enhancements |
| 170 | + |
| 171 | +1. **Additional Dependencies**: Extend to other conditional settings |
| 172 | +2. **Bulk Operations**: Handle multiple entity updates more efficiently |
| 173 | +3. **Configuration UI**: Add visual indicators in the configuration flow |
| 174 | +4. **Automation Support**: Allow automations to be aware of entity availability |
0 commit comments