Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aws component and consolidate aws notify platform #22240

Merged
merged 5 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions homeassistant/components/aws/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""Support for Amazon Web Services (AWS)."""
import asyncio
import logging
from collections import OrderedDict

import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import ATTR_CREDENTIALS, CONF_NAME, CONF_PROFILE_NAME
from homeassistant.helpers import config_validation as cv, discovery

# Loading the config flow file will register the flow
from . import config_flow # noqa
from .const import (
CONF_ACCESS_KEY_ID,
CONF_SECRET_ACCESS_KEY,
DATA_CONFIG,
DATA_HASS_CONFIG,
DATA_SESSIONS,
DOMAIN,
CONF_NOTIFY,
)
from .notify import PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA
awarecan marked this conversation as resolved.
Show resolved Hide resolved

REQUIREMENTS = ["aiobotocore==0.10.2"]

_LOGGER = logging.getLogger(__name__)

AWS_CREDENTIAL_SCHEMA = vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
vol.Inclusive(CONF_ACCESS_KEY_ID, ATTR_CREDENTIALS): cv.string,
vol.Inclusive(CONF_SECRET_ACCESS_KEY, ATTR_CREDENTIALS): cv.string,
vol.Exclusive(CONF_PROFILE_NAME, ATTR_CREDENTIALS): cv.string,
}
)

DEFAULT_CREDENTIAL = [{CONF_NAME: "default", CONF_PROFILE_NAME: "default"}]

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(
ATTR_CREDENTIALS, default=DEFAULT_CREDENTIAL
awarecan marked this conversation as resolved.
Show resolved Hide resolved
): vol.All(cv.ensure_list, [AWS_CREDENTIAL_SCHEMA]),
vol.Optional(CONF_NOTIFY): vol.All(
cv.ensure_list, [NOTIFY_PLATFORM_SCHEMA]
),
}
)
},
extra=vol.ALLOW_EXTRA,
)


async def async_setup(hass, config):
"""Set up AWS component."""
hass.data[DATA_HASS_CONFIG] = config

conf = config.get(DOMAIN)
if conf is None:
# create a default conf using default profile
conf = CONFIG_SCHEMA({ATTR_CREDENTIALS: DEFAULT_CREDENTIAL})

hass.data[DATA_CONFIG] = conf
hass.data[DATA_SESSIONS] = OrderedDict()

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=conf
)
)

return True


async def async_setup_entry(hass, entry):
"""Load a config entry.

Validate and save sessions per aws credential.
"""
config = hass.data.get(DATA_HASS_CONFIG)
conf = hass.data.get(DATA_CONFIG)

if entry.source == config_entries.SOURCE_IMPORT:
if conf is None:
# user removed config from configuration.yaml, abort setup
hass.async_create_task(
hass.config_entries.async_remove(entry.entry_id)
)
return False

if conf != entry.data:
# user changed config from configuration.yaml, use conf to setup
hass.config_entries.async_update_entry(entry, data=conf)

if conf is None:
conf = CONFIG_SCHEMA({DOMAIN: entry.data})[DOMAIN]

validation = True
tasks = []
for cred in conf.get(ATTR_CREDENTIALS):
tasks.append(_validate_aws_credentials(hass, cred))
if tasks:
results = await asyncio.gather(*tasks, return_exceptions=True)
for index, result in enumerate(results):
name = conf[ATTR_CREDENTIALS][index][CONF_NAME]
if isinstance(result, Exception):
_LOGGER.error(
"Validating credential [%s] failed: %s",
name, result, exc_info=result
)
validation = False
else:
hass.data[DATA_SESSIONS][name] = result

# No entry support for notify component yet
for notify_config in conf.get(CONF_NOTIFY, []):
awarecan marked this conversation as resolved.
Show resolved Hide resolved
discovery.load_platform(hass, "notify", DOMAIN, notify_config, config)

return validation


async def _validate_aws_credentials(hass, credential):
"""Validate AWS credential config."""
import aiobotocore

aws_config = credential.copy()
del aws_config[CONF_NAME]

profile = aws_config.get(CONF_PROFILE_NAME)

if profile is not None:
session = aiobotocore.AioSession(profile=profile, loop=hass.loop)
del aws_config[CONF_PROFILE_NAME]
if CONF_ACCESS_KEY_ID in aws_config:
del aws_config[CONF_ACCESS_KEY_ID]
if CONF_SECRET_ACCESS_KEY in aws_config:
del aws_config[CONF_SECRET_ACCESS_KEY]
else:
session = aiobotocore.AioSession(loop=hass.loop)

async with session.create_client("iam", **aws_config) as client:
await client.get_user()

return session
22 changes: 22 additions & 0 deletions homeassistant/components/aws/config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Config flow for AWS component."""

from homeassistant import config_entries

from .const import DOMAIN


@config_entries.HANDLERS.register(DOMAIN)
class AWSFlowHandler(config_entries.ConfigFlow):
"""Handle a config flow."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH

async def async_step_import(self, user_input):
"""Import a config entry."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

return self.async_create_entry(
title="configuration.yaml", data=user_input
)
13 changes: 13 additions & 0 deletions homeassistant/components/aws/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Constant for AWS component."""
DOMAIN = "aws"
DATA_KEY = DOMAIN
DATA_CONFIG = "aws_config"
DATA_HASS_CONFIG = "aws_hass_config"
DATA_SESSIONS = "aws_sessions"

CONF_REGION = "region_name"
CONF_ACCESS_KEY_ID = "aws_access_key_id"
CONF_SECRET_ACCESS_KEY = "aws_secret_access_key"
CONF_PROFILE_NAME = "profile_name"
CONF_CREDENTIAL_NAME = "credential_name"
CONF_NOTIFY = "notify"
Loading