Skip to content

Commit

Permalink
Adding 2-way sync (SD-to-HAss) for state_entity_id (#184)
Browse files Browse the repository at this point in the history
* Adding 2-way sync to state_entity_id

* call_service is async

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Factor out function

---------

Co-authored-by: Bas Nijholt <[email protected]>
  • Loading branch information
bogd and basnijholt authored May 2, 2024
1 parent 181910b commit 45ea80d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion home_assistant_streamdeck_yaml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
"""Home Assistant Stream Deck integration."""

from __future__ import annotations

import asyncio
Expand Down Expand Up @@ -1520,6 +1521,23 @@ def turn_off(config: Config, deck: StreamDeck) -> None:
deck.set_brightness(0)


async def _sync_input_boolean(
state_entity_id: str | None,
websocket: websockets.WebSocketClientProtocol,
state: Literal["on", "off"],
) -> None:
"""Sync the input boolean state with the Stream Deck."""
if (state_entity_id is not None) and (
state_entity_id.split(".")[0] == "input_boolean"
):
await call_service(
websocket,
f"input_boolean.turn_{state}",
{},
{"entity_id": state_entity_id},
)


async def _handle_key_press(
websocket: websockets.WebSocketClientProtocol,
complete_state: StateDict,
Expand All @@ -1529,7 +1547,7 @@ async def _handle_key_press(
) -> None:
if not config._is_on:
turn_on(config, deck, complete_state)
return
await _sync_input_boolean(config.state_entity_id, websocket, "on")

def update_all() -> None:
deck.reset()
Expand All @@ -1548,6 +1566,7 @@ def update_all() -> None:
return # to skip the _detached_page reset below
elif button.special_type == "turn-off":
turn_off(config, deck)
await _sync_input_boolean(config.state_entity_id, websocket, "off")
elif button.special_type == "light-control":
assert isinstance(button.special_type_data, dict)
page = _light_page(
Expand Down

0 comments on commit 45ea80d

Please sign in to comment.