Is a really fast script but here is:
#!/usr/bin/env python
import secure_smtpd
import json
import paho.mqtt.client as mqtt
import argparse
import email
from secure_smtpd import SMTPServer, LOG_NAME
parser = argparse.ArgumentParser()
parser.add_argument("-N", "--name", help="doorbell name")
parser.add_argument("-M", "--mqtthost", help="mqtt host")
parser.add_argument("-P", "--mqttport", help="mqtt port", type=int)
parser.add_argument("-U", "--mqttusername", help="mqtt username")
parser.add_argument("-W", "--mqttpassword", help="mqtt password")
args = parser.parse_args()
class SMTPServer(SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, message_data):
client = mqtt.Client(client_id="", clean_session=True, userdata=None,
protocol=mqtt.MQTTv311)
client.username_pw_set(args.mqttusername, args.mqttpassword)
client.connect(args.mqtthost, 1883, 60)
client.loop_start()
data = {}
data['peer'] = peer
data['mailfrom'] = mailfrom
data['rcpttos'] = rcpttos
data['bell'] = 'on'
json_data = json.dumps(data)
print (json_data)
msg = email.message_from_string(message_data)
#print (msg)
attachment = msg.get_payload()[1]
open('/var/www/html/doorbell/doorbell-1.jpg', 'wb').write(attachment.get_payload(decode=True))
attachment = msg.get_payload()[2]
open('/var/www/html/doorbell/doorbell-2.jpg', 'wb').write(attachment.get_payload(decode=True))
attachment = msg.get_payload()[3]
open('/var/www/html/doorbell/doorbell-3.jpg', 'wb').write(attachment.get_payload(decode=True))
client.publish("doorbell/"+args.name, json_data, qos=0)
client.disconnect()
class FakeCredentialValidator(object):
def validate(self, username, password):
return True
server = SMTPServer(
('0.0.0.0', 1125),
None,
require_authentication=True,
ssl=False,
credential_validator=FakeCredentialValidator(),
process_count=1,
)
print('server run')
server.run()
My doorbell send 3 images in the email, so I save it in /var/www/html/doorbell …
How to run the script:
/etc/home-assistance/smtp2mqtt.py -N entrada -M 127.0.0.1 -P 1883 -U <mqttuser> -W <mqttpassword>
The Mqtt sensor:
- platform: mqtt
state_topic: "doorbell/entrada"
name: Doorbell Ring
qos: 0
value_template: '{% if value_json.bell == "on" %} on {% else %} off {% endif %}'
And this automation for set off the doorbell_ring:
alias: 'Enviar Payload a OFF al pulsar VideoPortero'
hide_entity: True
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.doorbell_ring
to: 'on'
action:
- service: mqtt.publish
data:
topic: 'doorbell/entrada'
payload: '{"bell":"off"}'
And one automation example for telegram notification:
alias: 'Notificar Telegram al Pulsar VideoPortero'
hide_entity: True
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.doorbell_ring
to: 'on'
action:
- service: notify.telegram_bot
data:
message: '\U0001F514 Llamada desde el Videoportero'
data:
photo:
- file: /var/www/html/doorbell/doorbell-1.jpg
- file: /var/www/html/doorbell/doorbell-2.jpg
- file: /var/www/html/doorbell/doorbell-3.jpg
inline_keyboard:
- 'Abrir la puerta:/desactivar-alarma'
Other one for HA iOS Notification:
alias: 'Notificar iOS al Pulsar VideoPortero'
hide_entity: True
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.doorbell_ring
to: 'on'
action:
- service: notify.iphone_todos
data:
message: "Llamada Videoportero"
data:
attachment:
content-type: jpeg
push:
category: camera
thread-id: "doorbell-notification-group"
entity_id: camera.calle