Plugin - Wan IP Checker

Python and python framework
User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 12:34

Hi there.

I have managed to port emme's script bellow

viewtopic.php?t=14489

to a domoticz python plugin.
Plugin updates a "General,Text" Value with your Wan IP Address. This value can be used later on in order to update any scripts or notify you upon IP change.

I hope you like it.

Find Version 1.1.0 bellow:

Code: Select all


# WAN IP Checker
#
# Author: ycahome, 2017
#
"""
<plugin key="WAN-IP-CHECKER" name="Wan IP Checker" author="ycahome" version="1.1.0" externallink="https://www.domoticz.com/forum/viewtopic.php?t=16266">
    <params>
        <param field="Address" label="IP Address" width="200px" required="true" default="https://4.ifcfg.me/ip"/>
        <param field="Mode1" label="Check Interval(seconds)" width="75px" required="true" default="60"/>
        <param field="Mode6" label="Debug" width="75px">
            <options>
                <option label="True" value="Debug"/>
                <option label="False" value="Normal"  default="true" />
            </options>
        </param>
    </params>
</plugin>
"""

import Domoticz
import hmac
import hashlib
import time
import urllib
import urllib.request
import urllib.error

#from urllib2 import urlopen
from datetime import datetime, timedelta

class BasePlugin:
    enabled = False
    pluginState = "Not Ready"
    sessionCookie = ""
    privateKey = b""
    socketOn = "FALSE"

    def __init__(self):
        self.debug = False
        self.error = False
        self.nextpoll = datetime.now()
        self.pollinterval = 60  #Time in seconds between two polls
        return

    def onStart(self):

        Domoticz.Debug("onStart called")
        self.pollinterval = int(Parameters["Mode1"])  #Time in seconds between two polls

        if Parameters["Mode6"] == 'Debug':
            self.debug = True
            Domoticz.Debugging(1)
            DumpConfigToLog()
        else:
            Domoticz.Debugging(0)

        # create the mandatory child device if it does not yet exist
        if 1 not in Devices:
            Domoticz.Device(Name="WAN IP 1", Unit=1, TypeName="Text").Create()
            Domoticz.Log("Device created.")
        DumpConfigToLog()

        #url = "https://4.ifcfg.me/ip"
        url = Parameters["Address"]
        response = urllib.request.urlopen(url).read()

        WANip = str(response,'utf-8')
        WANip = WANip.strip(' \t\n\r')
        Domoticz.Debug("WAN IP retrieved:" + WANip)
        CurWANip = Devices[1].sValue
        CurWANip = CurWANip.strip(' \t\n\r')
        Domoticz.Debug("Previous WAN IP:" + CurWANip)
        if  CurWANip != WANip:
             Domoticz.Log("WAN IP Updated to:" + WANip)
             Devices[1].Update(1,WANip)
        else:
             Domoticz.Log("WAN IP the same. Skipping")
        Domoticz.Heartbeat(int(Parameters["Mode1"]))

    def onStop(self):
        Domoticz.Log("Plugin is stopping.")
        Domoticz.Debugging(0)

    def onHeartbeat(self):

        Domoticz.Debug("onHeartbeat called")

        #url = "https://4.ifcfg.me/ip"
        url = Parameters["Address"]

        now = datetime.now()
        if now >= self.nextpoll:
          self.nextpoll = now + timedelta(seconds=self.pollinterval)
          try:
            response = urllib.request.urlopen(url, timeout = 30).read()
          except urllib.error.HTTPError as err1:
            Domoticz.Error("HTTP Request error: " + str(err1) + " URL: " + url)
            return
          else:

            WANip = str(response,'utf-8')
            WANip = WANip.strip(' \t\n\r')
            Domoticz.Debug("WAN IP retrieved:" + WANip)
            CurWANip = Devices[1].sValue
            CurWANip = CurWANip.strip(' \t\n\r')
            Domoticz.Debug("Previous WAN IP:" + CurWANip)
            if  CurWANip != WANip:
              Domoticz.Log("WAN IP Updated to:" + WANip)
              Devices[1].Update(1,WANip)
            else:
              Domoticz.Log("WAN IP the same. Skipping")



global _plugin
_plugin = BasePlugin()

def onStart():
    global _plugin
    _plugin.onStart()

def onStop():
    global _plugin
    _plugin.onStop()

def onHeartbeat():
    global _plugin
    _plugin.onHeartbeat()


# Generic helper functions
def DumpConfigToLog():
    for x in Parameters:
        if Parameters[x] != "":
            Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
    Domoticz.Debug("Device count: " + str(len(Devices)))
    for x in Devices:
        Domoticz.Debug("Device:           " + str(x) + " - " + str(Devices[x]))
        Domoticz.Debug("Device ID:       '" + str(Devices[x].ID) + "'")
        Domoticz.Debug("Device Name:     '" + Devices[x].Name + "'")
        Domoticz.Debug("Device nValue:    " + str(Devices[x].nValue))
        Domoticz.Debug("Device sValue:   '" + Devices[x].sValue + "'")
    return


#
# Parse an int and return None if no int is given
#

def parseIntValue(s):

        try:
            return int(s)
        except:
            return None






Installation instructions:
- Create Plugin Folder "WAN-IP-CHECKER" under "domoticz/plugin" folder
- Save this script as "plugin.py" on "WAN-IP-CHECKER" folder
- Restart domoticz service.
- Add a new entry of this Hardware on your domoticz installation (Setup/Hardware/select and add "Wan Ip Checker")

What am going to see?:
- Plugin will auto-add one Text Counter on your "Utility" Section named "<Your Hardware Name>- WAN IP 1".
Last edited by ycahome on Monday 04 September 2017 10:38, edited 5 times in total.

User avatar
emme
Posts: 638
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Plugin - Wan IP Checker

Post by emme » Thursday 23 February 2017 13:13

:shock: uh... my first remix.... :lol: :lol: :lol: :lol: :lol:

quite honored!.. Thank you!!! :D

I'll test it this evening as soon as I get back home....
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

JimmyH1969
Posts: 80
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Plugin - Wan IP Checker

Post by JimmyH1969 » Thursday 23 February 2017 16:05

Hi, Nice.... do you need to create a variable? I added it to the plugin folder but can't see it anywhere

User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 16:14

if everything goes as designed ;) , it auto-create a Text Counter on "Utility" Section named "<Your Hardware Name>- WAN IP 1".
Did you added an entry of this specific hardware after Domoticz service restart? (check Installation Instructions on my first post)

JimmyH1969
Posts: 80
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Plugin - Wan IP Checker

Post by JimmyH1969 » Thursday 23 February 2017 18:42

Yes, i got it, but from that moment my Domoticz became unresponsive and i could no longer connect to Domoticz. I had to stop the service, delete the plugin.py and start the service again to get acess to domoticz again.... :shock:

btw this is on Windows :roll:

User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 18:51

Although this is built on Linux, I don't think that there is anything bound to Linux architecture. Its a simple python script.
Do you have any logs?

Unfortunately I don't have a Windows based domoticz to test it...

User avatar
emme
Posts: 638
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Plugin - Wan IP Checker

Post by emme » Thursday 23 February 2017 20:33

Simply GREAT!!!!
it works perfectly!!!!

Just a bit more clarification for the install:
you have to be root (not pi) to enter the plugins directory... so you have to issue a
sudo su
before everything else :P
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Plugin - Wan IP Checker

Post by BarryT » Thursday 23 February 2017 21:47

Nice idea, but i get a lot of errors...

Code: Select all

2017-02-23 20:46:13.797 (WAN IP) Initialized version 1.0.0, author 'ycahome'
2017-02-23 20:46:13.804 Error: (WAN IP) 'onStart' failed 'URLError'.
2017-02-23 20:46:13.804 Error: (WAN IP) ----> Line 94 in /home/pi/domoticz/plugins/wan-ip-checker/plugin.py, function onStart
2017-02-23 20:46:13.804 Error: (WAN IP) ----> Line 49 in /home/pi/domoticz/plugins/wan-ip-checker/plugin.py, function onStart
2017-02-23 20:46:13.804 Error: (WAN IP) ----> Line 162 in /usr/local/lib/python3.5/urllib/request.py, function urlopen
2017-02-23 20:46:13.804 Error: (WAN IP) ----> Line 465 in /usr/local/lib/python3.5/urllib/request.py, function open
2017-02-23 20:46:13.804 Error: (WAN IP) ----> Line 488 in /usr/local/lib/python3.5/urllib/request.py, function _open
2017-02-23 20:46:13.804 Error: (WAN IP) ----> Line 443 in /usr/local/lib/python3.5/urllib/request.py, function _call_chain
2017-02-23 20:46:13.804 Error: (WAN IP) ----> Line 1310 in /usr/local/lib/python3.5/urllib/request.py, function unknown_open
2017-02-23 20:46:23.322 Error: (WAN IP) 'onHeartbeat' failed 'URLError'.
2017-02-23 20:46:23.322 Error: (WAN IP) ----> Line 102 in /home/pi/domoticz/plugins/wan-ip-checker/plugin.py, function onHeartbeat
2017-02-23 20:46:23.322 Error: (WAN IP) ----> Line 73 in /home/pi/domoticz/plugins/wan-ip-checker/plugin.py, function onHeartbeat
2017-02-23 20:46:23.323 Error: (WAN IP) ----> Line 162 in /usr/local/lib/python3.5/urllib/request.py, function urlopen
2017-02-23 20:46:23.323 Error: (WAN IP) ----> Line 465 in /usr/local/lib/python3.5/urllib/request.py, function open
2017-02-23 20:46:23.323 Error: (WAN IP) ----> Line 488 in /usr/local/lib/python3.5/urllib/request.py, function _open
2017-02-23 20:46:23.323 Error: (WAN IP) ----> Line 443 in /usr/local/lib/python3.5/urllib/request.py, function _call_chain
2017-02-23 20:46:23.323 Error: (WAN IP) ----> Line 1310 in /usr/local/lib/python3.5/urllib/request.py, function unknown_open
6x Raspberry 3B
A lot of scripts, switches and sensors :)

User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 21:52

I can't tell for sure....
Am suspecting that you should rename your folder to WAN-IP-CHECKER as folders are case sensitive on Linux OS.

Can you try it?

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Plugin - Wan IP Checker

Post by BarryT » Thursday 23 February 2017 21:54

ycahome wrote:I can't tell for sure....
Am suspecting that you should rename your folder to WAN-IP-CHECKER as folders are case sensitive on Linux OS.

Can you try it?
Hold on a sec
6x Raspberry 3B
A lot of scripts, switches and sensors :)

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Plugin - Wan IP Checker

Post by BarryT » Thursday 23 February 2017 21:56

Code: Select all

2017-02-23 20:55:02.221 (IPChecker) Exiting work loop...
2017-02-23 20:55:02.264 (IPChecker) Stopped.
2017-02-23 20:55:02.388 Error: (WAN-IP-CHECKER) failed to load 'plugin.py', Python Path used was '/home/pi/domoticz/plugins/wan-ip-checker/:/usr/local/lib/python35.zip:/usr/local/lib/python3.5/:/usr/local/lib/python3.5/plat-linux:/usr/local/lib/python3.5/lib-dynload'.
2017-02-23 20:55:02.388 Error: (IPChecker) Module Import failed, exception: 'ImportError'
2017-02-23 20:55:02.388 Error: (IPChecker) Module Import failed: ' Name: plugin'
Rebooting now...
6x Raspberry 3B
A lot of scripts, switches and sensors :)

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Plugin - Wan IP Checker

Post by BarryT » Thursday 23 February 2017 21:57

Code: Select all

2017-02-23 20:56:51.276 Error: (IPChecker) 'onHeartbeat' failed 'URLError'.
2017-02-23 20:56:51.276 Error: (IPChecker) ----> Line 101 in /home/pi/domoticz/plugins/WAN-IP-CHECKER/plugin.py, function onHeartbeat
2017-02-23 20:56:51.276 Error: (IPChecker) ----> Line 72 in /home/pi/domoticz/plugins/WAN-IP-CHECKER/plugin.py, function onHeartbeat
2017-02-23 20:56:51.276 Error: (IPChecker) ----> Line 162 in /usr/local/lib/python3.5/urllib/request.py, function urlopen
2017-02-23 20:56:51.276 Error: (IPChecker) ----> Line 465 in /usr/local/lib/python3.5/urllib/request.py, function open
2017-02-23 20:56:51.276 Error: (IPChecker) ----> Line 488 in /usr/local/lib/python3.5/urllib/request.py, function _open
2017-02-23 20:56:51.276 Error: (IPChecker) ----> Line 443 in /usr/local/lib/python3.5/urllib/request.py, function _call_chain
2017-02-23 20:56:51.276 Error: (IPChecker) ----> Line 1310 in /usr/local/lib/python3.5/urllib/request.py, function unknown_open
2017-02-23 20:56:54.957 OpenZWave: Received timeout notification from HomeID: 25487523, NodeID: 9 (0x09)
2017-02-23 20:56:54.959 OpenZWave: All Nodes queried
2017-02-23 20:57:01.301 Error: (IPChecker) 'onHeartbeat' failed 'URLError'.
2017-02-23 20:57:01.302 Error: (IPChecker) ----> Line 101 in /home/pi/domoticz/plugins/WAN-IP-CHECKER/plugin.py, function onHeartbeat
2017-02-23 20:57:01.302 Error: (IPChecker) ----> Line 72 in /home/pi/domoticz/plugins/WAN-IP-CHECKER/plugin.py, function onHeartbeat
2017-02-23 20:57:01.302 Error: (IPChecker) ----> Line 162 in /usr/local/lib/python3.5/urllib/request.py, function urlopen
2017-02-23 20:57:01.302 Error: (IPChecker) ----> Line 465 in /usr/local/lib/python3.5/urllib/request.py, function open
2017-02-23 20:57:01.302 Error: (IPChecker) ----> Line 488 in /usr/local/lib/python3.5/urllib/request.py, function _open
2017-02-23 20:57:01.302 Error: (IPChecker) ----> Line 443 in /usr/local/lib/python3.5/urllib/request.py, function _call_chain
2017-02-23 20:57:01.302 Error: (IPChecker) ----> Line 1310 in /usr/local/lib/python3.5/urllib/request.py, function unknown_open
2017-02-23 20:57:11.272 Error: (IPChecker) 'onHeartbeat' failed 'URLError'.
2017-02-23 20:57:11.272 Error: (IPChecker) ----> Line 101 in /home/pi/domoticz/plugins/WAN-IP-CHECKER/plugin.py, function onHeartbeat
2017-02-23 20:57:11.272 Error: (IPChecker) ----> Line 72 in /home/pi/domoticz/plugins/WAN-IP-CHECKER/plugin.py, function onHeartbeat
2017-02-23 20:57:11.272 Error: (IPChecker) ----> Line 162 in /usr/local/lib/python3.5/urllib/request.py, function urlopen
2017-02-23 20:57:11.272 Error: (IPChecker) ----> Line 465 in /usr/local/lib/python3.5/urllib/request.py, function open
2017-02-23 20:57:11.272 Error: (IPChecker) ----> Line 488 in /usr/local/lib/python3.5/urllib/request.py, function _open
2017-02-23 20:57:11.272 Error: (IPChecker) ----> Line 443 in /usr/local/lib/python3.5/urllib/request.py, function _call_chain
2017-02-23 20:57:11.272 Error: (IPChecker) ----> Line 1310 in /usr/local/lib/python3.5/urllib/request.py, function unknown_open
Still the same
6x Raspberry 3B
A lot of scripts, switches and sensors :)

User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 22:02

Very strange, This is a raspbian?

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Plugin - Wan IP Checker

Post by BarryT » Thursday 23 February 2017 22:06

ycahome wrote:Very strange, This is a raspbian?
Yes, it is..

2017-02-23 21:03:08.187 Domoticz V3.6775 (c)2012-2017 GizMoCuz
2017-02-23 21:03:08.188 Build Hash: 050cd55, Date: 2017-02-23 12:25:52
2017-02-23 21:03:08.188 Startup Path: /home/pi/domoticz/
2017-02-23 21:03:08.516 EventSystem: reset all events...
2017-02-23 21:03:08.956 PluginSystem: Started, Python version '3.5.0'.

Raspberry 3B with an older sd-card image of domoticz, updated to latest version
6x Raspberry 3B
A lot of scripts, switches and sensors :)

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Plugin - Wan IP Checker

Post by BarryT » Thursday 23 February 2017 22:07

I think it has something to do with the Python, since i had to build it from source after getting errors about Python some weeks ago?
6x Raspberry 3B
A lot of scripts, switches and sensors :)

User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 22:08

let me check on Rpi2 with latest beta

User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 22:11

BarryT wrote:I think it has something to do with the Python, since i had to build it from source after getting errors about Python some weeks ago?
Do you have "python3-dev" installed?

BarryT
Posts: 222
Joined: Tuesday 31 March 2015 22:06
Target OS: Raspberry Pi
Domoticz version: beta
Location: east netherlands
Contact:

Re: Plugin - Wan IP Checker

Post by BarryT » Thursday 23 February 2017 22:17

ycahome wrote:
BarryT wrote:I think it has something to do with the Python, since i had to build it from source after getting errors about Python some weeks ago?
Do you have "python3-dev" installed?
Good question, hold on..

Otherwise, i think i'm going to re-install the rpi because there are just to many (old) files not in use anymore...

** weird, it seems Python 2.7.9 is my main version?
6x Raspberry 3B
A lot of scripts, switches and sensors :)

User avatar
ycahome
Posts: 71
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: Plugin - Wan IP Checker

Post by ycahome » Thursday 23 February 2017 22:27

Works Perfectly on my RPi-2B with Latest beta and Raspbian.

You have probably a lack of Python.....

JimmyH1969
Posts: 80
Joined: Tuesday 28 June 2016 16:38
Target OS: Windows
Domoticz version: Beta
Location: Amersfoort NL
Contact:

Re: Plugin - Wan IP Checker

Post by JimmyH1969 » Thursday 23 February 2017 23:07

Not working on Windows. Domoticz restarts every 10 seconds. Nothing in log, but thats hard to see if the system is no longer responding.
:(

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests