diff --git a/morse-code-blinker/README.md b/morse-code-blinker/README.md new file mode 100644 index 0000000..d23d352 --- /dev/null +++ b/morse-code-blinker/README.md @@ -0,0 +1,108 @@ +# Morse Code Blinker + +A Home Assistant custom integration that blinks lights in morse code. + +## Installation + +### HACS Installation + +1. Open HACS in Home Assistant +2. Go to "Integrations" +3. Click the three dots menu (⋮) in the top right +4. Select "Custom repositories" +5. Add this repository URL: `https://git.falkvinge.net/rick/home-assistant/morse-code-blinker` +6. Select "Integration" as the category +7. Click "Add" +8. Find "Morse Code Blinker" in the integrations list +9. Click "Download" +10. Restart Home Assistant + +### Manual Installation + +1. Copy the `custom_components/morse_code_blinker` folder to your Home Assistant `custom_components` directory +2. Restart Home Assistant + +## Usage + +After installation, the integration provides a service `morse_code_blinker.blink` that can be used to blink any light entity in morse code. + +### Service Parameters + +- `entity_id` (required): The entity ID of the light to blink (e.g., `light.living_room`) +- `message` (required): The text message to convert to morse code +- `dot_duration` (optional): Duration of a dot in seconds (default: 0.2) +- `dash_duration` (optional): Duration of a dash in seconds (default: 0.6) +- `element_gap` (optional): Gap between dots/dashes within a letter in seconds (default: 0.2) +- `letter_gap` (optional): Gap between letters in seconds (default: 0.6) +- `word_gap` (optional): Gap between words in seconds (default: 1.4) + +### Examples + +#### Using the Developer Tools + +1. Go to Developer Tools > Services +2. Select `morse_code_blinker.blink` +3. Enter the following YAML: + +```yaml +entity_id: light.living_room +message: "SOS" +``` + +#### Using Automations + +```yaml +automation: + - alias: "Blink SOS on doorbell" + trigger: + - platform: state + entity_id: binary_sensor.doorbell + to: "on" + action: + - service: morse_code_blinker.blink + data: + entity_id: light.front_porch + message: "SOS" +``` + +#### Using Scripts + +```yaml +script: + blink_hello: + alias: "Blink Hello" + sequence: + - service: morse_code_blinker.blink + data: + entity_id: light.bedroom + message: "Hello World" +``` + +#### Custom Timing + +```yaml +service: morse_code_blinker.blink +data: + entity_id: light.kitchen + message: "FAST" + dot_duration: 0.1 + dash_duration: 0.3 + element_gap: 0.1 + letter_gap: 0.3 + word_gap: 0.7 +``` + +## Supported Characters + +The integration supports: +- Letters (A-Z) +- Numbers (0-9) +- Common punctuation: `. , ? ' ! / ( ) & : ; = + - _ " $ @` +- Spaces (for word separation) + +## Notes + +- The light will be turned on and off automatically during the morse code sequence +- The service is non-blocking but will take time to complete based on the message length +- Make sure the light entity supports `turn_on` and `turn_off` services + diff --git a/morse-code-blinker/custom_components/morse_code_blinker/__init__.py b/morse-code-blinker/custom_components/morse_code_blinker/__init__.py new file mode 100644 index 0000000..2540591 --- /dev/null +++ b/morse-code-blinker/custom_components/morse_code_blinker/__init__.py @@ -0,0 +1,30 @@ +"""The Morse Code Blinker integration.""" +from __future__ import annotations + +from homeassistant.core import HomeAssistant +from homeassistant.helpers.typing import ConfigType + +DOMAIN = "morse_code_blinker" + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the Morse Code Blinker component.""" + # Register the service + async def handle_morse_code_blink(call): + """Handle the morse_code_blink service call.""" + from .service import async_blink_morse_code + + # Combine target entity_ids with data + data = dict(call.data) + if call.target: + if "entity_id" not in data: + data["entity_id"] = call.target.get("entity_id") + elif isinstance(data["entity_id"], str): + # If entity_id is a string, convert to list for consistency + data["entity_id"] = [data["entity_id"]] + + await async_blink_morse_code(hass, data) + + hass.services.async_register(DOMAIN, "blink", handle_morse_code_blink) + return True + diff --git a/morse-code-blinker/custom_components/morse_code_blinker/manifest.json b/morse-code-blinker/custom_components/morse_code_blinker/manifest.json new file mode 100644 index 0000000..816819c --- /dev/null +++ b/morse-code-blinker/custom_components/morse_code_blinker/manifest.json @@ -0,0 +1,11 @@ +{ + "domain": "morse_code_blinker", + "name": "Morse Code Blinker", + "version": "1.0.0", + "documentation": "https://git.falkvinge.net/rick/home-assistant/morse-code-blinker", + "issue_tracker": "https://git.falkvinge.net/rick/home-assistant/morse-code-blinker/issues", + "codeowners": ["@rick"], + "requirements": [], + "iot_class": "local_push" +} + diff --git a/morse-code-blinker/custom_components/morse_code_blinker/service.py b/morse-code-blinker/custom_components/morse_code_blinker/service.py new file mode 100644 index 0000000..d2d0f0d --- /dev/null +++ b/morse-code-blinker/custom_components/morse_code_blinker/service.py @@ -0,0 +1,140 @@ +"""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) + diff --git a/morse-code-blinker/custom_components/morse_code_blinker/services.yaml b/morse-code-blinker/custom_components/morse_code_blinker/services.yaml new file mode 100644 index 0000000..572b75e --- /dev/null +++ b/morse-code-blinker/custom_components/morse_code_blinker/services.yaml @@ -0,0 +1,64 @@ +blink: + name: Blink + description: Blink a light entity in morse code + target: + entity: + domain: light + fields: + message: + name: Message + description: The text message to convert to morse code + required: true + selector: + text: + dot_duration: + name: Dot Duration + description: Duration of a dot in seconds + default: 0.2 + selector: + number: + min: 0.05 + max: 2.0 + step: 0.05 + unit_of_measurement: s + dash_duration: + name: Dash Duration + description: Duration of a dash in seconds + default: 0.6 + selector: + number: + min: 0.15 + max: 6.0 + step: 0.05 + unit_of_measurement: s + element_gap: + name: Element Gap + description: Gap between dots/dashes within a letter in seconds + default: 0.2 + selector: + number: + min: 0.05 + max: 2.0 + step: 0.05 + unit_of_measurement: s + letter_gap: + name: Letter Gap + description: Gap between letters in seconds + default: 0.6 + selector: + number: + min: 0.15 + max: 6.0 + step: 0.05 + unit_of_measurement: s + word_gap: + name: Word Gap + description: Gap between words in seconds + default: 1.4 + selector: + number: + min: 0.35 + max: 14.0 + step: 0.05 + unit_of_measurement: s + diff --git a/morse-code-blinker/hacs.json b/morse-code-blinker/hacs.json new file mode 100644 index 0000000..520b6c1 --- /dev/null +++ b/morse-code-blinker/hacs.json @@ -0,0 +1,7 @@ +{ + "name": "Morse Code Blinker", + "domains": ["morse_code_blinker"], + "iot_class": "Local Push", + "homeassistant": "2023.1.0" +} +