Beispiel #1
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup a D-Link Smart Plug."""
    from pyW215.pyW215 import SmartPlug

    host = config.get(CONF_HOST)
    username = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    name = config.get(CONF_NAME)

    add_devices([SmartPlugSwitch(SmartPlug(host, password, username), name)])
def checkPlug():
    global Power_Status
    sp = SmartPlug('192.168.99.113','225564')
    # Get values if available otherwise return N/A
        print("Checking Plug")
        print(sp.current_consumption)
        print(sp.temperature)
        print(sp.total_consumption)
        if(sp.state == "ON"):
            Power_Status = "ON"
        else:
            Power_Status = "OFF"
Beispiel #3
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up a D-Link Smart Plug."""

    host = config.get(CONF_HOST)
    username = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    use_legacy_protocol = config.get(CONF_USE_LEGACY_PROTOCOL)
    name = config.get(CONF_NAME)

    smartplug = SmartPlug(host, password, username, use_legacy_protocol)
    data = SmartPlugData(smartplug)

    add_entities([SmartPlugSwitch(hass, data, name)], True)
def IoT_to_Raspberry_Change_Power(ShadowPayload):
    global Power_Status
    # Desired = POWER change
    checkPlug()
    print(" I AM IN THE POWER CHANGE FUNCTION")
    print("ShadowPayload" + ShadowPayload)
    print("Power Status" + Power_Status)

    checkPlug()
    sp = SmartPlug('192.168.99.113','225564')
    if ( ShadowPayload == "ON" and Power_Status == "OFF"): #Check if machine is indeed OFF
        print("Power status is off and shadowpayload is on")
        sp.state = 'ON'
        print("Turned plug on")
        checkPlug()
        JSONPayload = '{ "state" : {'+\
                            '"reported": {'+\
                                '"Power": "' + Power_Status + '" '+\
                            '} '+\
                        '} '+\
                    '}'
        myDeviceShadow.shadowUpdate(JSONPayload, IoTShadowCallback_Update, 5) #Send the new status as REPORTED values

    elif ( ShadowPayload == "OFF" and Power_Status == "ON"): #Check if machine is indeed ON
        print("Power status is on and shadowpayload is off")
        sp.state = 'OFF'
        print("Turned plug off")
        checkPlug()
        JSONPayload = '{ "state" : {'+\
                            '"reported": {'+\
                                '"Power": "' + Power_Status + '" '+\
                            '}, '+\
                            '"desired": {'+\
                                '"Power": "' + Power_Status + '" '+\
                            '} '+\
                        '} '+\
                    '}'
        myDeviceShadow.shadowUpdate(JSONPayload, IoTShadowCallback_Update, 5) #Send the new status as REPORTED values
Beispiel #5
0
 def initializePlugs(self, plug={}):
     if plug:
         self.plugs.pop(plug["ID"], None)
         if plug["brand"] == "PW":
             self.plugs[plug["ID"]] = pw.Circle(plug['Mac'].encode(),
                                                pw.Stick(port='COM7'))
             self.updateSensorStatus(plug["ID"], [
                 1 if self.plugs[plug["ID"]].get_info() == "ON" else 0
             ][0])
         elif plug["brand"] == "DL":
             address, pwd = plug['Mac'].encode().split(":")
             self.plugs[plug["ID"]] = SmartPlug(address, pwd)
             self.updateSensorStatus(plug["ID"], [
                 1 if self.plugs[plug["ID"]].state == "ON" else 0
             ][0])
     else:
         self.plugs = {}
         try:
             ports = self.serial_ports()
             if isinstance(ports[0], serial.serialposix.Serial):
                 pw_port = pw.Stick(port=ports[0].port)
             else:
                 pw_port = pw.Stick(port=ports[0])
         except SerialException:
             print("couldnot connect to port")
         for i in self.plug_details:
             if i["brand"] == "PW":
                 self.plugs[i["ID"]] = pw.Circle(i['Mac'].encode(), pw_port)
                 self.updateSensorStatus(i["ID"], [
                     1 if self.plugs[i["ID"]].get_info() == "ON" else 0
                 ][0])
             elif i["brand"] == "DL":
                 address, pwd = i['Mac'].encode().split(":")
                 self.plugs[i["ID"]] = SmartPlug(address, pwd)
                 self.updateSensorStatus(i["ID"], [
                     1 if self.plugs[i["ID"]].state == "ON" else 0
                 ][0])
         print("Initialised Plugs")
Beispiel #6
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup a D-Link Smart Plug."""
    from pyW215.pyW215 import SmartPlug

    host = config.get(CONF_HOST)
    username = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    use_legacy_protocol = config.get(CONF_USE_LEGACY_PROTOCOL)
    name = config.get(CONF_NAME)

    data = SmartPlugData(
        SmartPlug(host, password, username, use_legacy_protocol))

    add_devices([SmartPlugSwitch(hass, data, name)], True)
Beispiel #7
0
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """Find and return D-Link Smart Plugs."""
    from pyW215.pyW215 import SmartPlug

    # check for required values in configuration file
    if not validate_config({DOMAIN: config}, {DOMAIN: [CONF_HOST]}, _LOGGER):
        return False

    host = config.get(CONF_HOST)
    username = config.get(CONF_USERNAME, DEFAULT_USERNAME)
    password = str(config.get(CONF_PASSWORD, DEFAULT_PASSWORD))
    name = config.get(CONF_NAME, DEVICE_DEFAULT_NAME)

    add_devices_callback(
        [SmartPlugSwitch(SmartPlug(host, password, username), name)])
Beispiel #8
0
def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up a D-Link Smart Plug."""

    host = config[CONF_HOST]
    username = config[CONF_USERNAME]
    password = config[CONF_PASSWORD]
    use_legacy_protocol = config[CONF_USE_LEGACY_PROTOCOL]
    name = config[CONF_NAME]

    smartplug = SmartPlug(host, password, username, use_legacy_protocol)
    data = SmartPlugData(smartplug)

    add_entities([SmartPlugSwitch(hass, data, name)], True)
Beispiel #9
0
#!python3
import sys

from pyW215.pyW215 import SmartPlug, ON, OFF

try:
    command = sys.argv[1]
    ip = sys.argv[2]
    password = sys.argv[3]
except:
    print('smart plug: action (on or off), IP, password required')
    exit(1)

sp = SmartPlug(ip, password)

# Get values if available otherwise return N/A
# print(sp.current_consumption)
# print(sp.temperature)
# print(sp.total_consumption)

# Turn switch on and off
if (command == 'on'):
    sp.state = ON
else:
    sp.state = OFF
Beispiel #10
0
def index(request):
    Staff = Group.objects.all()[0]
    Tech = Group.objects.all()[1]
    data = pd.read_csv("C:\\Users\\viclo\\Desktop\\BSP2\\The_project\\final_product\\project\\values.csv", sep=';')
    dic = datamin(data)

    #plugs
    dic_of_plug1 = {'BLAB-PLUG1':dic['BLAB-PLUG1']}
    dic_of_plug2 = {'BLAB-PLUG2':dic['BLAB-PLUG2']}
    on_or_off1 = dic['BLAB-PLUG1']
    on_or_off2 = dic['BLAB-PLUG2']

    #humidity
    dic_of_humid1 = {'BLAB-HUMID-AIR-1':dic['BLAB-HUMID-AIR-1']}
    dic_of_humid2 = {'BLAB-HUMID-SOIL-1':dic['BLAB-HUMID-SOIL-1']}
    dic_of_approx_hum1 = real_to_mod20(dic_of_humid1)
    dic_of_approx_hum2 = real_to_mod20(dic_of_humid2)

    #temeperature                
    dic_of_temp1 = {'BLAB-TEMP-AIR-1':dic['BLAB-TEMP-AIR-1']}
    dic_of_temp2 = {'BLAB-TEMP-AIR-2':dic['BLAB-TEMP-AIR-2']}
    dic_of_temp3 = {'BLAB-TEMP-SOIL-2':dic['BLAB-TEMP-SOIL-2']}                 
    dic_of_approx_temp1 = real_to_mod5(dic_of_temp1)
    dic_of_approx_temp2 = real_to_mod5(dic_of_temp2)
    dic_of_approx_temp3 = real_to_mod5(dic_of_temp3)

    #plug_control
    IP = 'Invalid Ip'
    Password = '******'
    state = ''
    temperature = 0
    consumption = 0
    total_cuns = 0
    form = ""
    
    if request.method == 'POST':
        # Create a form instance and populate it with data from the request (binding):
        form = plugs(request.POST)

        # Check if the form is valid:
        if form.is_valid():
            action = form.cleaned_data['choice_field']

            if (action == 'info_1'):
                IP = '10.212.232.12'
                Password = '******'
                sp = SmartPlug(str(IP), str(Password))
                state = sp.state
                temperature = sp.temperature
                consumption = sp.current_consumption
                total_cuns = sp.total_consumption
            elif (action == 'on_off_1'):
                IP = '10.212.232.12'
                Password = '******'
                sp = SmartPlug(str(IP), str(Password))
                if sp.state == 'OFF':
                    sp.state = ON
                else:
                    sp.state = OFF
                state = sp.state
            elif (action == 'info_2'):
                IP = '10.212.232.11'
                Password = '******'
                sp = SmartPlug(str(IP), str(Password))
                state = sp.state
                temperature = sp.temperature
                consumption = sp.current_consumption
                total_cuns = sp.total_consumption
            elif (action == 'on_off_2'):
                IP = '10.212.232.11'
                Password = '******'
                sp = SmartPlug(str(IP), str(Password))
                if sp.state == 'OFF':
                    sp.state = ON
                else:
                    sp.state = OFF
                state = sp.state
                
    links = []
    for i in range(1,3):
        links += [img('https://messir.uni.lu/bicslab/blab-cam'+str(i)+'-snapshots/gallery-images/')]
    
    return render(request, 'index.html', {'dic_of_temp1':dic_of_temp1,
                                          'img1':links[0],
                                          'img2':links[1],
                                          'dic_of_approx_temp1':dic_of_approx_temp1,
                                          'dic_of_temp2':dic_of_temp2,
                                          'dic_of_approx_temp2':dic_of_approx_temp2,
                                          'dic_of_temp3':dic_of_temp3,
                                          'dic_of_approx_temp3':dic_of_approx_temp3,                                             'dic_of_humid1':dic_of_humid1,
                                          'dic_of_approx_hum1':dic_of_approx_hum1,
                                          'dic_of_humid2':dic_of_humid2,
                                          'dic_of_approx_hum2':dic_of_approx_hum2,
                                          'dic_of_plug1':dic_of_plug1,
                                          'dic_of_plug2':dic_of_plug2,
                                          'on_or_off1':on_or_off1,
                                          'on_or_off2':on_or_off2,
                                          'Staff':Staff,
                                          'Tech':Tech,
                                          'form':form,
                                          'state':state,
                                          'temeperature':temperature,
                                          'consumption':consumption,
                                          'total_cuns':total_cuns
                                               })
Beispiel #11
0
def get_temp():
    """ Get W125 temperature """
    val = sp.temperature
    if val == 'N/A':
        quit("Could not connect to host " + ip)
        return 0
    return val


def get_total():
    """ Get W125 total energy usage """
    val = sp.total_consumption
    if val == 'N/A':
        quit("Could not connect to host " + ip)
        return 0
    return val


# Main entry point
if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(listen_port)

    # Main loop
    while True:

        sp = SmartPlug(ip, code)
        REQUEST_STATE.state(state=get_state())

        time.sleep(sleep_time)