141 lines
4.1 KiB
Python
141 lines
4.1 KiB
Python
"""Service for blinking lights in morse code."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from typing import Any
|
|
|
|
from homeassistant.core import HomeAssistant, ServiceCall
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
# Morse code dictionary
|
|
MORSE_CODE = {
|
|
"A": ".-",
|
|
"B": "-...",
|
|
"C": "-.-.",
|
|
"D": "-..",
|
|
"E": ".",
|
|
"F": "..-.",
|
|
"G": "--.",
|
|
"H": "....",
|
|
"I": "..",
|
|
"J": ".---",
|
|
"K": "-.-",
|
|
"L": ".-..",
|
|
"M": "--",
|
|
"N": "-.",
|
|
"O": "---",
|
|
"P": ".--.",
|
|
"Q": "--.-",
|
|
"R": ".-.",
|
|
"S": "...",
|
|
"T": "-",
|
|
"U": "..-",
|
|
"V": "...-",
|
|
"W": ".--",
|
|
"X": "-..-",
|
|
"Y": "-.--",
|
|
"Z": "--..",
|
|
"0": "-----",
|
|
"1": ".----",
|
|
"2": "..---",
|
|
"3": "...--",
|
|
"4": "....-",
|
|
"5": ".....",
|
|
"6": "-....",
|
|
"7": "--...",
|
|
"8": "---..",
|
|
"9": "----.",
|
|
".": ".-.-.-",
|
|
",": "--..--",
|
|
"?": "..--..",
|
|
"'": ".----.",
|
|
"!": "-.-.--",
|
|
"/": "-..-.",
|
|
"(": "-.--.",
|
|
")": "-.--.-",
|
|
"&": ".-...",
|
|
":": "---...",
|
|
";": "-.-.-.",
|
|
"=": "-...-",
|
|
"+": ".-.-.",
|
|
"-": "-....-",
|
|
"_": "..--.-",
|
|
'"': ".-..-.",
|
|
"$": "...-..-",
|
|
"@": ".--.-.",
|
|
" ": " ",
|
|
}
|
|
|
|
# Timing constants (in seconds)
|
|
DOT_DURATION = 0.2 # Duration of a dot
|
|
DASH_DURATION = 0.6 # Duration of a dash (3x dot)
|
|
ELEMENT_GAP = 0.2 # Gap between dots/dashes within a letter
|
|
LETTER_GAP = 0.6 # Gap between letters (3x dot)
|
|
WORD_GAP = 1.4 # Gap between words (7x dot)
|
|
|
|
|
|
def text_to_morse(text: str) -> list[str]:
|
|
"""Convert text to morse code sequence."""
|
|
morse = []
|
|
for char in text.upper():
|
|
if char in MORSE_CODE:
|
|
morse.append(MORSE_CODE[char])
|
|
elif char == " ":
|
|
morse.append(" ") # Word separator
|
|
return morse
|
|
|
|
|
|
async def async_blink_morse_code(hass: HomeAssistant, data: dict[str, Any]) -> None:
|
|
"""Blink a light entity in morse code."""
|
|
entity_ids = data.get("entity_id")
|
|
message = data.get("message", "")
|
|
dot_duration = data.get("dot_duration", DOT_DURATION)
|
|
dash_duration = data.get("dash_duration", DASH_DURATION)
|
|
element_gap = data.get("element_gap", ELEMENT_GAP)
|
|
letter_gap = data.get("letter_gap", LETTER_GAP)
|
|
word_gap = data.get("word_gap", WORD_GAP)
|
|
|
|
if not entity_ids:
|
|
raise ValueError("entity_id is required")
|
|
|
|
# Normalize entity_id to list
|
|
if isinstance(entity_ids, str):
|
|
entity_ids = [entity_ids]
|
|
|
|
# Verify entities exist
|
|
for entity_id in entity_ids:
|
|
state = hass.states.get(entity_id)
|
|
if not state:
|
|
raise ValueError(f"Entity {entity_id} not found")
|
|
|
|
# Convert message to morse code
|
|
morse_sequence = text_to_morse(message)
|
|
|
|
# Blink the lights according to morse code
|
|
for i, morse_letter in enumerate(morse_sequence):
|
|
if morse_letter == " ":
|
|
# Word separator: wait for word gap
|
|
await asyncio.sleep(word_gap)
|
|
else:
|
|
# Process each dot/dash in the letter
|
|
for j, element in enumerate(morse_letter):
|
|
if element == ".":
|
|
# Dot: turn on, wait, turn off
|
|
await hass.services.async_call("light", "turn_on", {"entity_id": entity_ids})
|
|
await asyncio.sleep(dot_duration)
|
|
await hass.services.async_call("light", "turn_off", {"entity_id": entity_ids})
|
|
elif element == "-":
|
|
# Dash: turn on, wait longer, turn off
|
|
await hass.services.async_call("light", "turn_on", {"entity_id": entity_ids})
|
|
await asyncio.sleep(dash_duration)
|
|
await hass.services.async_call("light", "turn_off", {"entity_id": entity_ids})
|
|
|
|
# Add gap between elements (except after last element)
|
|
if j < len(morse_letter) - 1:
|
|
await asyncio.sleep(element_gap)
|
|
|
|
# Add gap between letters (except after last letter or before word separator)
|
|
if i < len(morse_sequence) - 1 and morse_sequence[i + 1] != " ":
|
|
await asyncio.sleep(letter_gap)
|
|
|