Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

possible to change persistent notifications to regular notifications? #12

Closed
Mariusthvdb opened this issue Jul 26, 2018 · 8 comments · Fixed by #14
Closed

possible to change persistent notifications to regular notifications? #12

Mariusthvdb opened this issue Jul 26, 2018 · 8 comments · Fixed by #14
Assignees

Comments

@Mariusthvdb
Copy link

Mariusthvdb commented Jul 26, 2018

hi,
thanks for the last update.

creating persistent notifications for each new authentication has certain ramifications on the system, i would like to prevent. Would like to change to regular notifications.

could the line

self.hass.components.persistent_notification.create('{}'.format(ip_address + ' (' + geo_country + ', ' + geo_region + ', ' + geo_city + ')'), 'New successful login from')
be adapted to

self.hass.components.notify.notify('{}'.format(ip_address + ' (' + geo_country + ', ' + geo_region + ', ' + geo_city + ')'), 'New successful login from')

and have regular notifications?

the user can always add an automation to create pers. notifications for each or certain regular notification if so desired (by creating a dedicated template, i include one here)

- alias: 'Forward notify to persistent notifications'
  id: 'Forward notify to persistent notifications'
#  initial_state: 'on'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: notify
  condition:
    condition: state
    entity_id: input_boolean.forward_notify_persistent
    state: 'on'
  action:
    service: persistent_notification.create
    data_template:
      message: >
        {% set message = trigger.event.data.service_data.message %}
        {% if 'invalid authentication' in message or 'login attempt' in message %}
          {{ message }}: http://www.ip-tracker.org/locator/ip-lookup.php?ip={{ message.split('from ')[1] }}
        {% else %}
          {{ message }}
        {% endif %}

btw this works also the other way round ;-)

- alias: 'Forward persistent notifications'
  id: 'Forward persistent notifications'
#  initial_state: 'on'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: create
  condition:
    condition: state
    entity_id: input_boolean.notify_notify
    state: 'on'
  action:
    service: notify.notify
    data_template:
      message: >
        {% set message = trigger.event.data.service_data.message %}
        {% if 'invalid authentication' in message or 'login attempt' in message %}
         Forwarded Pers Not: {{ message }}: http://www.ip-tracker.org/locator/ip-lookup.php?ip={{ message.split('from ')[1] }}
        {% else %}
         Forwarded Pers Not: {{ message }}
        {% endif %}
@ludeeus
Copy link
Contributor

ludeeus commented Jul 26, 2018

Hi,

Thanks for your sugestion, but this will not work if the user has not defined a notify.notify
It will give this error:

AttributeError: module 'homeassistant.components.notify' has no attribute 'notify'

You are better off construction your ovn notify action like you have in your examples and disable the persistent_notification with:

sensor:
  - platform: authenticated
    enable_notification: 'False'

@ludeeus ludeeus self-assigned this Jul 26, 2018
@Mariusthvdb
Copy link
Author

this will not work if the user has not defined a notify.notify

ok in understand, though users not having notify.notify must be an extreme minority ;-)

still, if one has notify.notify, it is safe to make that adjustment?

You are better off construction your ovn notify action like you

second: if the notification is disabled, what would be the correct trigger for a notify action picking up the sensors activity?

@ludeeus
Copy link
Contributor

ludeeus commented Jul 26, 2018

still, if one has notify.notify, it is safe to make that adjustment?

I would not recomend that, it wil break on updates.

second: if the notification is disabled, what would be the correct trigger for a notify action picking up the sensors activity?

This adds a sensor sensor.last_successful_authentication you can trigger on state changes in that

@ludeeus
Copy link
Contributor

ludeeus commented Jul 26, 2018

This adds a sensor sensor.last_successful_authentication you can trigger on state changes in that

Then it will trigger on every IP....
@hvbhome I need to add another attribute, that you can listen to.
does new_ip: true/false sound good?
You can then add this trigger:

automation:
  trigger:
    platform: template
    value_template: "{{ is_state_attr('sensor.last_successful_authentication', 'new_ip', 'true') }}"

@Mariusthvdb
Copy link
Author

that would be great, thank you!

@tjorim
Copy link
Contributor

tjorim commented Aug 7, 2018

What about letting the user define another notify service, but by default it would use persistent_notification.create? Something like what the Configurator add-on can do.

@ludeeus
Copy link
Contributor

ludeeus commented Aug 7, 2018

@tjorim That will get to messy.
At that point the platform would behave as an automation, this is not desired.

@caiosweet
Copy link

This adds a sensor sensor.last_successful_authentication you can trigger on state changes in that

Then it will trigger on every IP....
@hvbhome I need to add another attribute, that you can listen to.
does new_ip: true/false sound good?
You can then add this trigger:

automation:
  trigger:
    platform: template
    value_template: "{{ is_state_attr('sensor.last_successful_authentication', 'new_ip', 'true') }}"

Hello. This doesn't work for me. The new_ip attribute never changes from false to true.
but, I get both persistent notification and log.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

4 participants