|
| 1 | +"""Config flow for the Tween Light Infrared integration.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import voluptuous as vol |
| 6 | + |
| 7 | +from homeassistant.components.infrared import ( |
| 8 | + DOMAIN as INFRARED_DOMAIN, |
| 9 | + async_get_emitters, |
| 10 | + async_get_receivers, |
| 11 | +) |
| 12 | +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult |
| 13 | +from homeassistant.helpers.selector import ( |
| 14 | + EntitySelector, |
| 15 | + EntitySelectorConfig, |
| 16 | + SelectSelector, |
| 17 | + SelectSelectorConfig, |
| 18 | + SelectSelectorMode, |
| 19 | +) |
| 20 | + |
| 21 | +from .const import ( |
| 22 | + CONF_DEVICE_TYPE, |
| 23 | + CONF_INFRARED_ENTITY_ID, |
| 24 | + CONF_INFRARED_RECEIVER_ENTITY_ID, |
| 25 | + DEVICE_TYPE_NAMES, |
| 26 | + DOMAIN, |
| 27 | + TweenLightIrDeviceType, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class TweenLightIrConfigFlow(ConfigFlow, domain=DOMAIN): |
| 32 | + """Handle a config flow for Tween Light Infrared.""" |
| 33 | + |
| 34 | + async def async_step_user( |
| 35 | + self, user_input: dict[str, Any] | None = None |
| 36 | + ) -> ConfigFlowResult: |
| 37 | + """Handle the initial step.""" |
| 38 | + errors: dict[str, str] = {} |
| 39 | + emitter_entity_ids = async_get_emitters(self.hass) |
| 40 | + receiver_entity_ids = async_get_receivers(self.hass) |
| 41 | + if not emitter_entity_ids and not receiver_entity_ids: |
| 42 | + return self.async_abort(reason="no_infrared_entities") |
| 43 | + |
| 44 | + if user_input is not None: |
| 45 | + if user_input.get(CONF_INFRARED_ENTITY_ID) or user_input.get( |
| 46 | + CONF_INFRARED_RECEIVER_ENTITY_ID |
| 47 | + ): |
| 48 | + self._async_abort_entries_match( |
| 49 | + { |
| 50 | + CONF_DEVICE_TYPE: user_input[CONF_DEVICE_TYPE], |
| 51 | + CONF_INFRARED_ENTITY_ID: user_input.get( |
| 52 | + CONF_INFRARED_ENTITY_ID |
| 53 | + ), |
| 54 | + } |
| 55 | + ) |
| 56 | + return self.async_create_entry( |
| 57 | + title=DEVICE_TYPE_NAMES[user_input[CONF_DEVICE_TYPE]], |
| 58 | + data=user_input, |
| 59 | + ) |
| 60 | + |
| 61 | + errors["base"] = "missing_infrared_entity" |
| 62 | + |
| 63 | + return self.async_show_form( |
| 64 | + step_id="user", |
| 65 | + data_schema=vol.Schema( |
| 66 | + { |
| 67 | + vol.Required(CONF_DEVICE_TYPE): SelectSelector( |
| 68 | + SelectSelectorConfig( |
| 69 | + options=[ |
| 70 | + device_type.value |
| 71 | + for device_type in TweenLightIrDeviceType |
| 72 | + ], |
| 73 | + translation_key=CONF_DEVICE_TYPE, |
| 74 | + mode=SelectSelectorMode.DROPDOWN, |
| 75 | + ) |
| 76 | + ), |
| 77 | + vol.Optional(CONF_INFRARED_ENTITY_ID): EntitySelector( |
| 78 | + EntitySelectorConfig( |
| 79 | + domain=INFRARED_DOMAIN, |
| 80 | + include_entities=emitter_entity_ids, |
| 81 | + ) |
| 82 | + ), |
| 83 | + vol.Optional(CONF_INFRARED_RECEIVER_ENTITY_ID): EntitySelector( |
| 84 | + EntitySelectorConfig( |
| 85 | + domain=INFRARED_DOMAIN, |
| 86 | + include_entities=receiver_entity_ids, |
| 87 | + ) |
| 88 | + ), |
| 89 | + } |
| 90 | + ), |
| 91 | + errors=errors, |
| 92 | + ) |
0 commit comments