async def async_setup(hass, config): """Set up the MyLink platform.""" host = config[DOMAIN][CONF_HOST] port = config[DOMAIN][CONF_PORT] system_id = config[DOMAIN][CONF_SYSTEM_ID] entity_config = config[DOMAIN][CONF_ENTITY_CONFIG] entity_config[CONF_DEFAULT_REVERSE] = config[DOMAIN][CONF_DEFAULT_REVERSE] somfy_mylink = SomfyMyLinkSynergy(system_id, host, port) hass.data[DATA_SOMFY_MYLINK] = somfy_mylink for component in SOMFY_MYLINK_COMPONENTS: hass.async_create_task( async_load_platform(hass, component, DOMAIN, entity_config, config)) return True
async def validate_input(hass: core.HomeAssistant, data): """Validate the user input allows us to connect. Data has the keys from schema with values provided by the user. """ somfy_mylink = SomfyMyLinkSynergy(data[CONF_SYSTEM_ID], data[CONF_HOST], data[CONF_PORT]) try: status_info = await somfy_mylink.status_info() except asyncio.TimeoutError as ex: raise CannotConnect from ex if not status_info or "error" in status_info: _LOGGER.debug("Auth error: %s", status_info) raise InvalidAuth return {"title": f"MyLink {data[CONF_HOST]}"}
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Somfy MyLink from a config entry.""" _async_import_options_from_data_if_missing(hass, entry) config = entry.data somfy_mylink = SomfyMyLinkSynergy(config[CONF_SYSTEM_ID], config[CONF_HOST], config[CONF_PORT]) try: mylink_status = await somfy_mylink.status_info() except asyncio.TimeoutError as ex: raise ConfigEntryNotReady( "Unable to connect to the Somfy MyLink device, please check your settings" ) from ex if not mylink_status or "error" in mylink_status: _LOGGER.error( "Somfy Mylink failed to setup because of an error: %s", mylink_status.get("error", {}).get("message", "Empty response from mylink device"), ) return False if "result" not in mylink_status: raise ConfigEntryNotReady( "The Somfy MyLink device returned an empty result") _async_migrate_entity_config(hass, entry, mylink_status) undo_listener = entry.add_update_listener(_async_update_listener) hass.data[DOMAIN][entry.entry_id] = { DATA_SOMFY_MYLINK: somfy_mylink, MYLINK_STATUS: mylink_status, UNDO_UPDATE_LISTENER: undo_listener, } for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform)) return True
async def async_setup_entry(opp: OpenPeerPower, entry: ConfigEntry): """Set up Somfy MyLink from a config entry.""" opp.data.setdefault(DOMAIN, {}) config = entry.data somfy_mylink = SomfyMyLinkSynergy(config[CONF_SYSTEM_ID], config[CONF_HOST], config[CONF_PORT]) try: mylink_status = await somfy_mylink.status_info() except asyncio.TimeoutError as ex: raise ConfigEntryNotReady( "Unable to connect to the Somfy MyLink device, please check your settings" ) from ex if not mylink_status or "error" in mylink_status: _LOGGER.error( "Somfy Mylink failed to setup because of an error: %s", mylink_status.get("error", {}).get("message", "Empty response from mylink device"), ) return False if "result" not in mylink_status: raise ConfigEntryNotReady( "The Somfy MyLink device returned an empty result") undo_listener = entry.add_update_listener(_async_update_listener) opp.data[DOMAIN][entry.entry_id] = { DATA_SOMFY_MYLINK: somfy_mylink, MYLINK_STATUS: mylink_status, UNDO_UPDATE_LISTENER: undo_listener, } opp.config_entries.async_setup_platforms(entry, PLATFORMS) return True