Skip to content

Commit

Permalink
Tweak switch update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 27, 2023
1 parent 0f23e68 commit e061b39
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
3 changes: 2 additions & 1 deletion custom_components/ohme/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 14 additions & 15 deletions custom_components/ohme/switch.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit e061b39

Please sign in to comment.