diff --git a/apps/apps.yaml b/apps/apps.yaml index 6724d55..861924a 100644 --- a/apps/apps.yaml +++ b/apps/apps.yaml @@ -9,3 +9,7 @@ gone: - light.bar - light.h6076 - light.h6076_2 + +blue-button: + module: media_button + class: Button \ No newline at end of file diff --git a/apps/media_button.py b/apps/media_button.py new file mode 100644 index 0000000..561ada0 --- /dev/null +++ b/apps/media_button.py @@ -0,0 +1,38 @@ +from enum import Enum +from typing import Any + +from appdaemon.plugins.hass import Hass + + +class ButtonPress(str, Enum): + SINGLE = 'single' + DOUBLE = 'double' + HOLD = 'hold' + + +class Button(Hass): + def initialize(self): + self.set_log_level('DEBUG') + self.listen_state( + self.handle_button, + entity_id='sensor.blue_lamp_button_action', + new=lambda s: s.strip() != '', + ) + + def handle_button(self, entity: str, attribute: str, old: Any, new: Any, **kwargs: Any) -> None: + match new: + case ButtonPress.SINGLE: + self.log('Single press') + self.call_service('media_player/media_play_pause', entity_id='media_player.living_room_tv') + case ButtonPress.DOUBLE: + self.log('Double') + self.call_service( + 'scene/apply', + entities={ + 'light.bar': {'state': 'on', 'brightness': 10}, + 'light.living_room_stick': {'state': 'on', 'brightness': 100}, + 'light.server_lamp': {'state': 'on', 'brightness': 25}, + }, + ) + case ButtonPress.HOLD: + self.log('Hold down')