From e061b39718a8348b3a7a57fb4b9e1f7a76402647 Mon Sep 17 00:00:00 2001 From: Daniel Raper Date: Wed, 27 Dec 2023 17:29:40 +0000 Subject: [PATCH] Tweak switch update logic --- custom_components/ohme/binary_sensor.py | 3 ++- custom_components/ohme/switch.py | 29 ++++++++++++------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/custom_components/ohme/binary_sensor.py b/custom_components/ohme/binary_sensor.py index aa1ce58..87562df 100644 --- a/custom_components/ohme/binary_sensor.py +++ b/custom_components/ohme/binary_sensor.py @@ -107,7 +107,8 @@ def unique_id(self) -> str: @property def is_on(self) -> bool: if self.coordinator.data and self.coordinator.data["power"]: - self._state = self.coordinator.data["power"]["amp"] > 0 + # Assume the car is actively charging if drawing over 0 watts + self._state = self.coordinator.data["power"]["watt"] > 0 else: self._state = False diff --git a/custom_components/ohme/switch.py b/custom_components/ohme/switch.py index 977e6eb..52eb244 100644 --- a/custom_components/ohme/switch.py +++ b/custom_components/ohme/switch.py @@ -1,5 +1,6 @@ from __future__ import annotations import logging +import asyncio from homeassistant.core import callback, HomeAssistant from homeassistant.helpers.entity import generate_entity_id @@ -63,32 +64,28 @@ def icon(self): 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.""" + is evaluated only when new data is fetched 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._last_updated = utcnow() + 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 asyncio.sleep(1) 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 asyncio.sleep(1) await self.coordinator.async_refresh() class OhmeMaxCharge(CoordinatorEntity[OhmeUpdateCoordinator], SwitchEntity): @@ -127,20 +124,22 @@ def _handle_coordinator_update(self) -> None: else: self._attr_is_on = bool(self.coordinator.data["mode"] == "MAX_CHARGE") + self._last_updated = utcnow() + 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() + # Not very graceful but wait here to avoid the mode coming back as 'CALCULATING' + # It would be nice to simply ignore this state in future and try again after x seconds. + await asyncio.sleep(1) + await self.coordinator.async_refresh() 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() + await asyncio.sleep(1) + await self.coordinator.async_refresh()