-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added sensors for displaying current total consumption (40 minu…
…tes dev)
- Loading branch information
1 parent
0de0bb3
commit dbb2b5d
Showing
8 changed files
with
432 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
custom_components/octopus_energy/electricity/current_total_consumption.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import logging | ||
|
||
from homeassistant.const import ( | ||
STATE_UNAVAILABLE, | ||
STATE_UNKNOWN, | ||
) | ||
from homeassistant.core import HomeAssistant, callback | ||
|
||
from homeassistant.helpers.update_coordinator import ( | ||
CoordinatorEntity, | ||
) | ||
|
||
from homeassistant.components.sensor import ( | ||
RestoreSensor, | ||
SensorDeviceClass, | ||
SensorStateClass, | ||
) | ||
from homeassistant.const import ( | ||
UnitOfEnergy | ||
) | ||
|
||
from homeassistant.util.dt import (now) | ||
|
||
from ..coordinators.current_consumption import CurrentConsumptionCoordinatorResult | ||
from .base import (OctopusEnergyElectricitySensor) | ||
from ..utils.attributes import dict_to_typed_dict | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
class OctopusEnergyCurrentTotalElectricityConsumption(CoordinatorEntity, OctopusEnergyElectricitySensor, RestoreSensor): | ||
"""Sensor for displaying the current total electricity consumption.""" | ||
|
||
def __init__(self, hass: HomeAssistant, coordinator, meter, point): | ||
"""Init sensor.""" | ||
CoordinatorEntity.__init__(self, coordinator) | ||
|
||
self._state = None | ||
self._last_reset = None | ||
|
||
OctopusEnergyElectricitySensor.__init__(self, hass, meter, point) | ||
|
||
@property | ||
def entity_registry_enabled_default(self) -> bool: | ||
"""Return if the entity should be enabled when first added. | ||
This only applies when fist added to the entity registry. | ||
""" | ||
return self._is_smart_meter | ||
|
||
@property | ||
def unique_id(self): | ||
"""The id of the sensor.""" | ||
return f"octopus_energy_electricity_{self._serial_number}_{self._mpan}_current_total_consumption" | ||
|
||
@property | ||
def name(self): | ||
"""Name of the sensor.""" | ||
return f"Current Total Consumption Electricity ({self._serial_number}/{self._mpan})" | ||
|
||
@property | ||
def device_class(self): | ||
"""The type of sensor""" | ||
return SensorDeviceClass.ENERGY | ||
|
||
@property | ||
def state_class(self): | ||
"""The state class of sensor""" | ||
return SensorStateClass.TOTAL | ||
|
||
@property | ||
def native_unit_of_measurement(self): | ||
"""The unit of measurement of sensor""" | ||
return UnitOfEnergy.KILO_WATT_HOUR | ||
|
||
@property | ||
def icon(self): | ||
"""Icon of the sensor.""" | ||
return "mdi:lightning-bolt" | ||
|
||
@property | ||
def extra_state_attributes(self): | ||
"""Attributes of the sensor.""" | ||
return self._attributes | ||
|
||
@property | ||
def last_reset(self): | ||
"""Return the time when the sensor was last reset, if any.""" | ||
return self._last_reset | ||
|
||
@property | ||
def native_value(self): | ||
return self._state | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
"""Retrieve the current days accumulative consumption""" | ||
current = now() | ||
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None | ||
consumption_data = consumption_result.data if consumption_result is not None else None | ||
|
||
if (consumption_data is not None and len(consumption_data) > 0): | ||
_LOGGER.debug(f"Calculated total electricity consumption for '{self._mpan}/{self._serial_number}'...") | ||
|
||
self._state = consumption_data[-1]["total_consumption"] | ||
self._last_reset = current | ||
|
||
self._attributes = { | ||
"mpan": self._mpan, | ||
"serial_number": self._serial_number, | ||
"is_export": self._is_export, | ||
"is_smart_meter": self._is_smart_meter, | ||
"last_evaluated": current, | ||
"data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None | ||
} | ||
|
||
self._attributes = dict_to_typed_dict(self._attributes) | ||
super()._handle_coordinator_update() | ||
|
||
async def async_added_to_hass(self): | ||
"""Call when entity about to be added to hass.""" | ||
# If not None, we got an initial value. | ||
await super().async_added_to_hass() | ||
state = await self.async_get_last_state() | ||
|
||
if state is not None and self._state is None: | ||
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state | ||
self._attributes = dict_to_typed_dict(state.attributes) | ||
|
||
_LOGGER.debug(f'Restored state: {self._state}') |
124 changes: 124 additions & 0 deletions
124
custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import logging | ||
|
||
from homeassistant.const import ( | ||
STATE_UNAVAILABLE, | ||
STATE_UNKNOWN, | ||
) | ||
from homeassistant.core import HomeAssistant, callback | ||
|
||
from homeassistant.helpers.update_coordinator import ( | ||
CoordinatorEntity, | ||
) | ||
|
||
from homeassistant.components.sensor import ( | ||
RestoreSensor, | ||
SensorDeviceClass, | ||
SensorStateClass, | ||
) | ||
from homeassistant.const import ( | ||
UnitOfVolume | ||
) | ||
|
||
from homeassistant.util.dt import (now) | ||
|
||
from ..coordinators.current_consumption import CurrentConsumptionCoordinatorResult | ||
from .base import (OctopusEnergyGasSensor) | ||
from ..utils.attributes import dict_to_typed_dict | ||
from . import convert_kwh_to_m3 | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
class OctopusEnergyCurrentTotalGasConsumptionCubicMeters(CoordinatorEntity, OctopusEnergyGasSensor, RestoreSensor): | ||
"""Sensor for displaying the current total gas consumption in m3.""" | ||
|
||
def __init__(self, hass: HomeAssistant, coordinator, meter, point, calorific_value: float): | ||
"""Init sensor.""" | ||
CoordinatorEntity.__init__(self, coordinator) | ||
|
||
self._state = None | ||
self._last_reset = None | ||
self._calorific_value = calorific_value | ||
|
||
OctopusEnergyGasSensor.__init__(self, hass, meter, point) | ||
|
||
@property | ||
def entity_registry_enabled_default(self) -> bool: | ||
"""Return if the entity should be enabled when first added. | ||
This only applies when fist added to the entity registry. | ||
""" | ||
return self._is_smart_meter | ||
|
||
@property | ||
def unique_id(self): | ||
"""The id of the sensor.""" | ||
return f"octopus_energy_gas_{self._serial_number}_{self._mprn}_current_total_consumption_m3" | ||
|
||
@property | ||
def name(self): | ||
"""Name of the sensor.""" | ||
return f"Current Total Consumption (m3) Gas ({self._serial_number}/{self._mprn})" | ||
|
||
@property | ||
def device_class(self): | ||
"""The type of sensor""" | ||
return SensorDeviceClass.GAS | ||
|
||
@property | ||
def state_class(self): | ||
"""The state class of sensor""" | ||
return SensorStateClass.TOTAL | ||
|
||
@property | ||
def native_unit_of_measurement(self): | ||
"""The unit of measurement of sensor""" | ||
return UnitOfVolume.CUBIC_METERS | ||
|
||
@property | ||
def icon(self): | ||
"""Icon of the sensor.""" | ||
return "mdi:fire" | ||
|
||
@property | ||
def extra_state_attributes(self): | ||
"""Attributes of the sensor.""" | ||
return self._attributes | ||
|
||
@property | ||
def native_value(self): | ||
return self._state | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
"""Retrieve the current days accumulative consumption""" | ||
current = now() | ||
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None | ||
consumption_data = consumption_result.data if consumption_result is not None else None | ||
|
||
if (consumption_data is not None and len(consumption_data) > 0): | ||
_LOGGER.debug(f"Calculated total gas consumption for '{self._mprn}/{self._serial_number}'...") | ||
|
||
self._state = convert_kwh_to_m3(consumption_data[-1]["total_consumption"], self._calorific_value) if consumption_data[-1]["total_consumption"] is not None else None | ||
|
||
self._attributes = { | ||
"mprn": self._mprn, | ||
"serial_number": self._serial_number, | ||
"is_smart_meter": self._is_smart_meter, | ||
"last_evaluated": current, | ||
"data_last_retrieved": consumption_result.last_retrieved if consumption_result is not None else None | ||
} | ||
|
||
self._attributes = dict_to_typed_dict(self._attributes) | ||
super()._handle_coordinator_update() | ||
|
||
async def async_added_to_hass(self): | ||
"""Call when entity about to be added to hass.""" | ||
# If not None, we got an initial value. | ||
await super().async_added_to_hass() | ||
state = await self.async_get_last_state() | ||
|
||
if state is not None and self._state is None: | ||
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state | ||
self._attributes = dict_to_typed_dict(state.attributes) | ||
|
||
_LOGGER.debug(f'Restored state: {self._state}') |
Oops, something went wrong.