Skip to content

Commit

Permalink
Add solar boost switch
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Oct 3, 2024
1 parent 3aa80cd commit 646a81c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ This integration exposes the following entities:
* Lock Buttons - Locks buttons on charger
* Require Approval - Require approval to start a charge
* Sleep When Inactive - Charger screen & lights will automatically turn off
* Solar Boost
* Switches (Charge state) - **These are only functional when a car is connected**
* Max Charge - Forces the connected car to charge regardless of set schedule
* Pause Charge - Pauses an ongoing charge
Expand Down
10 changes: 8 additions & 2 deletions custom_components/ohme/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, email, password):
self._ct_connected = False
self._provision_date = None
self._disable_cap = False
self._solar_capable = False

# Authentication
self._token_birth = 0
Expand Down Expand Up @@ -169,6 +170,9 @@ def is_capable(self, capability):
"""Return whether or not this model has a given capability."""
return bool(self._capabilities[capability])

def solar_capable(self):
return self._solar_capable

def cap_available(self):
return not self._disable_cap

Expand Down Expand Up @@ -322,11 +326,13 @@ async def async_update_device_info(self, is_retry=False):
self._device_info = info
self._provision_date = device['provisioningTs']

self._disable_cap = False

if resp['tariff'] is not None and resp['tariff']['dsrTariff']:
self._disable_cap = True

solar_modes = device['modelCapabilities']['solarModes']
if isinstance(solar_modes, list) and len(solar_modes) == 1:
self._solar_capable = True

return True

async def async_get_charge_statistics(self):
Expand Down
64 changes: 62 additions & 2 deletions custom_components/ohme/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ async def async_setup_entry(
client = hass.data[DOMAIN][DATA_CLIENT]

switches = [OhmePauseChargeSwitch(coordinator, hass, client),
OhmeMaxChargeSwitch(coordinator, hass, client),
OhmeMaxChargeSwitch(coordinator, hass, client)
]

if client.cap_available():
switches.append(
OhmePriceCapSwitch(accountinfo_coordinator, hass, client)
)


if client.solar_capable():
switches.append(
OhmeSolarBoostSwitch(accountinfo_coordinator, hass, client)
)
if client.is_capable("buttonsLockable"):
switches.append(
OhmeConfigurationSwitch(
Expand Down Expand Up @@ -231,6 +235,62 @@ async def async_turn_off(self):
await self.coordinator.async_refresh()


class OhmeSolarBoostSwitch(CoordinatorEntity[OhmeAccountInfoCoordinator], SwitchEntity):
"""Switch for changing configuration options."""

def __init__(self, coordinator, hass: HomeAssistant, client):
super().__init__(coordinator=coordinator)

self._client = client

self._state = False
self._last_updated = None
self._attributes = {}

self._attr_name = "Solar Boost"
self.entity_id = generate_entity_id(
"switch.{}", "ohme_solar_boost", hass=hass)

self._attr_device_info = client.get_device_info()

@property
def unique_id(self):
"""The unique ID of the switch."""
return self._client.get_unique_id("solarMode")

@property
def icon(self):
"""Icon of the switch."""
return "mdi:solar-power"

@callback
def _handle_coordinator_update(self) -> None:
"""Determine configuration value."""
if self.coordinator.data is None:
self._attr_is_on = None
else:
settings = self.coordinator.data["chargeDevices"][0]["optionalSettings"]
self._attr_is_on = bool(settings["solarMode"] == "ZERO_EXPORT")

self._last_updated = utcnow()

self.async_write_ha_state()

async def async_turn_on(self):
"""Turn on the switch."""
await self._client.async_set_configuration_value({"solarMode": "ZERO_EXPORT"})

await asyncio.sleep(1)
await self.coordinator.async_refresh()

async def async_turn_off(self):
"""Turn off the switch."""
await self._client.async_set_configuration_value({"solarMode": "IGNORE"})

await asyncio.sleep(1)
await self.coordinator.async_refresh()


class OhmePriceCapSwitch(CoordinatorEntity[OhmeAccountInfoCoordinator], SwitchEntity):
"""Switch for enabling price cap."""
_attr_name = "Enable Price Cap"
Expand Down

0 comments on commit 646a81c

Please sign in to comment.