11"""Platform for sensor integration."""
22from typing import Any , Callable , Dict , Optional
33
4+ from homeassistant .config_entries import ConfigEntry
45from homeassistant .const import DEVICE_CLASS_POWER , POWER_WATT
56from homeassistant .core import HomeAssistant
67from homeassistant .helpers .entity import Entity
78
9+ from .config_flow import config_object
810from .const import DOMAIN
9- from .glow import Glow
11+ from .glow import Glow , InvalidAuth
1012
1113
1214async def async_setup_entry (
13- hass : HomeAssistant , config : Dict [ str , Any ] , async_add_entities : Callable
15+ hass : HomeAssistant , config : ConfigEntry , async_add_entities : Callable
1416) -> bool :
1517 """Set up the sensor platform."""
1618 new_entities = []
1719
20+ async def handle_failed_auth (config : ConfigEntry , hass : HomeAssistant ) -> None :
21+ glow_auth = await hass .async_add_executor_job (
22+ Glow .authenticate ,
23+ config .data ["app_id" ],
24+ config .data ["username" ],
25+ config .data ["password" ],
26+ )
27+
28+ current_config = dict (config .data .copy ())
29+ new_config = config_object (current_config , glow_auth )
30+ hass .config_entries .async_update_entry (entry = config , data = new_config )
31+
32+ glow = Glow (config .data ["app_id" ], glow_auth ["token" ])
33+ hass .data [DOMAIN ][config .entry_id ] = glow
34+
1835 for entry in hass .data [DOMAIN ]:
1936 glow = hass .data [DOMAIN ][entry ]
2037
21- resources = await glow . retrieve_resources ()
38+ resources : dict = dict ()
2239
40+ try :
41+ resources = await hass .async_add_executor_job (glow .retrieve_resources )
42+ except InvalidAuth :
43+ try :
44+ await handle_failed_auth (config , hass )
45+ except InvalidAuth :
46+ return False
47+
48+ glow = hass .data [DOMAIN ][entry ]
49+ resources = await hass .async_add_executor_job (glow .retrieve_resources )
2350 for resource in resources :
2451 if resource ["resourceTypeId" ] in GlowConsumptionCurrent .resourceTypeId :
2552 sensor = GlowConsumptionCurrent (glow , resource )
@@ -33,11 +60,15 @@ async def async_setup_entry(
3360class GlowConsumptionCurrent (Entity ):
3461 """Sensor object for the Glowmarkt resource's current consumption."""
3562
63+ hass : HomeAssistant
64+
3665 resourceTypeId = [
3766 "ea02304a-2820-4ea0-8399-f1d1b430c3a0" , # Smart Meter, electricity consumption
3867 "672b8071-44ff-4f23-bca2-f50c6a3ddd02" , # Smart Meter, gas consumption
3968 ]
4069
70+ available = True
71+
4172 def __init__ (self , glow : Glow , resource : Dict [str , Any ]):
4273 """Initialize the sensor."""
4374 self ._state : Optional [Dict [str , Any ]] = None
@@ -103,4 +134,11 @@ async def async_update(self) -> None:
103134
104135 This is the only method that should fetch new data for Home Assistant.
105136 """
106- self ._state = await self .glow .current_usage (self .resource ["resourceId" ])
137+ try :
138+ self ._state = await self .hass .async_add_executor_job (
139+ self .glow .current_usage , self .resource ["resourceId" ]
140+ )
141+ except InvalidAuth :
142+ # TODO: Trip the failed auth logic above somehow
143+ self .available = False
144+ pass
0 commit comments