From 97c6092675a6729c3138b5f6f8cd2859dfc57685 Mon Sep 17 00:00:00 2001 From: saniho <52693545+saniho@users.noreply.github.com> Date: Wed, 23 Feb 2022 22:06:52 +0100 Subject: [PATCH] add data volume update readme --- .idea/workspace.xml | 34 ++++++++++--- README.md | 19 ++++++- custom_components/apiEarnApp/apiEarnApp.py | 3 ++ custom_components/apiEarnApp/const.py | 2 +- custom_components/apiEarnApp/earnApp.py | 33 +----------- custom_components/apiEarnApp/manifest.json | 2 +- custom_components/apiEarnApp/sensor.py | 50 ++++++++++++++++++- .../apiEarnApp/sensorApiEarnApp.py | 13 ++++- .../apiEarnApp/testapiEarnApp.py | 17 ------- testapiEarnApp.py | 24 +++++++++ 10 files changed, 135 insertions(+), 62 deletions(-) delete mode 100644 custom_components/apiEarnApp/testapiEarnApp.py create mode 100644 testapiEarnApp.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a85034d..9861327 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,8 +3,14 @@ + + + + - + + + + + + + + @@ -52,11 +63,11 @@ - + - + - + - + @@ -314,7 +325,14 @@ diff --git a/README.md b/README.md index 1ed74fe..3b78d7c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ # apiEarnApp -Objectif, recuperer le montant earnApp \ No newline at end of file +Objectif, recuperer le montant earnApp + +config yaml +``` +- platform: apiEarnApp + token: xxx + scan_interval: 120 +``` + +pour la recupération du token : + +ouvrir : https://earnapp.com/dashboard + +et dans l'outil de developpement chrome + +et recuperer le token : oauth-refresh-token + +![](img/token.png) \ No newline at end of file diff --git a/custom_components/apiEarnApp/apiEarnApp.py b/custom_components/apiEarnApp/apiEarnApp.py index 9335c1a..b3f3060 100644 --- a/custom_components/apiEarnApp/apiEarnApp.py +++ b/custom_components/apiEarnApp/apiEarnApp.py @@ -33,6 +33,9 @@ def getInfo(self): def getMoney(self): return self._money.get("balance",0) + def getTotalMoney(self): + return self._money.get("earnings_total",0) + def getData(self): vol = 0 for device in self._devices: diff --git a/custom_components/apiEarnApp/const.py b/custom_components/apiEarnApp/const.py index 14888c3..ee03d73 100644 --- a/custom_components/apiEarnApp/const.py +++ b/custom_components/apiEarnApp/const.py @@ -8,6 +8,6 @@ # delai pour l'update http, toutes les 3 heures SCAN_INTERVAL_http = datetime.timedelta(seconds=60*60*3) -__VERSION__ = "1.0.0.2" +__VERSION__ = "1.0.0.4" __name__ = "apiEarnApp" \ No newline at end of file diff --git a/custom_components/apiEarnApp/earnApp.py b/custom_components/apiEarnApp/earnApp.py index 0f289d1..bd60283 100644 --- a/custom_components/apiEarnApp/earnApp.py +++ b/custom_components/apiEarnApp/earnApp.py @@ -37,7 +37,7 @@ def login(self, token: str, method: str="google") -> bool: # return the right value depending on succeeding/failing return True return False - + def userData(self) -> dict: """ Get data about the logged in user @@ -116,37 +116,6 @@ def transactions(self) -> dict: resp = makeEarnAppRequest("transactions", "GET", self.cookies) # get all transactions - try: - jsonData = resp.json() # attempt to get the JSON data - except: - return None # if it failed return NoneType - return jsonData - - def linkDevice(self, deviceID: str) -> dict: - """ - Link a device to the logged in EarnApp account - :param deviceID: EarnApp device ID to link to account - :return: a dictionary containing error message/success - """ - - resp = makeEarnAppRequest("link_device", "POST", self.cookies, {"uuid": deviceID}) # send request - - try: - jsonData = resp.json() # attempt to get the JSON data - except: - return None # if it failed return NoneType - return jsonData - - def redeemDetails(self, toEmail: str, paymentMethod: str="paypal.com") -> dict: - """ - Change the redeem details of the logged in account - :param toEmail: The email address to send payment to - :param paymentMethod: optional payment method to send via - :return: a dictionary containing error message/success - """ - - resp = makeEarnAppRequest("redeem_details", "POST", self.cookies, {"to": toEmail, "payment_method": paymentMethod}) # send request - try: jsonData = resp.json() # attempt to get the JSON data except: diff --git a/custom_components/apiEarnApp/manifest.json b/custom_components/apiEarnApp/manifest.json index 325aa0b..b438381 100644 --- a/custom_components/apiEarnApp/manifest.json +++ b/custom_components/apiEarnApp/manifest.json @@ -2,7 +2,7 @@ "domain": "apiEarnApp", "name": "apiEarnApp sensor", "documentation": "", - "version": "1.0.0.2", + "version": "1.0.0.4", "requirements": [ ], "dependencies": [], diff --git a/custom_components/apiEarnApp/sensor.py b/custom_components/apiEarnApp/sensor.py index c38a6cc..94eef01 100644 --- a/custom_components/apiEarnApp/sensor.py +++ b/custom_components/apiEarnApp/sensor.py @@ -66,6 +66,9 @@ def getmyEarnApp(self): def getMoney(self): return self._myEarnApp.getMoney() + def getTotalMoney(self): + return self._myEarnApp.getTotalMoney() + def getData(self): return self._myEarnApp.getData() @@ -84,6 +87,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): myEarn = myEarnApp( token, update_interval ) myEarn.update() add_entities([infoEanAppSensorMoney(session, name, update_interval, myEarn )], True) + add_entities([infoEanAppSensorTotalMoney(session, name, update_interval, myEarn )], True) add_entities([infoEanAppSensorData(session, name, update_interval, myEarn )], True) class infoEanAppSensorMoney(Entity): @@ -118,7 +122,51 @@ def unit_of_measurement(self): def _update(self): """Update device state.""" self._myEarn.update() - self._state, self._attributes = self._sAM.getstatus() + self._state, self._attributes = self._sAM.getstatusMoney() + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return self._attributes + + @property + def icon(self): + """Icon to use in the frontend.""" + return ICON + +class infoEanAppSensorTotalMoney(Entity): + """.""" + + def __init__(self, session, name, interval, myEarn): + """Initialize the sensor.""" + self._session = session + self._name = name + self._myEarn = myEarn + self._attributes = None + self._state = None + self.update = Throttle(interval)(self._update) + self._sAM = sensorApiEarnApp.manageSensorState() + self._sAM.init( self._myEarn ) + + @property + def name(self): + """Return the name of the sensor.""" + return "myEarnApp.totalMoney" + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def unit_of_measurement(self): + """Return the unit of measurement of this entity, if any.""" + return "$" + + def _update(self): + """Update device state.""" + self._myEarn.update() + self._state, self._attributes = self._sAM.getstatusTotalMoney() @property def device_state_attributes(self): diff --git a/custom_components/apiEarnApp/sensorApiEarnApp.py b/custom_components/apiEarnApp/sensorApiEarnApp.py index 3f8d6d4..bc15367 100644 --- a/custom_components/apiEarnApp/sensorApiEarnApp.py +++ b/custom_components/apiEarnApp/sensorApiEarnApp.py @@ -29,7 +29,7 @@ def init(self, _myEarnApp, _LOGGER=None, version=None): self._LOGGER = _LOGGER self.version = version - def getstatus(self): + def getstatusMoney(self): state = "unavailable" status_counts = defaultdict(int) status_counts["version"] = self.version @@ -40,6 +40,17 @@ def getstatus(self): self._state = self._myEarnApp.getMoney() return self._state, self._attributes + def getstatusTotalMoney(self): + state = "unavailable" + status_counts = defaultdict(int) + status_counts["version"] = self.version + + status_counts["version"] = __VERSION__ + status_counts["amount"] = self._myEarnApp.getTotalMoney() + self._attributes = status_counts + self._state = self._myEarnApp.getTotalMoney() + return self._state, self._attributes + def getstatusData(self): state = "unavailable" status_counts = defaultdict(int) diff --git a/custom_components/apiEarnApp/testapiEarnApp.py b/custom_components/apiEarnApp/testapiEarnApp.py deleted file mode 100644 index ed9802e..0000000 --- a/custom_components/apiEarnApp/testapiEarnApp.py +++ /dev/null @@ -1,17 +0,0 @@ -def testMoney(): - import apiEarnApp, sensorApiEarnApp - - _myEarn = apiEarnApp.apiEarnApp() - token = "...." - _myEarn.setToken( token ) - _myEarn.getInfo() - print(_myEarn.getMoney()) - sAM = sensorApiEarnApp.manageSensorState() - sAM.init(_myEarn ) - state, attributes = sAM.getstatus() - sensorApiEarnApp.logSensorState( attributes ) - state, attributes = sAM.getstatusData() - sensorApiEarnApp.logSensorState( attributes ) - - -testMoney() \ No newline at end of file diff --git a/testapiEarnApp.py b/testapiEarnApp.py new file mode 100644 index 0000000..9651aa3 --- /dev/null +++ b/testapiEarnApp.py @@ -0,0 +1,24 @@ +def testMoney(): + from custom_components.apiEarnApp import apiEarnApp, sensorApiEarnApp + + _myEarn = apiEarnApp.apiEarnApp() + + import configparser + mon_conteneur = configparser.ConfigParser() + mon_conteneur.read("../myCredential/security.txt") + print( mon_conteneur.keys) + token = mon_conteneur["EARNAPP"]['TOKEN'] + _myEarn.setToken( token ) + _myEarn.getInfo() + print(_myEarn.getMoney()) + sAM = sensorApiEarnApp.manageSensorState() + sAM.init(_myEarn ) + state, attributes = sAM.getstatusMoney() + sensorApiEarnApp.logSensorState( attributes ) + state, attributes = sAM.getstatusTotalMoney() + sensorApiEarnApp.logSensorState( attributes ) + state, attributes = sAM.getstatusData() + sensorApiEarnApp.logSensorState( attributes ) + + +testMoney() \ No newline at end of file