Skip to content

Commit

Permalink
Add some documentation to the code
Browse files Browse the repository at this point in the history
  • Loading branch information
KjellRanda committed Jan 8, 2025
1 parent f611d7e commit ec8e52e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
32 changes: 16 additions & 16 deletions MyUplink/MyUplinkApi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
### A set of classes and function to use both the public and the internal MyUplink API ###
""" A set of classes and function to use both the public and the internal MyUplink API """

import sys
import requests
from MyUplinkConst import PUB_API, INT_API, PUB_BASEURL, INT_BASEURL

class myuplinkapi:
### Class for the MyUplink APIs ###
### TODO: Reduce the number og class members ###
""" Class for the MyUplink APIs.
TODO: Reduce the number og class members """

def __init__(self) -> None:
self.apiver = PUB_API
Expand All @@ -25,25 +25,25 @@ def __init__(self) -> None:
self.ERROR = "ERROR"

def getDevName(self):
### Return the device name ###
""" Return the device name """
return self.devname

def setLogger(self, logger):
### Set a logger to be used by the class ###
""" Set a logger to be used by the class """
self.logger = logger

def setIntAPI(self) -> None:
### Set the class to use the internal API. Public API is default ###
""" Set the class to use the internal API. Public API is default """
self.apiver = INT_API
self.baseurl = INT_BASEURL

def apiUserPasswd(self, USERNAME, PASSWORD) -> None:
### Set the username amd password to be used accessing the MyUplink API ###
""" Set the username amd password to be used accessing the MyUplink API """
self.usrname = USERNAME
self.passwd = PASSWORD

def authorize(self):
### Authorize using username and password. Get an access token an token lifetime in return ###
""" Authorize using username and password. Get an access token an token lifetime in return """
message = f"Using MyUplink API {self.baseurl}"
self._output_(self.INFO, message)
url = self.baseurl + "/oauth/token"
Expand All @@ -70,7 +70,7 @@ def authorize(self):
sys.exit(1)

def getDevID(self):
### Get the device ID from MyUplink API. Responce depends on which API is used ####
""" Get the device ID from MyUplink API. Responce depends on which API is used """
if self.apiver == PUB_API:
url = self.baseurl + "/v2/systems/me"
else:
Expand Down Expand Up @@ -104,7 +104,7 @@ def getDevID(self):
sys.exit(2)

def getdevData(self, params):
### Get a list of device data from the API ###
""" Get a list of device data from the API """
retData = []
url = self.baseurl + "/v2/devices/" + self.devid + "/points?parameters=" + params
self.headers = {
Expand All @@ -129,8 +129,8 @@ def getdevData(self, params):
sys.exit(3)

def updateSchedule(self, json_data):
### Update the weekly heating schedule for the device ###
### The json_data is build from today and tomorrows power cost incling grid fee ###
""" Update the weekly heating schedule for the device
The json_data is build from today and tomorrows power cost incling grid fee """
url = self.baseurl + "/v2/devices/" + self.devid + "/weekly-schedules"
self.headers = {
"Content-Type": "application/json",
Expand All @@ -146,17 +146,17 @@ def updateSchedule(self, json_data):
sys.exit(4)

def getSchedule(self):
### Get the weekly schedule in json format for the device ###
""" Get the weekly schedule in json format for the device """
url = self.baseurl + "/v2/devices/" + self.devid + "/weekly-schedules"
return self._getScheduleData(url, "data")

def getScheduleMode(self):
### Get the schedule modes in json format for the device ###
""" Get the schedule modes in json format for the device """
url = self.baseurl + "/v2/devices/" + self.devid + "/schedule-modes"
return self._getScheduleData(url, "mode")

def _getScheduleData(self, apiurl, mess):
### Get the weekly schedule or schedule mode from the API ###
""" Get the weekly schedule or schedule mode from the API """
self.headers = {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
Expand All @@ -170,7 +170,7 @@ def _getScheduleData(self, apiurl, mess):
sys.exit(5)

def _output_(self, severity, mess):
### Output a logging message. Eiteh using a logger if defines or just a print command ###
""" Output a logging message. Eiteh using a logger if defines or just a print command """
mylogger = self.logger
if mylogger:
if severity == self.INFO:
Expand Down
2 changes: 1 addition & 1 deletion MyUplink/MyUplinkConst.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Various constants used by the myuplink class, function and scripts ###
""" Various constants used by the myuplink class, function and scripts """

DEF_SLEEP = 300

Expand Down
14 changes: 7 additions & 7 deletions MyUplink/MyUplinkUtil.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
### Some utility classes used by the myuplink class, functions and scripts ###
""" Some utility classes used by the myuplink class, functions and scripts """

import configparser
import os
import logging
import logging.handlers

class MyUptimeConfig:
### Class to read a config file usin the configparser library ###
### The config file must be in users home directopy on both Windows and Linux ###
""" Class to read a config file usin the configparser library
The config file must be in users home directopy on both Windows and Linux """
def __init__(self, filename) -> None:
self.configFile = filename
self.home = os.path.expanduser("~")
Expand All @@ -16,14 +16,14 @@ def __init__(self, filename) -> None:
self.config.read(self.inifile)

def getKey(self, key, val):
### Return a value from the config file ###
""" Return a value from the config file """
try:
return self.config[key][val]
except KeyError:
return ""

class myLogger:
### Class for a logger to be used from a python program ###
""" Class for a logger to be used from a python program """
def __init__(self, logfile, version) -> None:
logger = logging.getLogger('__name__')
logger.setLevel(logging.DEBUG)
Expand All @@ -35,12 +35,12 @@ def __init__(self, logfile, version) -> None:
self.logfile = logfile

def getLogger(self):
### Get the logger ###
""" Get the logger """
self.logger.info("Starting ...")
return self.logger

def setLoggerLevel(self, level: str):
### Set the logger level ###
""" Set the logger level """
goodLevels = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
level = level.upper()
if level in goodLevels:
Expand Down

0 comments on commit ec8e52e

Please sign in to comment.