|
| 1 | +from __future__ import annotations |
| 2 | +import logging |
| 3 | + |
| 4 | +from homeassistant.core import callback, HomeAssistant |
| 5 | +from homeassistant.helpers.entity import generate_entity_id |
| 6 | + |
| 7 | +from homeassistant.helpers.update_coordinator import ( |
| 8 | + CoordinatorEntity |
| 9 | +) |
| 10 | +from homeassistant.components.switch import SwitchEntity |
| 11 | +from homeassistant.util.dt import (utcnow) |
| 12 | + |
| 13 | +from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATOR |
| 14 | +from .coordinator import OhmeUpdateCoordinator |
| 15 | + |
| 16 | +_LOGGER = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +async def async_setup_entry( |
| 20 | + hass: HomeAssistant, |
| 21 | + config_entry: config_entries.ConfigEntry, |
| 22 | + async_add_entities |
| 23 | +): |
| 24 | + """Setup switches and configure coordinator.""" |
| 25 | + coordinator = hass.data[DOMAIN][DATA_COORDINATOR] |
| 26 | + client = hass.data[DOMAIN][DATA_CLIENT] |
| 27 | + |
| 28 | + buttons = [OhmePauseCharge(coordinator, hass, client), |
| 29 | + OhmeMaxCharge(coordinator, hass, client)] |
| 30 | + |
| 31 | + async_add_entities(buttons, update_before_add=True) |
| 32 | + |
| 33 | + |
| 34 | +class OhmePauseCharge(CoordinatorEntity[OhmeUpdateCoordinator], SwitchEntity): |
| 35 | + """Switch for pausing a charge.""" |
| 36 | + _attr_name = "Ohme Pause Charge" |
| 37 | + |
| 38 | + def __init__(self, coordinator, hass: HomeAssistant, client): |
| 39 | + super().__init__(coordinator=coordinator) |
| 40 | + |
| 41 | + self._client = client |
| 42 | + |
| 43 | + self._state = False |
| 44 | + self._last_updated = None |
| 45 | + self._attributes = {} |
| 46 | + |
| 47 | + self.entity_id = generate_entity_id( |
| 48 | + "switch.{}", "ohme_pause_charge", hass=hass) |
| 49 | + |
| 50 | + self._attr_device_info = client.get_device_info() |
| 51 | + |
| 52 | + @property |
| 53 | + def unique_id(self): |
| 54 | + """The unique ID of the switch.""" |
| 55 | + return self.entity_id |
| 56 | + |
| 57 | + @property |
| 58 | + def icon(self): |
| 59 | + """Icon of the switch.""" |
| 60 | + return "mdi:pause" |
| 61 | + |
| 62 | + @callback |
| 63 | + def _handle_coordinator_update(self) -> None: |
| 64 | + """Determine if charge is paused. |
| 65 | + We handle this differently to the sensors as the state of this switch |
| 66 | + is changed 'optimistically' to stop the switch flicking back then forth.""" |
| 67 | + if self.coordinator.data is None: |
| 68 | + self._attr_is_on = False |
| 69 | + else: |
| 70 | + self._attr_is_on = bool(self.coordinator.data["mode"] == "STOPPED") |
| 71 | + |
| 72 | + self.async_write_ha_state() |
| 73 | + |
| 74 | + async def async_turn_on(self): |
| 75 | + """Turn on the switch.""" |
| 76 | + await self._client.async_pause_charge() |
| 77 | + |
| 78 | + self._attr_is_on = True |
| 79 | + self._last_updated = utcnow() |
| 80 | + self.async_write_ha_state() |
| 81 | + |
| 82 | + await self.coordinator.async_refresh() |
| 83 | + |
| 84 | + async def async_turn_off(self): |
| 85 | + """Turn off the switch.""" |
| 86 | + await self._client.async_resume_charge() |
| 87 | + |
| 88 | + self._attr_is_on = False |
| 89 | + self._last_updated = utcnow() |
| 90 | + self.async_write_ha_state() |
| 91 | + |
| 92 | + await self.coordinator.async_refresh() |
| 93 | + |
| 94 | +class OhmeMaxCharge(CoordinatorEntity[OhmeUpdateCoordinator], SwitchEntity): |
| 95 | + """Switch for pausing a charge.""" |
| 96 | + _attr_name = "Ohme Max Charge" |
| 97 | + |
| 98 | + def __init__(self, coordinator, hass: HomeAssistant, client): |
| 99 | + super().__init__(coordinator=coordinator) |
| 100 | + |
| 101 | + self._client = client |
| 102 | + |
| 103 | + self._state = False |
| 104 | + self._last_updated = None |
| 105 | + self._attributes = {} |
| 106 | + |
| 107 | + self.entity_id = generate_entity_id( |
| 108 | + "switch.{}", "ohme_max_charge", hass=hass) |
| 109 | + |
| 110 | + self._attr_device_info = client.get_device_info() |
| 111 | + |
| 112 | + @property |
| 113 | + def unique_id(self): |
| 114 | + """The unique ID of the switch.""" |
| 115 | + return self.entity_id |
| 116 | + |
| 117 | + @property |
| 118 | + def icon(self): |
| 119 | + """Icon of the switch.""" |
| 120 | + return "mdi:battery-arrow-up" |
| 121 | + |
| 122 | + @callback |
| 123 | + def _handle_coordinator_update(self) -> None: |
| 124 | + """Determine if we are max charging.""" |
| 125 | + if self.coordinator.data is None: |
| 126 | + self._attr_is_on = False |
| 127 | + else: |
| 128 | + self._attr_is_on = bool(self.coordinator.data["mode"] == "MAX_CHARGE") |
| 129 | + |
| 130 | + self.async_write_ha_state() |
| 131 | + |
| 132 | + async def async_turn_on(self): |
| 133 | + """Turn on the switch.""" |
| 134 | + await self._client.async_max_charge() |
| 135 | + |
| 136 | + self._attr_is_on = True |
| 137 | + self._last_updated = utcnow() |
| 138 | + self.async_write_ha_state() |
| 139 | + |
| 140 | + async def async_turn_off(self): |
| 141 | + """Turn off the switch.""" |
| 142 | + await self._client.async_stop_max_charge() |
| 143 | + |
| 144 | + self._attr_is_on = False |
| 145 | + self._last_updated = utcnow() |
| 146 | + self.async_write_ha_state() |
0 commit comments