async def test_multi_part_only_html(opp): """Test multi part emails with only HTML.""" msg = MIMEMultipart("alternative") msg["Subject"] = "Link" msg["From"] = "*****@*****.**" html = "<html><head></head><body>Test Message</body></html>" htmlPart = MIMEText(html, "html") msg.attach(htmlPart) sensor = imap_email_content.EmailContentSensor( opp, FakeEMailReader(deque([msg])), "test_emails_sensor", ["*****@*****.**"], None, ) sensor.entity_id = "sensor.emailtest" sensor.async_schedule_update_op_state(True) await opp.async_block_till_done() assert sensor.state == "Link" assert (sensor.extra_state_attributes["body"] == "<html><head></head><body>Test Message</body></html>")
async def test_allowed_sender(opp): """Test emails from allowed sender.""" test_message = email.message.Message() test_message["From"] = "*****@*****.**" test_message["Subject"] = "Test" test_message["Date"] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message.set_payload("Test Message") sensor = imap_email_content.EmailContentSensor( opp, FakeEMailReader(deque([test_message])), "test_emails_sensor", ["*****@*****.**"], None, ) sensor.entity_id = "sensor.emailtest" sensor.async_schedule_update_op_state(True) await opp.async_block_till_done() assert sensor.state == "Test" assert sensor.extra_state_attributes["body"] == "Test Message" assert sensor.extra_state_attributes["from"] == "*****@*****.**" assert sensor.extra_state_attributes["subject"] == "Test" assert (datetime.datetime(2016, 1, 1, 12, 44, 57) == sensor.extra_state_attributes["date"])
async def test_sender_not_allowed(opp): """Test not whitelisted emails.""" test_message = email.message.Message() test_message["From"] = "*****@*****.**" test_message["Subject"] = "Test" test_message["Date"] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message.set_payload("Test Message") sensor = imap_email_content.EmailContentSensor( opp, FakeEMailReader(deque([test_message])), "test_emails_sensor", ["*****@*****.**"], None, ) sensor.entity_id = "sensor.emailtest" sensor.async_schedule_update_op_state(True) await opp.async_block_till_done() assert sensor.state is None
async def test_template(opp): """Test value template.""" test_message = email.message.Message() test_message["From"] = "*****@*****.**" test_message["Subject"] = "Test" test_message["Date"] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message.set_payload("Test Message") sensor = imap_email_content.EmailContentSensor( opp, FakeEMailReader(deque([test_message])), "test_emails_sensor", ["*****@*****.**"], Template("{{ subject }} from {{ from }} with message {{ body }}", opp), ) sensor.entity_id = "sensor.emailtest" sensor.async_schedule_update_op_state(True) await opp.async_block_till_done() assert sensor.state == "Test from [email protected] with message Test Message"
async def test_multiple_emails(opp): """Test multiple emails.""" states = [] test_message1 = email.message.Message() test_message1["From"] = "*****@*****.**" test_message1["Subject"] = "Test" test_message1["Date"] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message1.set_payload("Test Message") test_message2 = email.message.Message() test_message2["From"] = "*****@*****.**" test_message2["Subject"] = "Test 2" test_message2["Date"] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message2.set_payload("Test Message 2") def state_changed_listener(entity_id, from_s, to_s): states.append(to_s) async_track_state_change(opp, ["sensor.emailtest"], state_changed_listener) sensor = imap_email_content.EmailContentSensor( opp, FakeEMailReader(deque([test_message1, test_message2])), "test_emails_sensor", ["*****@*****.**"], None, ) sensor.entity_id = "sensor.emailtest" sensor.async_schedule_update_op_state(True) await opp.async_block_till_done() sensor.async_schedule_update_op_state(True) await opp.async_block_till_done() assert states[0].state == "Test" assert states[1].state == "Test 2" assert sensor.extra_state_attributes["body"] == "Test Message 2"