Files
dckc f50c871521
check / check (push) Waiting to run
feat: mute the TV while the box listens, if the brain turns that on
whisper hears people better without the show behind them. The brain
sets a mute code (and an unmute code, if not the same) with
mute_while_listening on its ir capability, or turns it off; ir-server
keeps the setting in its state directory.

Mute is often a toggle, and the camera can't reliably tell whether the
sound is off, so the box senses nothing: the setting is the brain's
to turn on or off.

The button service holds a connection to ir-server's conversation
socket for each conversation: ir-server mutes when it opens and
unmutes, with the same codes, when it closes. The socket's group is
the button's alone, and the button still can't send any code.

Generated-by: Claude Opus 5.5
2026-12-03 16:49:58 -06:00

57 lines
2.0 KiB
Python

"""Muting the TV while the box listens: the button service's side.
For the whole of a conversation, the button service holds a connection to the ir service's
conversation socket (../ir-server.js); if the brain has turned muting on, the ir service mutes
the TV when the connection opens and unmutes it when it closes. This side can't send any code,
nor tell whether muting is on: the connection says only that a conversation is going on.
"""
import logging
from contextlib import asynccontextmanager
class Muting:
"""during(): for the length of a conversation, the ir service may mute the TV.
open_unix_connection is asyncio's, bound to the ir service's conversation socket; log, logging.info.
>>> import asyncio
>>> class Writer:
... def close(self): print("hang up")
... async def wait_closed(self): pass
>>> async def connect(): return print("connect") or (None, Writer())
>>> async def talk(muting):
... async with muting.during():
... print("conversation")
>>> asyncio.run(talk(Muting(connect)))
connect
conversation
hang up
Without the ir service, the conversation goes on, unmuted:
>>> async def refused(): raise ConnectionRefusedError("no ir service")
>>> asyncio.run(talk(Muting(refused, log=print)))
mute: can't reach the ir service: no ir service
conversation
"""
def __init__(self, open_unix_connection, log=logging.info):
self.__connect, self.__log = open_unix_connection, log
@asynccontextmanager
async def during(self):
writer = None
try:
_, writer = await self.__connect()
except OSError as err:
self.__log(f"mute: can't reach the ir service: {err}")
try:
yield
finally:
if writer:
try:
writer.close()
await writer.wait_closed()
except OSError as err:
self.__log(f"mute: hanging up: {err}")