1010import voluptuous as vol
1111
1212from homeassistant .config_entries import ConfigEntry , ConfigFlow , ConfigFlowResult , OptionsFlow
13- from homeassistant .const import CONF_API_TOKEN , CONF_EMAIL , CONF_PASSWORD , CONF_REGION , Platform
13+ from homeassistant .const import CONF_API_TOKEN , CONF_EMAIL , CONF_PASSWORD , CONF_REGION
1414from homeassistant .core import callback
1515from homeassistant .data_entry_flow import section
1616from homeassistant .helpers .aiohttp_client import async_get_clientsession
1717from homeassistant .helpers .selector import selector
18- from homeassistant .helpers .entity_registry import async_get as get_entity_registry
1918
2019from .api import PetLibroAPI
2120from .const import (
2221 DEFAULT_FEED ,
2322 DEFAULT_WATER ,
2423 DEFAULT_WEIGHT ,
25- ROUNDING_RULES ,
2624 DOMAIN ,
27- APIKey as API ,
25+ CommonAPIKeys as API ,
2826 Gender ,
29- Unit ,
27+ UnitTypes ,
3028)
3129from .exceptions import PetLibroCannotConnect , PetLibroInvalidAuth
3230from .hub import PetLibroHub
@@ -203,54 +201,25 @@ async def async_step_account_settings(
203201 return self .async_abort (reason = "account_update_nomember" )
204202
205203 user_input = user_input or {}
206- if user_input :
207- update_setting_temp = user_input .pop ("measurement_unit" , {})
208- update_info_temp = user_input .copy ()
209- user_input .update (** update_setting_temp )
210- update_all_units = update_setting_temp .pop ("update_all_units" , False )
211-
204+ if user_input :
212205 update_setting = self .collect_updates (
213206 fields = (API .FEED_UNIT , API .WATER_UNIT , API .WEIGHT_UNIT ),
214- user_input = update_setting_temp ,
215- enum_cls = Unit ,
207+ user_input = user_input . pop ( "measurement_unit" , {}) ,
208+ enum_cls = UnitTypes ,
216209 )
217210
218211 update_info = self .collect_updates (
219212 fields = (API .NICKNAME , API .GENDER ),
220- user_input = update_info_temp ,
213+ user_input = user_input ,
221214 special = {
222215 API .NICKNAME : lambda v : v or "" ,
223216 API .GENDER : lambda v : self .validate_enum (API .GENDER , v , Gender ),
224217 },
225218 )
226-
227- if update_setting or update_all_units :
228- registry = get_entity_registry (self .hass )
229- for unit_type in self .hub .unit_sensor_unique_ids :
230- unit = (input if isinstance (input := update_setting .get (unit_type ), Unit )
231- else Unit (input ) if input else getattr (self .member , unit_type , None ))
232- if (unit_type not in update_setting or not unit or not unit .device_class ) and not update_all_units :
233- continue
234- _LOGGER .debug ("Updating %s sensor entities" , unit_type )
235- if update_all_units and unit_type == API .FEED_UNIT :
236- target_units = {"weight" : unit if unit .device_class == "weight" else Unit .GRAMS ,
237- "volume" : unit if unit .device_class == "volume" else Unit .MILLILITERS }
238- else :
239- target_units = {unit .device_class : unit }
240- for device_class , target_unit in target_units .items ():
241- display_precision = ROUNDING_RULES .get (target_unit , 0 )
242- options = { "unit_of_measurement" : target_unit .symbol ,
243- "display_precision" : display_precision ,
244- "suggested_display_precision" : display_precision }
245- for unique_id in self .hub .unit_sensor_unique_ids .get (unit_type , {}).get (device_class , []):
246- entity_id = registry .async_get_entity_id (Platform .SENSOR , DOMAIN , unique_id )
247- _LOGGER .debug ("Setting %s to %s with display precision %s" , entity_id , unit .symbol , display_precision )
248- registry .async_update_entity_options (entity_id , Platform .SENSOR , options )
249219
250220 if not (update_info or update_setting ):
251221 _LOGGER .debug ("No account settings were changed." )
252- reason = "account_update_nochanges" + ("_update_sensors" if update_all_units else "" )
253- return self .async_abort (reason = reason )
222+ return self .async_abort (reason = "account_update_nochanges" )
254223
255224 no_error = await self .api .member_update_info (update_info , update_setting )
256225 await self .hub .async_refresh (force_member = True )
@@ -283,12 +252,12 @@ def _show_account_settings_form(self, user_input: dict[str, Any]) -> ConfigFlowR
283252 vol .Required (
284253 str (API .GENDER ),
285254 default = user_input .get (
286- API .GENDER , getattr (self .member , API .GENDER , Gender .NONE ). lower
255+ API .GENDER , getattr (self .member , API .GENDER , str ( Gender .NONE ))
287256 ),
288257 ): selector (
289258 {
290259 "select" : {
291- "options" : [g .lower for g in Gender ],
260+ "options" : [g .name . lower () for g in Gender ],
292261 "mode" : "dropdown" ,
293262 "translation_key" : "member_gender" ,
294263 }
@@ -311,32 +280,33 @@ def _get_measurement_schema(self, user_input: dict[str, Any]) -> vol.Schema:
311280 vol .Required (
312281 str (API .FEED_UNIT ),
313282 default = user_input .get (
314- API .FEED_UNIT , getattr (self .member , API .FEED_UNIT , DEFAULT_FEED ). lower
283+ API .FEED_UNIT , getattr (self .member , API .FEED_UNIT , DEFAULT_FEED . name )
315284 ),
316- ): self ._unit_selector ((Unit .CUPS , Unit .OUNCES , Unit .GRAMS , Unit .MILLILITERS )),
285+ ): self ._unit_selector (
286+ (UnitTypes .CUPS , UnitTypes .OUNCES , UnitTypes .GRAMS , UnitTypes .MILLILITERS )
287+ ),
317288 vol .Required (
318289 str (API .WATER_UNIT ),
319290 default = user_input .get (
320- API .WATER_UNIT , getattr (self .member , API .WATER_UNIT , DEFAULT_WATER ). lower
291+ API .WATER_UNIT , getattr (self .member , API .WATER_UNIT , DEFAULT_WATER . name )
321292 ),
322- ): self ._unit_selector ((Unit . WATER_OUNCES , Unit . WATER_MILLILITERS )),
293+ ): self ._unit_selector ((UnitTypes . OUNCES , UnitTypes . MILLILITERS )),
323294 vol .Required (
324295 str (API .WEIGHT_UNIT ),
325296 default = user_input .get (
326297 API .WEIGHT_UNIT ,
327- getattr (self .member , API .WEIGHT_UNIT , DEFAULT_WEIGHT ). lower ,
298+ getattr (self .member , API .WEIGHT_UNIT , DEFAULT_WEIGHT . name ) ,
328299 ),
329- ): self ._unit_selector ((Unit .POUNDS , Unit .KILOGRAMS )),
330- vol .Optional ("update_all_units" , default = user_input .get ("update_all_units" , False )): bool ,
300+ ): self ._unit_selector ((UnitTypes .POUNDS , UnitTypes .KILOGRAMS )),
331301 }
332302 )
333303
334- def _unit_selector (self , options : tuple [Unit , ...]) -> Any :
304+ def _unit_selector (self , options : tuple [Enum , ...]) -> Any :
335305 """Return a dropdown selector for measurement unit options."""
336306 return selector (
337307 {
338308 "select" : {
339- "options" : [o .lower for o in options ],
309+ "options" : [o .name . lower () for o in options ],
340310 "mode" : "dropdown" ,
341311 "translation_key" : "unit_type" ,
342312 }
@@ -354,7 +324,7 @@ def validate_enum(self, api_key: str, form_value: Any, enum_cls: type[Enum]) ->
354324
355325 form_value_str = str (form_value ).upper ()
356326 if form_value_str in enum_cls .__members__ :
357- return enum_cls [form_value_str ]
327+ return enum_cls [form_value_str ]. value
358328
359329 _LOGGER .error ("Invalid value: %s for API key: %s" , form_value , api_key )
360330 return None
@@ -388,7 +358,6 @@ def collect_updates(
388358 else :
389359 api_value = form_value
390360
391- if api_value != current_value :
392- updates [api_key ] = api_value
393-
361+ updates [api_key ] = api_value
362+
394363 return updates
0 commit comments