1 """The WattTime integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
7 from aiowatttime
import Client
8 from aiowatttime.emissions
import RealTimeEmissionsResponseType
9 from aiowatttime.errors
import InvalidCredentialsError, WattTimeError
24 from .const
import DOMAIN, LOGGER
28 PLATFORMS: list[Platform] = [Platform.SENSOR]
32 """Set up WattTime from a config entry."""
33 session = aiohttp_client.async_get_clientsession(hass)
36 client = await Client.async_login(
37 entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], session=session
39 except InvalidCredentialsError
as err:
41 except WattTimeError
as err:
42 LOGGER.error(
"Error while authenticating with WattTime: %s", err)
45 async
def async_update_data() -> RealTimeEmissionsResponseType:
46 """Get the latest realtime emissions data."""
48 return await client.emissions.async_get_realtime_emissions(
49 entry.data[CONF_LATITUDE], entry.data[CONF_LONGITUDE]
51 except InvalidCredentialsError
as err:
53 except WattTimeError
as err:
55 f
"Error while requesting data from WattTime: {err}"
63 update_interval=DEFAULT_UPDATE_INTERVAL,
64 update_method=async_update_data,
67 await coordinator.async_config_entry_first_refresh()
68 hass.data.setdefault(DOMAIN, {})
69 hass.data[DOMAIN][entry.entry_id] = coordinator
71 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
73 entry.async_on_unload(entry.add_update_listener(async_reload_entry))
79 """Unload a config entry."""
80 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
82 hass.data[DOMAIN].pop(entry.entry_id)
88 """Handle an options update."""
89 await hass.config_entries.async_reload(config_entry.entry_id)
None async_reload_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)