Skip to content

Commit

Permalink
New release (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave authored Jan 4, 2025
2 parents eeb3047 + 79fe1a7 commit 1cc8d8a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 34 deletions.
29 changes: 22 additions & 7 deletions custom_components/octopus_energy/intelligent/bump_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class OctopusEnergyIntelligentBumpCharge(CoordinatorEntity, SwitchEntity, OctopusEnergyIntelligentSensor, RestoreEntity):
"""Switch for turning intelligent bump charge on and off."""

def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str):
def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str, is_mocked: bool):
"""Init sensor."""
# Pass coordinator to base class
CoordinatorEntity.__init__(self, coordinator)
Expand All @@ -36,6 +36,7 @@ def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiCli
self._client = client
self._account_id = account_id
self._attributes = {}
self._is_mocked = is_mocked
self.entity_id = generate_entity_id("switch.{}", self.unique_id, hass=hass)

@property
Expand Down Expand Up @@ -77,18 +78,32 @@ def _handle_coordinator_update(self) -> None:

async def async_turn_on(self):
"""Turn on the switch."""
await self._client.async_turn_on_intelligent_bump_charge(
self._account_id
)
try:
await self._client.async_turn_on_intelligent_bump_charge(
self._account_id
)
except Exception as e:
if self._is_mocked:
_LOGGER.warning(f'Suppress async_turn_on error due to mocking mode: {e}')
else:
raise

self._state = True
self._last_updated = utcnow()
self.async_write_ha_state()

async def async_turn_off(self):
"""Turn off the switch."""
await self._client.async_turn_off_intelligent_bump_charge(
self._account_id
)
try:
await self._client.async_turn_off_intelligent_bump_charge(
self._account_id
)
except Exception as e:
if self._is_mocked:
_LOGGER.warning(f'Suppress async_turn_off error due to mocking mode: {e}')
else:
raise

self._state = False
self._last_updated = utcnow()
self.async_write_ha_state()
Expand Down
20 changes: 14 additions & 6 deletions custom_components/octopus_energy/intelligent/charge_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class OctopusEnergyIntelligentChargeTarget(CoordinatorEntity, RestoreNumber, OctopusEnergyIntelligentSensor):
"""Sensor for setting the target percentage for car charging."""

def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str):
def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str, is_mocked: bool):
"""Init sensor."""
# Pass coordinator to base class
CoordinatorEntity.__init__(self, coordinator)
Expand All @@ -35,6 +35,7 @@ def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiCli
self._client = client
self._account_id = account_id
self._attributes = {}
self._is_mocked = is_mocked
self.entity_id = generate_entity_id("number.{}", self.unique_id, hass=hass)

self._attr_native_min_value = 10
Expand Down Expand Up @@ -92,11 +93,18 @@ def _handle_coordinator_update(self) -> None:
async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
if value and value % self._attr_native_step == 0:
await self._client.async_update_intelligent_car_target_percentage(
self._account_id,
self._device.id,
int(value)
)
try:
await self._client.async_update_intelligent_car_target_percentage(
self._account_id,
self._device.id,
int(value)
)
except Exception as e:
if self._is_mocked:
_LOGGER.warning(f'Suppress async_set_native_value error due to mocking mode: {e}')
else:
raise

self._state = value
self._last_updated = utcnow()
self.async_write_ha_state()
Expand Down
29 changes: 22 additions & 7 deletions custom_components/octopus_energy/intelligent/smart_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class OctopusEnergyIntelligentSmartCharge(CoordinatorEntity, SwitchEntity, OctopusEnergyIntelligentSensor, RestoreEntity):
"""Switch for turning intelligent smart charge on and off."""

def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str):
def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str, is_mocked: bool):
"""Init sensor."""
# Pass coordinator to base class
CoordinatorEntity.__init__(self, coordinator)
Expand All @@ -35,6 +35,7 @@ def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiCli
self._client = client
self._account_id = account_id
self._attributes = {}
self._is_mocked = is_mocked
self.entity_id = generate_entity_id("switch.{}", self.unique_id, hass=hass)

@property
Expand Down Expand Up @@ -76,18 +77,32 @@ def _handle_coordinator_update(self) -> None:

async def async_turn_on(self):
"""Turn on the switch."""
await self._client.async_turn_on_intelligent_smart_charge(
self._account_id
)
try:
await self._client.async_turn_on_intelligent_smart_charge(
self._account_id
)
except Exception as e:
if self._is_mocked:
_LOGGER.warning(f'Suppress async_turn_on error due to mocking mode: {e}')
else:
raise

self._state = True
self._last_updated = utcnow()
self.async_write_ha_state()

async def async_turn_off(self):
"""Turn off the switch."""
await self._client.async_turn_off_intelligent_smart_charge(
self._account_id
)
try:
await self._client.async_turn_off_intelligent_smart_charge(
self._account_id
)
except Exception as e:
if self._is_mocked:
_LOGGER.warning(f'Suppress async_turn_off error due to mocking mode: {e}')
else:
raise

self._state = False
self._last_updated = utcnow()
self.async_write_ha_state()
Expand Down
22 changes: 15 additions & 7 deletions custom_components/octopus_energy/intelligent/target_time_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class OctopusEnergyIntelligentTargetTimeSelect(CoordinatorEntity, SelectEntity, OctopusEnergyIntelligentSensor, RestoreEntity):
"""Sensor for setting the target time to charge the car to the desired percentage."""

def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str):
def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiClient, device, account_id: str, is_mocked: bool):
"""Init sensor."""
# Pass coordinator to base class
CoordinatorEntity.__init__(self, coordinator)
Expand All @@ -37,6 +37,7 @@ def __init__(self, hass: HomeAssistant, coordinator, client: OctopusEnergyApiCli
self._client = client
self._account_id = account_id
self._attributes = {}
self._is_mocked = is_mocked
self.entity_id = generate_entity_id("select.{}", self.unique_id, hass=hass)

self._options = []
Expand Down Expand Up @@ -92,12 +93,19 @@ async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
parts = option.split(":")
value = time(int(parts[0]), int(parts[1]))
await self._client.async_update_intelligent_car_target_time(
self._account_id,
self._device.id,
value,
)
self._state = value
try:
await self._client.async_update_intelligent_car_target_time(
self._account_id,
self._device.id,
value,
)
except Exception as e:
if self._is_mocked:
_LOGGER.warning(f'Suppress async_select_option error due to mocking mode: {e}')
else:
raise

self._state = option
self._last_updated = utcnow()
self.async_write_ha_state()

Expand Down
6 changes: 5 additions & 1 deletion custom_components/octopus_energy/number.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

from .utils.debug_overrides import async_get_account_debug_override

from .intelligent import get_intelligent_features
from .intelligent.charge_target import OctopusEnergyIntelligentChargeTarget
from .api_client.intelligent_device import IntelligentDevice
Expand Down Expand Up @@ -37,13 +39,15 @@ async def async_setup_intelligent_sensors(hass, config, async_add_entities):

account_id = config[CONFIG_ACCOUNT_ID]

account_debug_override = await async_get_account_debug_override(hass, account_id)

client = hass.data[DOMAIN][account_id][DATA_CLIENT]
intelligent_device: IntelligentDevice = hass.data[DOMAIN][account_id][DATA_INTELLIGENT_DEVICE] if DATA_INTELLIGENT_DEVICE in hass.data[DOMAIN][account_id] else None
if intelligent_device is not None:
intelligent_features = get_intelligent_features(intelligent_device.provider)
settings_coordinator = hass.data[DOMAIN][account_id][DATA_INTELLIGENT_SETTINGS_COORDINATOR]

if intelligent_features.charge_limit_supported == True:
entities.append(OctopusEnergyIntelligentChargeTarget(hass, settings_coordinator, client, intelligent_device, account_id))
entities.append(OctopusEnergyIntelligentChargeTarget(hass, settings_coordinator, client, intelligent_device, account_id, account_debug_override.mock_intelligent_controls if account_debug_override is not None else False))

async_add_entities(entities)
5 changes: 4 additions & 1 deletion custom_components/octopus_energy/select.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from .utils.debug_overrides import async_get_account_debug_override
from .intelligent.target_time_select import OctopusEnergyIntelligentTargetTimeSelect
from .api_client import OctopusEnergyApiClient
from .intelligent import get_intelligent_features
Expand Down Expand Up @@ -38,6 +39,8 @@ async def async_setup_intelligent_sensors(hass, config, async_add_entities):

account_id = config[CONFIG_ACCOUNT_ID]

account_debug_override = await async_get_account_debug_override(hass, account_id)

client = hass.data[DOMAIN][account_id][DATA_CLIENT]
intelligent_device: IntelligentDevice = hass.data[DOMAIN][account_id][DATA_INTELLIGENT_DEVICE] if DATA_INTELLIGENT_DEVICE in hass.data[DOMAIN][account_id] else None
if intelligent_device is not None:
Expand All @@ -46,6 +49,6 @@ async def async_setup_intelligent_sensors(hass, config, async_add_entities):
client: OctopusEnergyApiClient = hass.data[DOMAIN][account_id][DATA_CLIENT]

if intelligent_features.ready_time_supported:
entities.append(OctopusEnergyIntelligentTargetTimeSelect(hass, settings_coordinator, client, intelligent_device, account_id))
entities.append(OctopusEnergyIntelligentTargetTimeSelect(hass, settings_coordinator, client, intelligent_device, account_id, account_debug_override.mock_intelligent_controls if account_debug_override is not None else False))

async_add_entities(entities)
9 changes: 4 additions & 5 deletions custom_components/octopus_energy/switch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from .utils.debug_overrides import async_get_account_debug_override
from .intelligent.smart_charge import OctopusEnergyIntelligentSmartCharge
from .intelligent.bump_charge import OctopusEnergyIntelligentBumpCharge
from .api_client import OctopusEnergyApiClient
Expand Down Expand Up @@ -40,10 +41,8 @@ async def async_setup_intelligent_sensors(hass, config, async_add_entities):

account_id = config[CONFIG_ACCOUNT_ID]

account_result = hass.data[DOMAIN][account_id][DATA_ACCOUNT]
account_info = account_result.account if account_result is not None else None
account_debug_override = await async_get_account_debug_override(hass, account_id)

account_id = account_info["id"]
client = hass.data[DOMAIN][account_id][DATA_CLIENT]
intelligent_device: IntelligentDevice = hass.data[DOMAIN][account_id][DATA_INTELLIGENT_DEVICE] if DATA_INTELLIGENT_DEVICE in hass.data[DOMAIN][account_id] else None
if intelligent_device is not None:
Expand All @@ -53,9 +52,9 @@ async def async_setup_intelligent_sensors(hass, config, async_add_entities):
client: OctopusEnergyApiClient = hass.data[DOMAIN][account_id][DATA_CLIENT]

if intelligent_features.bump_charge_supported:
entities.append(OctopusEnergyIntelligentSmartCharge(hass, settings_coordinator, client, intelligent_device, account_id))
entities.append(OctopusEnergyIntelligentSmartCharge(hass, settings_coordinator, client, intelligent_device, account_id, account_debug_override.mock_intelligent_controls if account_debug_override is not None else False))

if intelligent_features.smart_charge_supported:
entities.append(OctopusEnergyIntelligentBumpCharge(hass, dispatches_coordinator, client, intelligent_device, account_id))
entities.append(OctopusEnergyIntelligentBumpCharge(hass, dispatches_coordinator, client, intelligent_device, account_id, account_debug_override.mock_intelligent_controls if account_debug_override is not None else False))

async_add_entities(entities)

0 comments on commit 1cc8d8a

Please sign in to comment.