From 9fa3b631730ba72e9e24b53b4100b807a4b513ed Mon Sep 17 00:00:00 2001 From: Pavel Veretennikov Date: Fri, 24 May 2024 18:16:59 +0300 Subject: [PATCH] feat: expose skip also as buttons --- custom_components/openmower/__init__.py | 1 + custom_components/openmower/button.py | 61 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 custom_components/openmower/button.py diff --git a/custom_components/openmower/__init__.py b/custom_components/openmower/__init__.py index 17a00a3..9137567 100644 --- a/custom_components/openmower/__init__.py +++ b/custom_components/openmower/__init__.py @@ -14,6 +14,7 @@ Platform.LAWN_MOWER, Platform.SENSOR, Platform.BINARY_SENSOR, + Platform.BUTTON, Platform.DEVICE_TRACKER, ] _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/openmower/button.py b/custom_components/openmower/button.py new file mode 100644 index 0000000..a03e119 --- /dev/null +++ b/custom_components/openmower/button.py @@ -0,0 +1,61 @@ +from __future__ import annotations + +import logging + +from homeassistant.components import mqtt +from homeassistant.components.button import ButtonEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_PREFIX +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .entity import OpenMowerMqttEntity + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + # Make sure MQTT integration is enabled and the client is available + if not await mqtt.async_wait_for_mqtt_client(hass): + _LOGGER.error("MQTT integration is not available") + return + + prefix = entry.data[CONF_PREFIX] + async_add_entities( + [ + OpenMowerSkipAreaButton("Skip Area", prefix, "DUMMY", None), + OpenMowerSkipPathButton("Skip Path", prefix, "DUMMY", None), + ] + ) + + +class OpenMowerMqttButtonEntity(OpenMowerMqttEntity, ButtonEntity): + async def async_added_to_hass(self) -> None: + pass + + def _async_robot_state_received(self, msg: mqtt.ReceiveMessage) -> None: + pass + + def _update_state(self, msg): + pass + + +class OpenMowerSkipAreaButton(OpenMowerMqttButtonEntity): + async def async_press(self) -> None: + await mqtt.async_publish( + self.hass, + self._mqtt_topic_prefix + "action", + "mower_logic:mowing/skip_area", + ) + + +class OpenMowerSkipPathButton(OpenMowerMqttButtonEntity): + async def async_press(self) -> None: + await mqtt.async_publish( + self.hass, + self._mqtt_topic_prefix + "action", + "mower_logic:mowing/skip_path", + )