From f4a17253589de6031f6a4cd67a7d74144f757f95 Mon Sep 17 00:00:00 2001 From: Jonas Karlsson Date: Sun, 18 Sep 2022 23:03:18 +0200 Subject: [PATCH] Added manual start and stop buttons. --- .github/release-drafter.yml | 3 + custom_components/ev_smart_charging/button.py | 55 +++++++++++++++++++ custom_components/ev_smart_charging/const.py | 5 +- .../ev_smart_charging/coordinator.py | 24 +++++--- 4 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 custom_components/ev_smart_charging/button.py diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml index 21a1679..8b74311 100644 --- a/.github/release-drafter.yml +++ b/.github/release-drafter.yml @@ -34,6 +34,9 @@ autolabeler: - label: "enhancement" branch: - '/feature\/.+/' + - label: "minor" + branch: + - '/feature\/.+/' - label: "bugfix" branch: - '/fix\/.+/' diff --git a/custom_components/ev_smart_charging/button.py b/custom_components/ev_smart_charging/button.py new file mode 100644 index 0000000..63a8515 --- /dev/null +++ b/custom_components/ev_smart_charging/button.py @@ -0,0 +1,55 @@ +"""Button platform for EV Smart Charging.""" +import logging + +from homeassistant.components.button import ButtonEntity +from homeassistant.core import HomeAssistant + +from .const import BUTTON, DOMAIN, ENTITY_NAME_START_BUTTON, ENTITY_NAME_STOP_BUTTON +from .coordinator import EVSmartChargingCoordinator +from .entity import EVSmartChargingEntity + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass: HomeAssistant, entry, async_add_devices): + """Setup button platform.""" + _LOGGER.debug("EVSmartCharging.button.py") + coordinator = hass.data[DOMAIN][entry.entry_id] + buttons = [] + buttons.append(EVSmartChargingButtonStart(entry, coordinator)) + buttons.append(EVSmartChargingButtonStop(entry, coordinator)) + async_add_devices(buttons) + + +class EVSmartChargingButton(EVSmartChargingEntity, ButtonEntity): + """EV Smart Charging button class.""" + + def __init__(self, entry, coordinator: EVSmartChargingCoordinator): + _LOGGER.debug("EVSmartChargingButton.__init__()") + super().__init__(entry) + self.coordinator = coordinator + id_name = self._attr_name.replace(" ", "").lower() + self._attr_unique_id = ".".join([entry.entry_id, BUTTON, id_name]) + _LOGGER.debug("self._attr_unique_id = %s", self._attr_unique_id) + + +class EVSmartChargingButtonStart(EVSmartChargingButton): + """EV Smart Charging start button class.""" + + _attr_name = ENTITY_NAME_START_BUTTON + + def press(self) -> None: + """Press the button.""" + _LOGGER.debug("EVSmartChargingButtonStart.press()") + self.coordinator.turn_on_charging() + + +class EVSmartChargingButtonStop(EVSmartChargingButton): + """EV Smart Charging start button class.""" + + _attr_name = ENTITY_NAME_STOP_BUTTON + + def press(self) -> None: + """Press the button.""" + _LOGGER.debug("EVSmartChargingButtonStop.press()") + self.coordinator.turn_off_charging() diff --git a/custom_components/ev_smart_charging/const.py b/custom_components/ev_smart_charging/const.py index b4d06ea..b4f14b2 100644 --- a/custom_components/ev_smart_charging/const.py +++ b/custom_components/ev_smart_charging/const.py @@ -12,7 +12,8 @@ # Platforms SENSOR = "sensor" SWITCH = "switch" -PLATFORMS = [SWITCH, SENSOR] +BUTTON = "button" +PLATFORMS = [SWITCH, SENSOR, BUTTON] PLATFORM_NORDPOOL = "nordpool" PLATFORM_VW = "volkswagen_we_connect_id" PLATFORM_OCPP = "ocpp" @@ -20,6 +21,8 @@ # Entity names ENTITY_NAME_CHARGING_SENSOR = "Charging" ENTITY_NAME_ACTIVE_SWITCH = "Activate smart charging" +ENTITY_NAME_START_BUTTON = "Start charging" +ENTITY_NAME_STOP_BUTTON = "Stop charging" # Configuration and options CONF_NORDPOOL_SENSOR = "nordpool_sensor" diff --git a/custom_components/ev_smart_charging/coordinator.py b/custom_components/ev_smart_charging/coordinator.py index d561f75..20a1690 100644 --- a/custom_components/ev_smart_charging/coordinator.py +++ b/custom_components/ev_smart_charging/coordinator.py @@ -98,16 +98,24 @@ def new_hour(self, date_time: datetime = None): # pylint: disable=unused-argume _LOGGER.debug("current_value = %s", current_value) if turn_on_charging and not current_value: # Turn on charging - _LOGGER.debug("Turn on charging") - self.sensor.native_value = STATE_ON - if self.charger_switch is not None: - self.hass.states.async_set(self.charger_switch, STATE_ON) + self.turn_on_charging() if not turn_on_charging and current_value: # Turn off charging - _LOGGER.debug("Turn off charging") - self.sensor.native_value = STATE_OFF - if self.charger_switch is not None: - self.hass.states.async_set(self.charger_switch, STATE_OFF) + self.turn_off_charging() + + def turn_on_charging(self): + """Turn on charging""" + _LOGGER.debug("Turn on charging") + self.sensor.native_value = STATE_ON + if self.charger_switch is not None: + self.hass.states.async_set(self.charger_switch, STATE_ON) + + def turn_off_charging(self): + """Turn off charging""" + _LOGGER.debug("Turn off charging") + self.sensor.native_value = STATE_OFF + if self.charger_switch is not None: + self.hass.states.async_set(self.charger_switch, STATE_OFF) def add_sensor(self, sensor: EVSmartChargingSensor): """Set up sensor"""