Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Notification component #51

Closed
Sylph opened this issue Dec 14, 2018 · 2 comments
Closed

Feature Request: Notification component #51

Sylph opened this issue Dec 14, 2018 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Sylph
Copy link

Sylph commented Dec 14, 2018

I'm rusty with python and have just started using home assistant a couple days ago, but I needed a notification component so I made this hackish custom component.

I'll be setting up the rest of my systems before I might come back to clean this up. Posting this just in case someone need something similar.
It might be easier for someone w/ more experience to clean it up.
There are some unnecessary imports, should probably confirm that the echo devices exist in hass, renamed to echo tts, have media_player.alexa dependencies set, and allow setting volume in config file too?

configurations:

notify:
  - id: alexa_tts
    name: Alexa TTS
    platform: alexa_tts
    alexa_devices:
      - echo_dot

notify/alexa_tts.py

"""
Support for the Alexa media player Alex as a notification service.
"""
import logging
import json

from homeassistant.components import media_player
from homeassistant.components.notify import (
    BaseNotificationService, ATTR_TITLE)
from homeassistant.const import (
    CONF_API_KEY, CONF_DOMAIN, CONF_RECIPIENT, CONF_SENDER)

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['media_player','logger']

ALEXA_DEVICES = 'alexa_devices'
SERVICE_NAME = "Alexa TTS Notification"
ERROR_PREFIX = SERVICE_NAME + " Service Error"

def get_service(hass, config, discovery_info=None):
    """Get the Alexa TTS notification service."""
    alexa_devices = config[ALEXA_DEVICES]

    alexa_service = AlexaNotificationService(alexa_devices, hass)
    if (alexa_service.devices_are_valid):
      return alexa_service
    else:
      _LOGGER.error(ERROR_PREFIX+"alexa tts notification service devices are invalid")
      
    return alexa_service

class AlexaNotificationService(BaseNotificationService):
    """Implement a notification service for the Alexa TTS notifier service."""

    def __init__(self, alexa_devices, hass):
      """Initialize the service."""
      _LOGGER.info(SERVICE_NAME + " is initializing")
      self.alexa_devices = alexa_devices
      self.hass = hass

    def devices_are_valid(self):
      _LOGGER.info(SERVICE_NAME + " is checking if devices are valid")
      """Check whether device exist."""
      if not self.alexa_devices:
        _LOGGER.error(ERROR_PREFIX + "alexa_devices parameter is empty or missing.")
        return False

      devices_are_valid = True
      for device in self.alexa_devices:
        if not device:
          _LOGGER.error(ERROR_PREFIX+"An empty device is passed.")
          devices_are_valid = False
          continue
        if (device not in hass.media_player):
          _LOGGER.error(("{}: Device {} is invalid.").format(ERROR_PREFIX, 
            str(device)))
          devices_are_valid = False
      return devices_are_valid

    def send_message(self, message="", **kwargs):
      title = kwargs.get(ATTR_TITLE)      
      data = '. '.join(filter(None, [title, message]))
      
      _LOGGER.info(SERVICE_NAME + " is sending message to alexa devices with data: " + data)
      
      for alexa_device in self.alexa_devices:
        _LOGGER.info(SERVICE_NAME + " TTS Service call on :" + alexa_device)
        self.hass.services.call('media_player', 'alexa_tts',
          {'entity_id':"media_player." + alexa_device,'message':data})
@Sylph Sylph changed the title Creating a notification component Feature Request: Creating a notification component Dec 14, 2018
@Sylph Sylph changed the title Feature Request: Creating a notification component Feature Request: Notification component Dec 14, 2018
@alandtse alandtse added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Dec 18, 2018
@alandtse
Copy link
Owner

Thanks for this.

While I understand this is hackish for now and just in case someone could use it, if you do come back can you do this in the form of a pull request so we have an option to merge it in? Our eventual goal is to submit this as an official component so it'd be great if you made sure the code you provided conforms to the home-assistant developer guide. You'll note we have outstanding issue to address that (e.g., #14, #16)

Even if you can't, I'll flag this as a first contribution item so someone else with time can help.

@Sylph
Copy link
Author

Sylph commented Dec 18, 2018

I've been very busy recently, but I plan to if I get the time to! :)
It'll be amazing if someone else could do it. I still need to get oriented over the formatting (part of why I didn't make a pull request).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants