Skip to content

Commit

Permalink
Added manual start and stop buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbkarlsson authored Sep 18, 2022
1 parent 19aad3f commit f4a1725
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ autolabeler:
- label: "enhancement"
branch:
- '/feature\/.+/'
- label: "minor"
branch:
- '/feature\/.+/'
- label: "bugfix"
branch:
- '/fix\/.+/'
Expand Down
55 changes: 55 additions & 0 deletions custom_components/ev_smart_charging/button.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 4 additions & 1 deletion custom_components/ev_smart_charging/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
# 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"

# 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"
Expand Down
24 changes: 16 additions & 8 deletions custom_components/ev_smart_charging/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit f4a1725

Please sign in to comment.