Skip to content

Commit 3b28468

Browse files
committed
Logic for buttons
1 parent f904b92 commit 3b28468

2 files changed

Lines changed: 50 additions & 28 deletions

File tree

custom_components/ir_light/light.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ def __init__(self, hass: HomeAssistant, name: str, config_data: dict):
7272
self._hs_color = None
7373
self._color_steps = self._config_data.get("brightness_levels")
7474

75-
# Supported features
76-
#self._supported_features = (
77-
# SUPPORT_BRIGHTNESS | SUPPORT_EFFECT | SUPPORT_COLOR
78-
#)
79-
8075
self.button_map = {
8176
'ON': self._config_data.get('ir_button_on'),
8277
'OFF': self._config_data.get('ir_button_off'),
@@ -129,17 +124,18 @@ def effect(self) -> str | None:
129124
def hs_color(self) -> tuple[float, float] | None:
130125
return self._hs_color
131126

132-
async def _async_map_color_to_button(self, hue: float, sat: float):
127+
async def _async_map_color_to_button(self, hue: float, sat: float) -> None:
133128
"""Color Selector Script"""
134129

135130
button_id = None
136131

137132
if sat < 10:
138133
button_id = self._config_data.get("color_white")
139134
if button_id:
140-
await self.hass.services.async_call(
141-
"homeassistant", "turn_on", {"entity_id": button_id}, blocking=False
142-
)
135+
await self._async_press_button(button_id)
136+
#await self.hass.services.async_call(
137+
# "homeassistant", "turn_on", {"entity_id": button_id}, blocking=False
138+
#)
143139
return
144140

145141
available_buttons = {}
@@ -162,19 +158,45 @@ async def _async_map_color_to_button(self, hue: float, sat: float):
162158

163159
if best_button:
164160
_LOGGER.debug(f"Closest match for Hue {hue}: {best_button} (dist: {min_distance})")
165-
await self.hass.services.async_call(
166-
"homeassistant", "turn_on", {"entity_id": best_button}, blocking=False
167-
)
161+
await self._async_press_button(best_button)
162+
#await self.hass.services.async_call(
163+
# "homeassistant", "turn_on", {"entity_id": best_button}, blocking=False
164+
#)
168165

169-
async def _async_press_button(self, action_key: str):
166+
async def _async_press_button(self, entity_id: str) -> None:
170167
"""Helper to press the corresponding IR button."""
171-
entity_id = self.button_map.get(action_key)
172-
if entity_id:
173-
await self.hass.services.async_call(
174-
"homeassistant", "turn_on", {"entity_id": entity_id}, blocking=False
175-
)
168+
169+
if not entity_id or not isinstance(entity_id, str):
170+
_LOGGER.warning(f"Entity_id not found for action.")
171+
return
172+
173+
domain = entity_id.split(".")[0]
174+
175+
if domain == "button":
176+
service_domain = "button"
177+
service_name = "press"
178+
elif domain in ["scene", "script"]:
179+
service_domain = domain
180+
service_name = "turn_on"
176181
else:
177-
_LOGGER.warning(f"IR button for action '{action_key}' not found in BUTTON_MAP.")
182+
# Future prof
183+
service_domain = "homeassistant"
184+
service_name = "turn_on"
185+
186+
await self.hass.services.async_call(
187+
service_domain,
188+
service_name,
189+
{"entity_id": entity_id},
190+
blocking=False
191+
)
192+
193+
#entity_id = self.button_map.get(action_key)
194+
#if entity_id:
195+
# await self.hass.services.async_call(
196+
# "homeassistant", "turn_on", {"entity_id": entity_id}, blocking=False
197+
# )
198+
#else:
199+
# _LOGGER.warning(f"IR button for action '{action_key}' not found in BUTTON_MAP.")
178200

179201
async def async_turn_on(self, **kwargs) -> None:
180202
"""Turn on light. Manages brightness, color and effect"""
@@ -185,7 +207,7 @@ async def async_turn_on(self, **kwargs) -> None:
185207
hue, sat = self._hs_color
186208

187209
if not self._state or not kwargs:
188-
await self._async_press_button('ON')
210+
await self._async_press_button(self.button_map.get('ON'))
189211
self._state = True
190212
await asyncio.sleep(0.5)
191213

@@ -197,11 +219,11 @@ async def async_turn_on(self, **kwargs) -> None:
197219
self._effect = effect
198220

199221
if not self._state or not kwargs:
200-
await self._async_press_button('ON')
222+
await self._async_press_button(self.button_map.get('ON'))
201223
self._state = True
202224
await asyncio.sleep(0.5)
203225

204-
await self._async_press_button(f"EFFECT_{effect.upper()}")
226+
await self._async_press_button(self.button_map.get(f"EFFECT_{effect.upper()}"))
205227

206228
# --- 3. Brightness Management (set_level) ---
207229
if ATTR_BRIGHTNESS in kwargs:
@@ -213,14 +235,14 @@ async def async_turn_on(self, **kwargs) -> None:
213235

214236
# Replicar la lógica de pulsos de brillo (arriba/abajo)
215237
if target_ir_level == 0:
216-
await self._async_press_button('OFF')
238+
await self._async_press_button(self.button_map.get('OFF'))
217239
self._state = False
218240
self._effect = None # Clean effect
219241
self.async_write_ha_state()
220242
return
221243
else:
222244
if not self._state or not kwargs:
223-
await self._async_press_button('ON')
245+
await self._async_press_button(self.button_map.get('ON'))
224246
self._state = True
225247
await asyncio.sleep(0.5)
226248

@@ -231,7 +253,7 @@ async def async_turn_on(self, **kwargs) -> None:
231253

232254
# Executes brigthness change N times
233255
for _ in range(abs(diff)):
234-
await self._async_press_button(action_key)
256+
await self._async_press_button(self.button_map.get(action_key))
235257
# Delay
236258
await asyncio.sleep(0.5)
237259

@@ -241,14 +263,14 @@ async def async_turn_on(self, **kwargs) -> None:
241263
# --- 4. General switch (turn_on) ---
242264
# If nothing has change before or have changed but it was not ON
243265
if not self._state or not kwargs:
244-
await self._async_press_button('ON')
266+
await self._async_press_button(self.button_map.get('ON'))
245267
self._state = True
246268

247269
self.async_write_ha_state()
248270

249271
async def async_turn_off(self, **kwargs) -> None:
250272
"""Turn off light"""
251-
await self._async_press_button('OFF')
273+
await self._async_press_button(self.button_map.get('OFF'))
252274
self._state = False
253275
self._effect = None # Clean effect
254276

custom_components/ir_light/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "local_polling",
99
"issue_tracker": "https://github.com/Infinitte/HA-IR-Light/issues",
1010
"requirements": [],
11-
"version": "0.2.1"
11+
"version": "0.2.2"
1212
}

0 commit comments

Comments
 (0)