Skip to content
Snippets Groups Projects
Commit c67abdc3 authored by jkerdreu's avatar jkerdreu
Browse files

Initial release



git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2700 b32b6428-25c9-4566-ad07-03861ab6144f
parent f551d624
Branches
No related tags found
No related merge requests found
Install
=======
- Download the remote script: https://github.com/thorsten-gehrig/alexa-remote-control
- Edit / run the remote script
- Try the gateway: python -m xaal.alexa
- Edit the config file to add Alexa Echo devices (or Group) (find them w/ alexa-remote-control.sh -a)
You can omit the address.
- re-run the gateway
- done
from setuptools import setup,find_packages
with open('README.rst') as f:
long_description = f.read()
VERSION = "0.1"
setup(
name='xaal.alexa',
version=VERSION,
license='GPL License',
author='Jerome Kerdreux',
author_email='Jerome.Kerdreux@imt-atlantique.fr',
#url='',
description=('xAAL devices for Amazon Alexa' ),
long_description=long_description,
classifiers=[
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
],
keywords=['xaal', 'alexa'],
platforms='any',
packages=find_packages(),
include_package_data=True,
install_requires=[
'xaal.lib',
'xaal.schemas',
]
)
import asyncio
ALEXA_SCRIPT="alexa_remote_control.sh"
async def run(cmd):
cmd = ALEXA_SCRIPT+ " " + cmd
proc = await asyncio.create_subprocess_shell(cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
print(f'[{cmd!r} exited with {proc.returncode}]')
if stdout:
print(f'[stdout]\n{stdout.decode()}')
if stderr:
print(f'[stderr]\n{stderr.decode()}')
async def say(txt):
await run('-e speak:"%s"' % txt)
#asyncio.run(run('alexa_remote_control.sh -e speak:"test"'))
\ No newline at end of file
[config]
addr = b6d85914-872b-4f9e-a620-bf14003aeabf
remote_script = alexa_remote_control.sh
[devices]
[[Dot3-desk]]
base_addr = b6d85914-872b-4f9e-a620-bf14003ae1e3
[[Dot3-kitchen]]
base_addr = b6d859aa-872b-4f9e-a620-bf14003ae1e3
[[Everywhere]]
base_addr = b6d859bb-872b-4f9e-a620-bf14003ae1e3
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
from .gw import setup
\ No newline at end of file
from . import gw
from xaal.lib import helpers
helpers.run_async_package(gw.PACKAGE_NAME,gw.setup)
import logging
from xaal.schemas import devices
from xaal.lib import tools,helpers
import logging
logger = logging.getLogger(__name__)
class Echo(object):
def __init__(self,name,cfg,gw):
self.name = name
self.gw = gw
self.cfg = cfg
self.setup_base_addr()
self.setup_embedded()
def setup_base_addr(self):
b_addr = self.cfg.get('base_addr',None)
if b_addr != None:
self.base_addr = tools.get_uuid(b_addr)
else:
self.base_addr = tools.get_random_base_uuid()
self.cfg['base_addr'] = self.base_addr
def setup_embedded(self):
# TTS
self.setup_tts()
# set some default info on embedded
for dev in self.embedded():
dev.vendor_id = "IHSEV"
dev.version = 0.1
def embedded(self):
return [self.tts,]
def setup_tts(self):
tts = devices.tts(self.base_addr+1)
tts.methods['say'] = self.say
tts.product_id = "TTS for Alexa"
tts.info = f'TTS on Echo:{self.name}'
self.tts = tts
def say(self,_msg,_lang=None,_voice=None):
if _lang or _voice:
logger.warning("Unable to change lang or voice right now")
self.gw.run_script(f'-d {self.name} -e speak:"{_msg}"')
import platform
import logging
from xaal.lib import tools
from xaal.schemas.devices import gateway
import atexit
import asyncio
from .devices import Echo
PACKAGE_NAME = "xaal.alexa"
logger = logging.getLogger(PACKAGE_NAME)
class GW(object):
def __init__(self,engine):
self.devices = []
self.engine = engine
self.config()
atexit.register(self._exit)
self.setup()
self.setup_gw()
def config(self):
self.save_config = False
cfg = tools.load_cfg(PACKAGE_NAME)
if not cfg:
logger.info('Missing config file, building a new one')
cfg = tools.new_cfg(PACKAGE_NAME)
cfg['config']['remote_script'] = 'alexa_remote_control.sh'
cfg['devices'] = {}
cfg.write()
self.cfg = cfg
self.remote_script = cfg['config']['remote_script']
def run_script(self,cmd):
asyncio.ensure_future(self.async_run(cmd))
async def async_run(self,cmd):
cmd = self.remote_script + " " + cmd
proc = await asyncio.create_subprocess_shell(cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
logger.info(f"Run {cmd}: {proc.returncode}")
logger.debug(f"{stdout.decode()}")
def setup(self):
cfg_list = self.cfg.get('devices',[])
for k in cfg_list:
dev = Echo(k,cfg_list[k],self)
self.engine.add_devices(dev.embedded())
def setup_gw(self):
# last step build the GW device
gw = gateway()
gw.address = tools.get_uuid(self.cfg['config']['addr'])
gw.vendor_id = "IHSEV"
gw.product_id = "Alexa gateway"
gw.version = 0.1
gw.url = "http://alexa.amazon.com"
gw.info = "%s@%s" % (PACKAGE_NAME,platform.node())
self.engine.add_device(gw)
self.gw = gw
def _exit(self):
cfg = tools.load_cfg(PACKAGE_NAME)
if cfg != self.cfg:
logger.info('Saving configuration file')
self.cfg.write()
def setup(eng):
gw=GW(eng)
return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment