1 """Support for monitoring a Sense energy sensor."""
3 from dataclasses
import dataclass
4 from functools
import partial
7 from sense_energy
import (
9 SenseAuthenticationException,
10 SenseMFARequiredException,
21 SENSE_CONNECT_EXCEPTIONS,
22 SENSE_TIMEOUT_EXCEPTIONS,
23 SENSE_WEBSOCKET_EXCEPTIONS,
25 from .coordinator
import SenseRealtimeCoordinator, SenseTrendCoordinator
27 _LOGGER = logging.getLogger(__name__)
29 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
30 type SenseConfigEntry = ConfigEntry[SenseData]
33 @dataclass(kw_only=True, slots=True)
35 """Sense data type."""
38 trends: SenseTrendCoordinator
39 rt: SenseRealtimeCoordinator
43 """Set up Sense from a config entry."""
45 entry_data = entry.data
46 timeout = entry_data[CONF_TIMEOUT]
48 access_token = entry_data.get(
"access_token",
"")
49 user_id = entry_data.get(
"user_id",
"")
50 device_id = entry_data.get(
"device_id",
"")
51 refresh_token = entry_data.get(
"refresh_token",
"")
52 monitor_id = entry_data.get(
"monitor_id",
"")
58 gateway = await hass.async_add_executor_job(
63 client_session=client_session,
66 gateway.rate_limit = ACTIVE_UPDATE_RATE
69 gateway.load_auth(access_token, user_id, device_id, refresh_token)
70 gateway.set_monitor_id(monitor_id)
71 await gateway.get_monitor_data()
72 except (SenseAuthenticationException, SenseMFARequiredException)
as err:
73 _LOGGER.warning(
"Sense authentication expired")
75 except SENSE_TIMEOUT_EXCEPTIONS
as err:
77 str(err)
or "Timed out during authentication"
79 except SENSE_CONNECT_EXCEPTIONS
as err:
83 await gateway.fetch_devices()
84 await gateway.update_realtime()
85 except SENSE_TIMEOUT_EXCEPTIONS
as err:
87 str(err)
or "Timed out during realtime update"
89 except SENSE_WEBSOCKET_EXCEPTIONS
as err:
98 entry.async_create_background_task(
100 trends_coordinator.async_request_refresh(),
101 "sense.trends-coordinator-refresh",
103 entry.async_create_background_task(
105 realtime_coordinator.async_request_refresh(),
106 "sense.realtime-coordinator-refresh",
111 trends=trends_coordinator,
112 rt=realtime_coordinator,
115 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
120 """Unload a config entry."""
121 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, SenseConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, SenseConfigEntry entry)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)