@@ -22,14 +22,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
2222 client = data ["client" ]
2323 coordinator = data ["coordinator" ]
2424
25- async_add_entities ([IntexSWGRebootButton (client , coordinator , entry )])
25+ async_add_entities ([
26+ IntexSWGRebootButton (client , coordinator , entry ),
27+ IntexSWGPowerOnButton (client , coordinator , entry ),
28+ IntexSWGPowerOffButton (client , coordinator , entry ),
29+ IntexSWGPowerStandbyButton (client , coordinator , entry ),
30+ ])
2631
2732class IntexSWGRebootButton (CoordinatorEntity , ButtonEntity ):
2833 def __init__ (self , client , coordinator , entry ):
2934 super ().__init__ (coordinator )
3035 self ._client = client
3136 self ._attr_name = "Reboot ESP32"
3237 self ._attr_unique_id = f"{ coordinator .name } _reboot"
38+ self ._attr_icon = "mdi:restart"
3339
3440 # device_info
3541 host = entry .data .get (CONF_HOST )
@@ -41,7 +47,7 @@ def __init__(self, client, coordinator, entry):
4147 "manufacturer" : DEVICE_MANUFACTURER ,
4248 "model" : DEVICE_MODEL ,
4349 "connections" : {("ip" , host )}
44- }
50+ }
4551
4652 async def async_press (self ) -> None :
4753 url = f"http://{ self ._client ._host } :{ self ._client ._port } /api/v1/intex/swg/reboot"
@@ -58,3 +64,160 @@ async def async_press(self) -> None:
5864 def _refresh_cb (_ ):
5965 self .hass .async_create_task (self .coordinator .async_request_refresh ())
6066 async_call_later (self .hass , 60 , _refresh_cb )
67+
68+ class IntexSWGPowerOnButton (CoordinatorEntity , ButtonEntity ):
69+ def __init__ (self , client , coordinator , entry ):
70+ super ().__init__ (coordinator )
71+ self ._client = client
72+ self ._attr_name = "Power ON"
73+ self ._attr_unique_id = f"{ coordinator .name } _power_on"
74+
75+ # Device info for the dashboard
76+ host = entry .data .get (CONF_HOST )
77+ port = entry .data .get (CONF_PORT )
78+ self ._attr_device_info = {
79+ "identifiers" : {(DOMAIN , entry .entry_id )},
80+ "name" : f"{ DEVICE_NAME } ({ host } :{ port } )" ,
81+ "manufacturer" : DEVICE_MANUFACTURER ,
82+ "model" : DEVICE_MODEL ,
83+ "connections" : {("ip" , host )},
84+ }
85+
86+ @property
87+ def icon (self ) -> str :
88+ """Return a solid power icon if status == 'ON', else outline."""
89+ current_power = self ._client .data .get ("status" , {}).get ("power" , "" )
90+ return "mdi:radiobox-marked" if current_power == "ON" else "mdi:radiobox-blank"
91+
92+ @property
93+ def extra_state_attributes (self ) -> dict [str , bool ]:
94+ """Provide an 'active' attribute for Lovelace usage."""
95+ current_power = self ._client .data .get ("status" , {}).get ("power" , "" )
96+ return {"active" : current_power == "ON" }
97+
98+ async def async_press (self ) -> None :
99+ url = f"http://{ self ._client ._host } :{ self ._client ._port } /api/v1/intex/swg"
100+ payload = {"data" : {"power" : "on" }}
101+
102+ _LOGGER .debug ("POST Power ON: URL=%s, payload=%s" , url , payload )
103+
104+ try :
105+ await self ._client ._session .post (url , json = payload )
106+ except Exception as err :
107+ _LOGGER .warning ("Error sending Power ON: %s" , err )
108+
109+ # Attempt to clear cache
110+ try :
111+ self ._client .clear_cache ()
112+ except Exception as err :
113+ _LOGGER .warning ("Unable to clear cache: %s" , err )
114+
115+ # Schedule a brief refresh so the UI reflects the new state
116+ async def _refresh_cb (_ ):
117+ self .hass .async_create_task (self .coordinator .async_request_refresh ())
118+
119+ async_call_later (self .hass , 0.1 , _refresh_cb )
120+
121+ class IntexSWGPowerOffButton (CoordinatorEntity , ButtonEntity ):
122+ def __init__ (self , client , coordinator , entry ):
123+ super ().__init__ (coordinator )
124+ self ._client = client
125+ self ._attr_name = "Power OFF"
126+ self ._attr_unique_id = f"{ coordinator .name } _power_off"
127+
128+ # Device info for the dashboard
129+ host = entry .data .get (CONF_HOST )
130+ port = entry .data .get (CONF_PORT )
131+ self ._attr_device_info = {
132+ "identifiers" : {(DOMAIN , entry .entry_id )},
133+ "name" : f"{ DEVICE_NAME } ({ host } :{ port } )" ,
134+ "manufacturer" : DEVICE_MANUFACTURER ,
135+ "model" : DEVICE_MODEL ,
136+ "connections" : {("ip" , host )},
137+ }
138+
139+ @property
140+ def icon (self ) -> str :
141+ """Return a solid power-off icon if status == 'OFF', else outline."""
142+ current_power = self ._client .data .get ("status" , {}).get ("power" , "" )
143+ return "mdi:radiobox-marked" if current_power == "OFF" else "mdi:radiobox-blank"
144+
145+ @property
146+ def extra_state_attributes (self ) -> dict [str , bool ]:
147+ """Provide an 'active' attribute for Lovelace usage."""
148+ current_power = self ._client .data .get ("status" , {}).get ("power" , "" )
149+ return {"active" : current_power == "OFF" }
150+
151+ async def async_press (self ) -> None :
152+ url = f"http://{ self ._client ._host } :{ self ._client ._port } /api/v1/intex/swg"
153+ payload = {"data" : {"power" : "off" }}
154+
155+ _LOGGER .debug ("POST Power OFF: URL=%s, payload=%s" , url , payload )
156+
157+ try :
158+ await self ._client ._session .post (url , json = payload )
159+ except Exception as err :
160+ _LOGGER .warning ("Error sending Power OFF: %s" , err )
161+
162+ try :
163+ self ._client .clear_cache ()
164+ except Exception as err :
165+ _LOGGER .warning ("Unable to clear cache: %s" , err )
166+
167+ # Schedule a brief refresh so the UI reflects the new state
168+ async def _refresh_cb (_ ):
169+ self .hass .async_create_task (self .coordinator .async_request_refresh ())
170+
171+ async_call_later (self .hass , 0.1 , _refresh_cb )
172+
173+ class IntexSWGPowerStandbyButton (CoordinatorEntity , ButtonEntity ):
174+ def __init__ (self , client , coordinator , entry ):
175+ super ().__init__ (coordinator )
176+ self ._client = client
177+ self ._attr_name = "Power STANDBY"
178+ self ._attr_unique_id = f"{ coordinator .name } _power_standby"
179+
180+ # Device info for the dashboard
181+ host = entry .data .get (CONF_HOST )
182+ port = entry .data .get (CONF_PORT )
183+ self ._attr_device_info = {
184+ "identifiers" : {(DOMAIN , entry .entry_id )},
185+ "name" : f"{ DEVICE_NAME } ({ host } :{ port } )" ,
186+ "manufacturer" : DEVICE_MANUFACTURER ,
187+ "model" : DEVICE_MODEL ,
188+ "connections" : {("ip" , host )},
189+ }
190+
191+ @property
192+ def icon (self ) -> str :
193+ """Return a solid power-sleep icon if status == 'STANDBY', else outline."""
194+ current_power = self ._client .data .get ("status" , {}).get ("power" , "" )
195+ return "mdi:radiobox-marked" if current_power == "STANDBY" else "mdi:radiobox-blank"
196+
197+ @property
198+ def extra_state_attributes (self ) -> dict [str , bool ]:
199+ """Provide an 'active' attribute for Lovelace usage."""
200+ current_power = self ._client .data .get ("status" , {}).get ("power" , "" )
201+ return {"active" : current_power == "STANDBY" }
202+
203+ async def async_press (self ) -> None :
204+ url = f"http://{ self ._client ._host } :{ self ._client ._port } /api/v1/intex/swg"
205+ payload = {"data" : {"power" : "standby" }}
206+
207+ _LOGGER .debug ("POST Power STANDBY: URL=%s, payload=%s" , url , payload )
208+
209+ try :
210+ await self ._client ._session .post (url , json = payload )
211+ except Exception as err :
212+ _LOGGER .warning ("Error sending Power STANDBY: %s" , err )
213+
214+ try :
215+ self ._client .clear_cache ()
216+ except Exception as err :
217+ _LOGGER .warning ("Unable to clear cache: %s" , err )
218+
219+ # Schedule a brief refresh so the UI reflects the new state
220+ async def _refresh_cb (_ ):
221+ self .hass .async_create_task (self .coordinator .async_request_refresh ())
222+
223+ async_call_later (self .hass , 0.1 , _refresh_cb )
0 commit comments