text: #!/usr/bin/env python3.6
  # coding: utf-8
  
  # Copyright (c) 2015 Michael Auchter <a@phire.org>
  #
  # Permission is hereby granted, free of charge, to any person obtaining a copy
  # of this software and associated documentation files (the "Software"), to deal
  # in the Software without restriction, including without limitation the rights
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  # copies of the Software, and to permit persons to whom the Software is
  # furnished to do so, subject to the following conditions:
  #
  # The above copyright notice and this permission notice shall be included in
  # all copies or substantial portions of the Software.
  #
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  # SOFTWARE.
  
  import os
  import json
  import logging
  import requests
  
  logger = logging.getLogger()
  
  
  class HomeAssistant(object):
      def __init__(self, config):
          self.config = config
          self.url = config.url.rstrip('/')
          agent_str = 'Home Assistant Alexa Smart Home Skill - %s - %s'
          agent_fmt = agent_str % (os.environ['AWS_DEFAULT_REGION'],
                                   requests.utils.default_user_agent())
          self.session = requests.Session()
          self.session.headers = {'content-type': 'application/json',
                                  'User-Agent': agent_fmt}
          
          if self.config.token_auth:
              self.session.headers['Authorization'] = 'Bearer ' + self.config.token
          else:
              self.session.headers['x-ha-access'] = self.config.password
          
          self.session.verify = config.ssl_verify
          self.session.cert = config.ssl_client
  
      def build_url(self, relurl):
          return '%s/%s' % (self.config.url, relurl)
  
      def get(self, relurl):
          r = self.session.get(self.build_url(relurl))
          r.raise_for_status()
          return r.json()
  
      def post(self, relurl, d, wait=False):
          read_timeout = None if wait else 0.01
          r = None
          try:
              logger.debug('calling %s with %s', relurl, str(d))
              r = self.session.post(self.build_url(relurl),
                                    data=json.dumps(d),
                                    timeout=(None, read_timeout))
              r.raise_for_status()
          except requests.exceptions.ReadTimeout:
              # Allow response timeouts after request was sent
              logger.debug('request for %s sent without waiting for response',
                           relurl)
          return r
  
  
  class Configuration(object):
      def __init__(self, filename=None, optsDict=None):
          self._json = {}
          if filename is not None:
              with open(filename) as f:
                  self._json = json.load(f)
  
          if optsDict is not None:
              self._json = optsDict
  
          opts = {}
          opts['url'] = self.get(['url', 'ha_url'],
                                 default='http://localhost:8123/api')
          opts['ssl_verify'] = self.get(['ssl_verify', 'ha_cert'], default=True)
          opts['token_auth'] = self.get(['token_auth'], default=False)
          opts['password'] = self.get(['password', 'ha_passwd'], default='')
          opts['token'] = self.get(['token'], default='')
          opts['ssl_client'] = self.get(['ssl_client'], default='')
          opts['debug'] = self.get(['debug'], default=False)
          self.opts = opts
  
      def __getattr__(self, name):
          return self.opts[name]
  
      def get(self, keys, default):
          for key in keys:
              if key in self._json:
                  return self._json[key]
          return default
  
      def dump(self):
          return json.dumps(self.opts, indent=2, separators=(',', ': '))
  
  
  def event_handler(event, context):
      config = Configuration('config.json')
      if config.debug:
          logger.setLevel(logging.DEBUG)
      ha = HomeAssistant(config)
  
      return ha.post('alexa/smart_home', event, wait=True).json()
  