Add Microphone Mute Switch Entity

Subscribes to the new mic_muted and mic_unmuted events.

Provides visual feedback by setting the LEDs to a solid dim red when muted and calling the idle state handler (turning the LEDs off) when unmuted.
This commit is contained in:
imonlinux
2025-10-11 17:11:00 -07:00
committed by GitHub
parent 39d6d3e010
commit d90943fe8e
+15 -6
View File
@@ -15,6 +15,7 @@ _OFF = (0, 0, 0)
_BLUE = (0, 0, 255) _BLUE = (0, 0, 255)
_YELLOW = (255, 255, 0) _YELLOW = (255, 255, 0)
_GREEN = (0, 255, 0) _GREEN = (0, 255, 0)
_DIM_RED = (50, 0, 0) # ADDED for Mute
class LedController(EventHandler): class LedController(EventHandler):
@@ -33,7 +34,6 @@ class LedController(EventHandler):
try: try:
if interface == "gpio": if interface == "gpio":
_LOGGER.debug(f"Initializing LEDs on GPIO data={data_pin}, clock={clock_pin}") _LOGGER.debug(f"Initializing LEDs on GPIO data={data_pin}, clock={clock_pin}")
# Dynamically get GPIO pin objects from the board library
data_pin_obj = getattr(board, f"D{data_pin}") data_pin_obj = getattr(board, f"D{data_pin}")
clock_pin_obj = getattr(board, f"D{clock_pin}") clock_pin_obj = getattr(board, f"D{clock_pin}")
self.leds = adafruit_dotstar.DotStar( self.leds = adafruit_dotstar.DotStar(
@@ -47,7 +47,6 @@ class LedController(EventHandler):
self._is_ready = True self._is_ready = True
_LOGGER.info(f"LED Controller initialized using {interface} interface.") _LOGGER.info(f"LED Controller initialized using {interface} interface.")
# Signal that the system is ready
self.run_action("blink", _GREEN, 2) self.run_action("blink", _GREEN, 2)
except Exception: except Exception:
@@ -79,12 +78,10 @@ class LedController(EventHandler):
try: try:
r, g, b = color r, g, b = color
while True: while True:
# Fade in
for i in range(0, 101, 5): for i in range(0, 101, 5):
brightness = i / 100.0 brightness = i / 100.0
await self.color((int(r * brightness), int(g * brightness), int(b * brightness))) await self.color((int(r * brightness), int(g * brightness), int(b * brightness)))
await asyncio.sleep(speed) await asyncio.sleep(speed)
# Fade out
for i in range(100, -1, -5): for i in range(100, -1, -5):
brightness = i / 100.0 brightness = i / 100.0
await self.color((int(r * brightness), int(g * brightness), int(b * brightness))) await self.color((int(r * brightness), int(g * brightness), int(b * brightness)))
@@ -92,7 +89,8 @@ class LedController(EventHandler):
except asyncio.CancelledError: except asyncio.CancelledError:
await self.color(_OFF) await self.color(_OFF)
# --- THIS IS THE NEWLY ADDED METHOD --- # --- EVENT HANDLERS ---
@subscribe @subscribe
def ha_connected(self, data: dict): def ha_connected(self, data: dict):
_LOGGER.debug("HA Connected, setting idle LED state.") _LOGGER.debug("HA Connected, setting idle LED state.")
@@ -111,7 +109,6 @@ class LedController(EventHandler):
@subscribe @subscribe
def voice_vad_start(self, data: dict): def voice_vad_start(self, data: dict):
_LOGGER.debug("LED Event: voice_vad_start (user speaking)") _LOGGER.debug("LED Event: voice_vad_start (user speaking)")
# Do nothing, allow the blue pulse to continue
pass pass
@subscribe @subscribe
@@ -128,3 +125,15 @@ class LedController(EventHandler):
def voice_run_end(self, data: dict): def voice_run_end(self, data: dict):
_LOGGER.debug("LED Event: voice_run_end") _LOGGER.debug("LED Event: voice_run_end")
self.run_action("color", _OFF) self.run_action("color", _OFF)
# --- ADDED: Mute State Handlers ---
@subscribe
def mic_muted(self, data: dict):
_LOGGER.debug("LED Event: Mic muted")
self.run_action("color", _DIM_RED)
@subscribe
def mic_unmuted(self, data: dict):
_LOGGER.debug("LED Event: Mic unmuted")
# Return to the normal idle state (off)
self.voice_run_end(data)