Skip to content

Commit

Permalink
feat: Added service for resetting a cost tracker back to zero.
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Mar 8, 2024
1 parent c24db54 commit d28d63b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
10 changes: 9 additions & 1 deletion _docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,12 @@ This service allows the user to turn the tracking on/off for a given [cost track

### Automation Example

For automation examples, please refer to the available [blueprints](./blueprints.md#cost-tracker).
For automation examples, please refer to the available [blueprints](./blueprints.md#cost-tracker).

## octopus_energy.reset_cost_tracker

Resets a given [cost tracker](./setup/cost_tracker.md) sensor back to zero before it's normal reset time.

| Attribute | Optional | Description |
| ------------------------ | -------- | --------------------------------------------------------------------------------------------------------------------- |
| `target.entity_id` | `no` | The name of the cost tracker sensor(s) whose configuration is to be updated. |
12 changes: 11 additions & 1 deletion custom_components/octopus_energy/cost_tracker/cost_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,14 @@ def _reset_if_new_day(self, current: datetime):

return True

return False
return False

@callback
async def async_reset_cost_tracker(self):
"""Resets the sensor"""
self._state = 0
self._attributes["tracked_charges"] = []
self._attributes["untracked_charges"] = []
self._attributes["total_consumption"] = 0

self.async_write_ha_state()
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import logging

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.util.dt import (now)

Expand Down Expand Up @@ -171,4 +171,13 @@ def _reset_if_new_week(self, current: datetime):

return True

return False
return False

@callback
async def async_reset_cost_tracker(self):
"""Resets the sensor"""
self._state = 0
self._attributes["accumulated_data"] = []
self._attributes["total_consumption"] = 0

self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,14 @@ def _reset_if_new_day(self, current: datetime):

return True

return False
return False

@callback
async def async_reset_cost_tracker(self):
"""Resets the sensor"""
self._state = 0
self._attributes["tracked_charges"] = []
self._attributes["untracked_charges"] = []
self._attributes["total_consumption"] = 0

self.async_write_ha_state()
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,14 @@ def _reset_if_new_day(self, current: datetime):

return True

return False
return False

@callback
async def async_reset_cost_tracker(self):
"""Resets the sensor"""
self._state = 0
self._attributes["tracked_charges"] = []
self._attributes["untracked_charges"] = []
self._attributes["total_consumption"] = 0

self.async_write_ha_state()
13 changes: 11 additions & 2 deletions custom_components/octopus_energy/cost_tracker/cost_tracker_week.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import logging

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.util.dt import (now)

Expand Down Expand Up @@ -171,4 +171,13 @@ def _reset_if_new_week(self, current: datetime):

return True

return False
return False

@callback
async def async_reset_cost_tracker(self):
"""Resets the sensor"""
self._state = 0
self._attributes["total_consumption"] = 0
self._attributes["accumulated_data"] = []

self.async_write_ha_state()
11 changes: 11 additions & 0 deletions custom_components/octopus_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ async def async_setup_entry(hass, entry, async_add_entities):
"async_update_cost_tracker_config"
)

platform.async_register_entity_service(
"reset_cost_tracker",
vol.All(
vol.Schema(
{},
extra=vol.ALLOW_EXTRA,
),
),
"async_reset_cost_tracker"
)

async def async_setup_default_sensors(hass: HomeAssistant, config, async_add_entities):
account_id = config[CONFIG_ACCOUNT_ID]

Expand Down
8 changes: 8 additions & 0 deletions custom_components/octopus_energy/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ update_cost_tracker:
description: Determines if the cost tracker should be enabled or disabled.
selector:
boolean:

reset_cost_tracker:
name: Reset cost tracker
description: Resets a given cost tracker back to zero.
target:
entity:
integration: octopus_energy
domain: sensor

0 comments on commit d28d63b

Please sign in to comment.