Allows interaction between Alexa and your Raspberry Pi
Project description
Chattyraspi is a support library to connect your Raspberry Pi to Amazon Alexa.
Quick start
Connect to the configuration console
Logon using your Amazon credentials
Add your device(s)
Download your configuration file
Start the the example script: test_chattyraspi --config devices_configuration.yaml
Install Chattyraspy skill on your Alexa
Detect your new device(s) on Alexa
Turn on/off your device(s) using Alexa
Custom script
Chattyraspi allows you to intercept the following Alexa interface commands:
All the turn on/off requests
Temperature sensor commands
Thermostat control commands
upon which you can freely react as you wish.
For instance, you might decide to:
Power control devices attached on your Raspi using Alexa interface
Receive on Alexa temperature info from a sensor connected to your Raspi
Hack the Alexa thermostat interface to control step motors rotation
Hack the Alexa thermostat interface to control brushless motors speed
Associate the Tun On/Off command to sysadmin tasks, such as starting networking services
This snippet of code comes from the test_chattyraspi script, as an example of how to implement custom logics when receiving Alexa commands:
#!/usr/bin/env python
from logging.config import fileConfig
import click
import typing
from chattyraspi.client import Client, ThermostatMode
from chattyraspi.device import DevicesConfiguration
def test_devices():
# Optionally configure you logging system
logging_conf_file = '<youg logging configuration>'
fileConfig(logging_conf_file, disable_existing_loggers=False)
config = DevicesConfiguration('devices_configuration.yaml')
client = Client(config)
statuses = dict()
temperatures = dict()
thermostat_modes = dict()
def _turn_on(device_id: str):
print('Device {} turned ON'.format(device_id))
# Here you have received a turn on request.
# Feel free to do whatever you want, but please remember
# to mark your device as ON somehow
statuses[device_id] = True
def _turn_off(device_id: str):
print('Device {} turned OFF'.format(device_id))
# Here you have received a turn off request.
# Here you are free to do whatever you want, but reasonably
# you would do the opposite as turn on callback.
# Finally, please remember that your device is OFF.
statuses[device_id] = False
def _fetch_is_power_on(device_id: str) -> bool:
print('Device {} requested power status'.format(device_id))
# Here Alexa is asking for the power status of your device.
# Please be consistent with what you have done before
status = statuses[device_id]
print('Returning', status)
return status
def _fetch_temperature(device_id: str) -> float:
# Here Alexa is asking for the current temperature.
# This may happen both on status report or explicit temperature request.
print('Device {} requested temperature'.format(device_id))
temperature = temperatures[device_id]
# Simulate a temperature that has not been reached yet
if temperature is not None:
temperature -= 10
print('Returning', temperature)
return temperature
def _fetch_thermostat_mode(device_id: str) -> ThermostatMode:
# Here Alexa is asking for the thermostat mode.
# This may happen both on status report or explicit thermostat mode request.
print('Device {} requested thermostat mode'.format(device_id))
thermostat_mode = thermostat_modes[device_id]
print('Returning', thermostat_mode)
return thermostat_mode
def _fetch_thermostat_target_setpoing(device_id: str) -> float:
# Here Alexa is asking for the current temperature.
# This may happen both on status report or explicit temperature request.
print('Device {} requested target setpoint'.format(device_id))
temperature = temperatures[device_id]
print('Returning', temperature)
return temperature
def _on_set_temperature(device_id: str, temperature: float):
# Here Alexa is asking to set the target temperature.
# Please be consistent with what you have done before
print('Device {} set temperature at {}'.format(device_id, temperature))
temperatures[device_id] = temperature
def _on_adjust_temperature(device_id: str, temperature: float):
# Here Alexa is asking to adjust the target temperature by a delta..
# Please be consistent with what you have done before
print('Device {} adjust temperature by {}'.format(device_id, temperature))
temperatures[device_id] += temperature
def _on_set_thermostat_mode(device_id: str, thermostat_mode: ThermostatMode):
# Here Alexa is asking to set the thermostat mode.
# Please be consistent with what you have done before
print('Device {} set thermostat_mode {}'.format(device_id, thermostat_mode))
thermostat_modes[device_id] = thermostat_mode
# Some boilerplate code: here we add the same callbacks for each configured
# device.
for device_id in map(lambda d: d['device_id'], config.get_configuration()['Devices']):
statuses[device_id] = False
client.set_on_turn_on(device_id, _turn_on)
client.set_on_turn_off(device_id, _turn_off)
client.set_fetch_is_power_on(device_id, _fetch_is_power_on)
client.set_fetch_temperature(device_id, _fetch_temperature)
client.set_fetch_thermostat_mode(device_id, _fetch_thermostat_mode)
client.set_fetch_thermostat_target_setpoint(device_id, _fetch_thermostat_target_setpoing)
client.set_on_set_temperature(device_id, _on_set_temperature)
client.set_on_adjust_temperature(device_id, _on_adjust_temperature)
client.set_on_set_thermostat_mode(device_id, _on_set_thermostat_mode)
client.listen()
if __name__ == '__main__':
test_devices()
Disclaimer
This software is provided “as is” and “with all faults.” I make no representations or warranties of any kind concerning the safety, suitability, lack of viruses, inaccuracies, typographical errors, or other harmful components of this software. There are inherent dangers in the use of any software, and you are solely responsible for determining whether this software is compatible with your equipment and other software installed on your equipment. You are also solely responsible for the protection of your equipment and backup of your data, and I will not be liable for any damages you may suffer in connection with using, modifying, or distributing this software.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file chattyraspi-0.1.1.tar.gz
.
File metadata
- Download URL: chattyraspi-0.1.1.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ad0a3975519cd0a06ae24e8cdfa570a42ab80ed196b515c00e786c481b901f1 |
|
MD5 | 88cb5d57c855ca9f2202e3a2dbeede66 |
|
BLAKE2b-256 | de469a146d84e6d8ed043f4cb13dd5c34b42090b0c3a68fe6e68e068e95ca71b |