1 """The EnergyFlip integration."""
4 from datetime
import timedelta
8 from energyflip
import EnergyFlip, EnergyFlipException
22 SENSOR_TYPE_THIS_MONTH,
23 SENSOR_TYPE_THIS_WEEK,
24 SENSOR_TYPE_THIS_YEAR,
28 PLATFORMS = [Platform.SENSOR]
30 _LOGGER = logging.getLogger(__name__)
34 """Set up EnergyFlip from a config entry."""
36 energyflip = EnergyFlip(
37 username=entry.data[CONF_USERNAME],
38 password=entry.data[CONF_PASSWORD],
39 source_types=SOURCE_TYPES,
40 request_timeout=FETCH_TIMEOUT,
45 await energyflip.authenticate()
46 except EnergyFlipException
as exception:
47 _LOGGER.error(
"Authentication failed: %s",
str(exception))
50 async
def async_update_data() -> dict[str, dict[str, Any]]:
59 update_method=async_update_data,
60 update_interval=
timedelta(seconds=POLLING_INTERVAL),
63 await coordinator.async_config_entry_first_refresh()
66 hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DATA_COORDINATOR: coordinator}
69 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
75 """Unload a config entry."""
77 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
81 hass.data[DOMAIN].pop(entry.entry_id)
87 """Update the data by performing a request to EnergyFlip."""
91 async
with asyncio.timeout(FETCH_TIMEOUT):
92 if not energyflip.is_authenticated():
93 _LOGGER.warning(
"EnergyFlip is unauthenticated. Reauthenticating")
94 await energyflip.authenticate()
96 current_measurements = await energyflip.current_measurements()
101 current_measurements, source_type
104 current_measurements, source_type, SENSOR_TYPE_THIS_DAY
107 current_measurements, source_type, SENSOR_TYPE_THIS_WEEK
110 current_measurements, source_type, SENSOR_TYPE_THIS_MONTH
113 current_measurements, source_type, SENSOR_TYPE_THIS_YEAR
116 for source_type
in SOURCE_TYPES
118 except EnergyFlipException
as exception:
119 raise UpdateFailed(f
"Error communicating with API: {exception}")
from exception
123 current_measurements: dict,
127 """Get the cumulative energy consumption for a certain period.
129 :param current_measurements: The result from the EnergyFlip client
130 :param source_type: The source of energy (electricity or gas)
131 :param period_type: The period for which cumulative value should be given.
133 if source_type
in current_measurements:
135 period_type
in current_measurements[source_type]
136 and current_measurements[source_type][period_type]
is not None
138 return current_measurements[source_type][period_type][
"value"]
141 "Source type %s not present in %s", source_type, current_measurements
147 if source_type
in current_measurements:
149 "measurement" in current_measurements[source_type]
150 and current_measurements[source_type][
"measurement"]
is not None
152 return current_measurements[source_type][
"measurement"][
"rate"]
155 "Source type %s not present in %s", source_type, current_measurements
def _get_measurement_rate(dict current_measurements, str source_type)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
def _get_cumulative_value(dict current_measurements, str source_type, str period_type)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
dict[str, dict[str, Any]] async_update_energyflip(EnergyFlip energyflip)