1+ import logging
2+
3+ from homeassistant .const import (
4+ STATE_UNAVAILABLE ,
5+ STATE_UNKNOWN ,
6+ )
7+ from homeassistant .core import HomeAssistant , callback
8+
9+ from homeassistant .helpers .update_coordinator import (
10+ CoordinatorEntity ,
11+ )
12+
13+ from homeassistant .components .sensor import (
14+ RestoreSensor ,
15+ SensorDeviceClass ,
16+ SensorStateClass ,
17+ )
18+ from homeassistant .const import (
19+ UnitOfEnergy
20+ )
21+
22+ from homeassistant .util .dt import (now )
23+
24+ from ..coordinators .current_consumption import CurrentConsumptionCoordinatorResult
25+ from .base import (OctopusEnergyElectricitySensor )
26+ from ..utils .attributes import dict_to_typed_dict
27+
28+ _LOGGER = logging .getLogger (__name__ )
29+
30+ class OctopusEnergyCurrentTotalElectricityExport (CoordinatorEntity , OctopusEnergyElectricitySensor , RestoreSensor ):
31+ """Sensor for displaying the current total electricity export."""
32+
33+ def __init__ (self , hass : HomeAssistant , coordinator , meter , point ):
34+ """Init sensor."""
35+ CoordinatorEntity .__init__ (self , coordinator )
36+
37+ self ._state = None
38+ self ._last_reset = None
39+
40+ OctopusEnergyElectricitySensor .__init__ (self , hass , meter , point )
41+
42+ @property
43+ def entity_registry_enabled_default (self ) -> bool :
44+ """Return if the entity should be enabled when first added.
45+
46+ This only applies when fist added to the entity registry.
47+ """
48+ return False
49+
50+ @property
51+ def unique_id (self ):
52+ """The id of the sensor."""
53+ return f"octopus_energy_electricity_{ self ._serial_number } _{ self ._mpan } _current_total_export"
54+
55+ @property
56+ def name (self ):
57+ """Name of the sensor."""
58+ return f"Current Total Export Electricity ({ self ._serial_number } /{ self ._mpan } )"
59+
60+ @property
61+ def device_class (self ):
62+ """The type of sensor"""
63+ return SensorDeviceClass .ENERGY
64+
65+ @property
66+ def state_class (self ):
67+ """The state class of sensor"""
68+ return SensorStateClass .TOTAL_INCREASING
69+
70+ @property
71+ def native_unit_of_measurement (self ):
72+ """The unit of measurement of sensor"""
73+ return UnitOfEnergy .KILO_WATT_HOUR
74+
75+ @property
76+ def icon (self ):
77+ """Icon of the sensor."""
78+ return "mdi:lightning-bolt"
79+
80+ @property
81+ def extra_state_attributes (self ):
82+ """Attributes of the sensor."""
83+ return self ._attributes
84+
85+ @property
86+ def native_value (self ):
87+ return self ._state
88+
89+ @callback
90+ def _handle_coordinator_update (self ) -> None :
91+ """Retrieve the current days accumulative export"""
92+ current = now ()
93+ consumption_result : CurrentConsumptionCoordinatorResult = self .coordinator .data if self .coordinator is not None and self .coordinator .data is not None else None
94+ consumption_data = consumption_result .data if consumption_result is not None else None
95+
96+ if (consumption_data is not None and len (consumption_data ) > 0 ):
97+ _LOGGER .debug (f"Calculated total electricity export for '{ self ._mpan } /{ self ._serial_number } '..." )
98+
99+ if consumption_data [- 1 ]["total_export" ] is not None :
100+ self ._state = consumption_data [- 1 ]["total_export" ] if consumption_data [- 1 ]["total_export" ] is not None and consumption_data [- 1 ]["total_export" ] != 0 else None
101+ self ._last_reset = current
102+
103+ self ._attributes = {
104+ "mpan" : self ._mpan ,
105+ "serial_number" : self ._serial_number ,
106+ "is_smart_meter" : self ._is_smart_meter ,
107+ }
108+
109+ self ._attributes = dict_to_typed_dict (self ._attributes )
110+ super ()._handle_coordinator_update ()
111+
112+ async def async_added_to_hass (self ):
113+ """Call when entity about to be added to hass."""
114+ # If not None, we got an initial value.
115+ await super ().async_added_to_hass ()
116+ state = await self .async_get_last_state ()
117+ last_sensor_state = await self .async_get_last_sensor_data ()
118+
119+ if state is not None and last_sensor_state is not None and self ._state is None :
120+ self ._state = None if state .state in (STATE_UNAVAILABLE , STATE_UNKNOWN ) else last_sensor_state .native_value
121+ self ._attributes = dict_to_typed_dict (state .attributes )
122+
123+ _LOGGER .debug (f'Restored state: { self ._state } ' )
0 commit comments