1 """The Smart Meter Texas integration."""
6 from smart_meter_texas
import Account, Client, ClientSSLContext
7 from smart_meter_texas.exceptions
import (
8 SmartMeterTexasAPIError,
9 SmartMeterTexasAuthError,
28 _LOGGER = logging.getLogger(__name__)
30 PLATFORMS = [Platform.SENSOR]
34 """Set up Smart Meter Texas from a config entry."""
36 username = entry.data[CONF_USERNAME]
37 password = entry.data[CONF_PASSWORD]
39 account = Account(username, password)
41 client_ssl_context = ClientSSLContext()
42 ssl_context = await client_ssl_context.get_ssl_context()
46 await smart_meter_texas_data.client.authenticate()
47 except SmartMeterTexasAuthError:
48 _LOGGER.error(
"Username or password was not accepted")
50 except TimeoutError
as error:
51 raise ConfigEntryNotReady
from error
53 await smart_meter_texas_data.setup()
55 async
def async_update_data():
56 _LOGGER.debug(
"Fetching latest data")
57 await smart_meter_texas_data.read_meters()
58 return smart_meter_texas_data
68 name=
"Smart Meter Texas",
69 update_method=async_update_data,
70 update_interval=SCAN_INTERVAL,
72 hass, _LOGGER, cooldown=DEBOUNCE_COOLDOWN, immediate=
True
76 hass.data.setdefault(DOMAIN, {})
77 hass.data[DOMAIN][entry.entry_id] = {
78 DATA_COORDINATOR: coordinator,
79 DATA_SMART_METER: smart_meter_texas_data,
82 entry.async_create_background_task(
83 hass, coordinator.async_refresh(),
"smart_meter_texas-coordinator-refresh"
86 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
92 """Manages coordinatation of API data updates."""
99 ssl_context: ssl.SSLContext,
101 """Initialize the data coordintator."""
104 websession = aiohttp_client.async_get_clientsession(hass)
105 self.
clientclient = Client(websession, account, ssl_context=ssl_context)
106 self.
metersmeters: list = []
109 """Fetch all of the user's meters."""
111 _LOGGER.debug(
"Discovered %s meter(s)", len(self.
metersmeters))
114 """Read each meter."""
115 for meter
in self.
metersmeters:
117 await meter.read_meter(self.
clientclient)
118 except (SmartMeterTexasAPIError, SmartMeterTexasAuthError)
as error:
124 """Unload a config entry."""
125 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
127 hass.data[DOMAIN].pop(entry.entry_id)
None __init__(self, HomeAssistant hass, ConfigEntry entry, Account account, ssl.SSLContext ssl_context)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)