Skip to content

Commit

Permalink
Fix issue with photo embedding
Browse files Browse the repository at this point in the history
And move calls async
  • Loading branch information
RogerSelwyn committed May 2, 2022
1 parent f9dfedd commit d0eff81
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions custom_components/o365/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@
import logging
import os

from homeassistant.components.notify import (ATTR_DATA, ATTR_TARGET,
ATTR_TITLE,
BaseNotificationService)

from .const import (ATTR_ATTACHMENTS, ATTR_MESSAGE_IS_HTML, ATTR_PHOTOS,
ATTR_ZIP_ATTACHMENTS, ATTR_ZIP_NAME, CONF_ACCOUNT,
CONF_ACCOUNT_NAME, CONF_CONFIG_TYPE, DOMAIN,
PERM_MAIL_SEND, PERM_MINIMUM_SEND)
from homeassistant.components.notify import (
ATTR_DATA,
ATTR_TARGET,
ATTR_TITLE,
BaseNotificationService,
)

from .const import (
ATTR_ATTACHMENTS,
ATTR_MESSAGE_IS_HTML,
ATTR_PHOTOS,
ATTR_ZIP_ATTACHMENTS,
ATTR_ZIP_NAME,
CONF_ACCOUNT,
CONF_ACCOUNT_NAME,
CONF_CONFIG_TYPE,
DOMAIN,
PERM_MAIL_SEND,
PERM_MINIMUM_SEND,
)
from .schema import NOTIFY_BASE_SCHEMA
from .utils import (build_token_filename, get_ha_filepath, get_permissions,
validate_minimum_permission, zip_files)
from .utils import (
build_token_filename,
get_ha_filepath,
get_permissions,
validate_minimum_permission,
zip_files,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,6 +71,10 @@ def targets(self):

def send_message(self, message="", **kwargs):
"""Send a message to a user."""
_LOGGER.warning("Non async send_message unsupported")

async def async_send_message(self, message="", **kwargs):
"""Send an async message to a user."""
if not validate_minimum_permission(PERM_MINIMUM_SEND, self._permissions):
_LOGGER.error(
"Not authorisied to send mail - requires permission: %s", PERM_MAIL_SEND
Expand All @@ -72,15 +93,16 @@ def send_message(self, message="", **kwargs):
if data and data.get(ATTR_TARGET, None):
target = data.get(ATTR_TARGET)
else:
target = self.account.get_current_user().mail
resp = await self.hass.async_add_executor_job(self.account.get_current_user)
target = resp.mail

new_message = self.account.new_message()
message = self._build_message(data, message, new_message.attachments)
self._build_attachments(data, new_message.attachments)
new_message.to.add(target)
new_message.subject = title
new_message.body = message
new_message.send()
await self.hass.async_add_executor_job(new_message.send)

self._cleanup()

Expand All @@ -105,16 +127,18 @@ def _build_photo_content(self, photos, new_message_attachments):
photos = [photos]

photos_content = ""
i = 0
for photo in photos:
i += 1
if photo.startswith("http"):
photos_content += f'<br><img src="{photo}">'
else:
photo = get_ha_filepath(self._hass, photo)
new_message_attachments.add(photo)
att = new_message_attachments[-1]
att.is_inline = True
att.content_id = "1"
photos_content += f'<br><img src="cid:{photo}">'
att.content_id = str(i)
photos_content += f'<br><img src="cid:{att.content_id}">'

return photos_content

Expand Down

0 comments on commit d0eff81

Please sign in to comment.