-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of switches for pause/max charge (#1)
* Add pause charge switch * Add max charge switch * Change update interval
- Loading branch information
Showing
4 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
from __future__ import annotations | ||
import logging | ||
|
||
from homeassistant.core import callback, HomeAssistant | ||
from homeassistant.helpers.entity import generate_entity_id | ||
|
||
from homeassistant.helpers.update_coordinator import ( | ||
CoordinatorEntity | ||
) | ||
from homeassistant.components.switch import SwitchEntity | ||
from homeassistant.util.dt import (utcnow) | ||
|
||
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATOR | ||
from .coordinator import OhmeUpdateCoordinator | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: config_entries.ConfigEntry, | ||
async_add_entities | ||
): | ||
"""Setup switches and configure coordinator.""" | ||
coordinator = hass.data[DOMAIN][DATA_COORDINATOR] | ||
client = hass.data[DOMAIN][DATA_CLIENT] | ||
|
||
buttons = [OhmePauseCharge(coordinator, hass, client), | ||
OhmeMaxCharge(coordinator, hass, client)] | ||
|
||
async_add_entities(buttons, update_before_add=True) | ||
|
||
|
||
class OhmePauseCharge(CoordinatorEntity[OhmeUpdateCoordinator], SwitchEntity): | ||
"""Switch for pausing a charge.""" | ||
_attr_name = "Ohme Pause Charge" | ||
|
||
def __init__(self, coordinator, hass: HomeAssistant, client): | ||
super().__init__(coordinator=coordinator) | ||
|
||
self._client = client | ||
|
||
self._state = False | ||
self._last_updated = None | ||
self._attributes = {} | ||
|
||
self.entity_id = generate_entity_id( | ||
"switch.{}", "ohme_pause_charge", hass=hass) | ||
|
||
self._attr_device_info = client.get_device_info() | ||
|
||
@property | ||
def unique_id(self): | ||
"""The unique ID of the switch.""" | ||
return self.entity_id | ||
|
||
@property | ||
def icon(self): | ||
"""Icon of the switch.""" | ||
return "mdi:pause" | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
"""Determine if charge is paused. | ||
We handle this differently to the sensors as the state of this switch | ||
is changed 'optimistically' to stop the switch flicking back then forth.""" | ||
if self.coordinator.data is None: | ||
self._attr_is_on = False | ||
else: | ||
self._attr_is_on = bool(self.coordinator.data["mode"] == "STOPPED") | ||
|
||
self.async_write_ha_state() | ||
|
||
async def async_turn_on(self): | ||
"""Turn on the switch.""" | ||
await self._client.async_pause_charge() | ||
|
||
self._attr_is_on = True | ||
self._last_updated = utcnow() | ||
self.async_write_ha_state() | ||
|
||
await self.coordinator.async_refresh() | ||
|
||
async def async_turn_off(self): | ||
"""Turn off the switch.""" | ||
await self._client.async_resume_charge() | ||
|
||
self._attr_is_on = False | ||
self._last_updated = utcnow() | ||
self.async_write_ha_state() | ||
|
||
await self.coordinator.async_refresh() | ||
|
||
class OhmeMaxCharge(CoordinatorEntity[OhmeUpdateCoordinator], SwitchEntity): | ||
"""Switch for pausing a charge.""" | ||
_attr_name = "Ohme Max Charge" | ||
|
||
def __init__(self, coordinator, hass: HomeAssistant, client): | ||
super().__init__(coordinator=coordinator) | ||
|
||
self._client = client | ||
|
||
self._state = False | ||
self._last_updated = None | ||
self._attributes = {} | ||
|
||
self.entity_id = generate_entity_id( | ||
"switch.{}", "ohme_max_charge", hass=hass) | ||
|
||
self._attr_device_info = client.get_device_info() | ||
|
||
@property | ||
def unique_id(self): | ||
"""The unique ID of the switch.""" | ||
return self.entity_id | ||
|
||
@property | ||
def icon(self): | ||
"""Icon of the switch.""" | ||
return "mdi:battery-arrow-up" | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
"""Determine if we are max charging.""" | ||
if self.coordinator.data is None: | ||
self._attr_is_on = False | ||
else: | ||
self._attr_is_on = bool(self.coordinator.data["mode"] == "MAX_CHARGE") | ||
|
||
self.async_write_ha_state() | ||
|
||
async def async_turn_on(self): | ||
"""Turn on the switch.""" | ||
await self._client.async_max_charge() | ||
|
||
self._attr_is_on = True | ||
self._last_updated = utcnow() | ||
self.async_write_ha_state() | ||
|
||
async def async_turn_off(self): | ||
"""Turn off the switch.""" | ||
await self._client.async_stop_max_charge() | ||
|
||
self._attr_is_on = False | ||
self._last_updated = utcnow() | ||
self.async_write_ha_state() |