Skip to content

Commit eeba549

Browse files
Check for config entities (#1841)
1 parent 9220b3d commit eeba549

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

custom_components/battery_notes/config_flow.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async def async_step_device(
150150
user_input: dict | None = None,
151151
) -> config_entries.FlowResult:
152152
"""Handle a flow for a device or discovery."""
153-
_errors = {}
153+
errors: dict[str, str] = {}
154154
if user_input is not None:
155155
self.data = user_input
156156

@@ -205,7 +205,7 @@ async def async_step_device(
205205
return self.async_show_form(
206206
step_id="device",
207207
data_schema=schema,
208-
errors=_errors,
208+
errors=errors,
209209
last_step=False,
210210
)
211211

@@ -214,7 +214,7 @@ async def async_step_entity(
214214
user_input: dict | None = None,
215215
) -> config_entries.FlowResult:
216216
"""Handle a flow for a device or discovery."""
217-
_errors = {}
217+
errors: dict[str, str] = {}
218218
if user_input is not None:
219219
self.data = user_input
220220

@@ -226,52 +226,57 @@ async def async_step_entity(
226226
# Default battery quantity if not found in library lookup
227227
self.data[CONF_BATTERY_QUANTITY] = 1
228228

229-
if entity_entry.device_id:
229+
if entity_entry:
230230

231-
self.data[CONF_DEVICE_ID] = entity_entry.device_id
231+
if entity_entry.device_id:
232232

233-
if (
234-
DOMAIN in self.hass.data
235-
and DATA_LIBRARY_UPDATER in self.hass.data[DOMAIN]
236-
):
237-
library_updater: LibraryUpdater = self.hass.data[DOMAIN][
238-
DATA_LIBRARY_UPDATER
239-
]
240-
await library_updater.get_library_updates(dt_util.utcnow())
233+
self.data[CONF_DEVICE_ID] = entity_entry.device_id
241234

242-
device_registry = dr.async_get(self.hass)
243-
device_entry = device_registry.async_get(entity_entry.device_id)
235+
if (
236+
DOMAIN in self.hass.data
237+
and DATA_LIBRARY_UPDATER in self.hass.data[DOMAIN]
238+
):
239+
library_updater: LibraryUpdater = self.hass.data[DOMAIN][
240+
DATA_LIBRARY_UPDATER
241+
]
242+
await library_updater.get_library_updates(dt_util.utcnow())
244243

245-
_LOGGER.debug(
246-
"Looking up device %s %s %s", device_entry.manufacturer, device_entry.model, device_entry.hw_version
247-
)
244+
device_registry = dr.async_get(self.hass)
245+
device_entry = device_registry.async_get(entity_entry.device_id)
248246

249-
model_info = ModelInfo(device_entry.manufacturer, device_entry.model, device_entry.hw_version)
247+
_LOGGER.debug(
248+
"Looking up device %s %s %s", device_entry.manufacturer, device_entry.model, device_entry.hw_version
249+
)
250250

251-
library = await Library.factory(self.hass)
251+
model_info = ModelInfo(device_entry.manufacturer, device_entry.model, device_entry.hw_version)
252252

253-
device_battery_details = await library.get_device_battery_details(
254-
model_info
255-
)
253+
library = await Library.factory(self.hass)
256254

257-
if device_battery_details and not device_battery_details.is_manual:
258-
_LOGGER.debug(
259-
"Found device %s %s %s", device_entry.manufacturer, device_entry.model, device_entry.hw_version
255+
device_battery_details = await library.get_device_battery_details(
256+
model_info
260257
)
261-
self.data[CONF_BATTERY_TYPE] = device_battery_details.battery_type
262258

263-
self.data[
264-
CONF_BATTERY_QUANTITY
265-
] = device_battery_details.battery_quantity
259+
if device_battery_details and not device_battery_details.is_manual:
260+
_LOGGER.debug(
261+
"Found device %s %s %s", device_entry.manufacturer, device_entry.model, device_entry.hw_version
262+
)
263+
self.data[CONF_BATTERY_TYPE] = device_battery_details.battery_type
266264

267-
return await self.async_step_battery()
265+
self.data[
266+
CONF_BATTERY_QUANTITY
267+
] = device_battery_details.battery_quantity
268+
269+
return await self.async_step_battery()
270+
else:
271+
# No entity_registry entry, must be a config.yaml entity which we can't support
272+
errors["base"] = "unconfigurable_entity"
268273

269274
schema = ENTITY_SCHEMA_ALL
270275

271276
return self.async_show_form(
272277
step_id="entity",
273278
data_schema=schema,
274-
errors=_errors,
279+
errors=errors,
275280
last_step=False,
276281
)
277282

custom_components/battery_notes/translations/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"already_configured": "Device is already configured"
4848
},
4949
"error": {
50-
"unknown": "Unknown error occurred."
50+
"unknown": "Unknown error occurred.",
51+
"unconfigurable_entity": "It is not possible to add this entity to Battery Notes."
5152
}
5253
},
5354
"options": {

0 commit comments

Comments
 (0)