Skip to content

Commit 537edbb

Browse files
authored
Merge pull request #179 from saniho/dev
Dev
2 parents c80b699 + e59d955 commit 537edbb

39 files changed

+285
-183
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
exclude: (?x)^( \.idea/.* |.*cache/.* |.scannerwork/.* |.*properties )$
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v4.1.0
5+
rev: v4.2.0
66
hooks:
77
- id: no-commit-to-branch
88
args: [--branch, main]
@@ -27,21 +27,21 @@ repos:
2727
- id: yamllint
2828
args: [--no-warnings, -d, '{extends: relaxed, rules: {line-length: {max: 120}}}']
2929
- repo: https://github.com/asottile/pyupgrade
30-
rev: v2.31.1
30+
rev: v2.32.0
3131
hooks:
3232
- id: pyupgrade
3333
args:
3434
- --py37-plus
3535
- repo: https://github.com/psf/black
36-
rev: 22.1.0
36+
rev: 22.3.0
3737
hooks:
3838
- id: black
3939
args:
4040
- --safe
4141
- --quiet
4242
- -l 88
4343
- repo: https://github.com/Lucas-C/pre-commit-hooks-bandit
44-
rev: v1.0.5
44+
rev: v1.0.6
4545
hooks:
4646
- id: python-bandit-vulnerability-check
4747
args: [--skip, 'B101,B105,B110,B307,B310,B311', --recursive, .]
@@ -79,7 +79,7 @@ repos:
7979
# - --skip="./.*"
8080
# - --quiet-level=2
8181
- repo: https://github.com/pre-commit/mirrors-mypy
82-
rev: v0.942
82+
rev: v0.950
8383
hooks:
8484
- id: mypy
8585
args: [--ignore-missing-imports, --install-types, --non-interactive, --check-untyped-defs,

README.md

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,62 @@ Avant de pouvoir utiliser cette intégration, assurez vous :
2626
## Installer l'intégration
2727

2828
<details>
29-
<summary><b>Via HACS (mise à jour en un clic) : </b></summary><br>
29+
<summary><b>Via HACS (mise à jour en un clic) : </b></summary><br />
3030

3131
- Ouvrez HACS, cliquez sur `Intégration`, puis selectionnez le menu 3
3232
points en haut à droite.
3333

3434
\*si vous n'avez pas HACS, pour l'installer cela se passe ici :
3535
[HACS : Ajoutez des modules et des cartes personnalisées](https://forum.hacf.fr/t/hacs-ajoutez-des-modules-et-des-cartes-personnalisees/359)
3636

37-
<br>
37+
<br />
3838
<p align="center">
39-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_add_repo_01.png" height="300"/>
40-
<br>
39+
<img src="./img/HACS_add_repo_01.png" height="300"/>
40+
<br />
4141
</p>
4242

4343
- Ajoutez le dépot personnalisé : `https://github.com/saniho/apiEnedis`
4444

45-
<br>
45+
<br />
4646
<p align="center">
47-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_add_repo_02.png" width="600"/>
48-
<br>
47+
<img src="./img/HACS_add_repo_02.png" width="600"/>
48+
<br />
4949
</p>
5050

5151
- Cliquez sur le bouton `Installer` de la carte correspondant à
5252
l'intégration
5353

54-
<br>
54+
<br />
5555
<p align="center">
56-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_install_integration_01.png" width="400"/>
57-
<br>
56+
<img src="./img/HACS_install_integration_01.png" width="400"/>
57+
<br />
5858
</p>
5959

6060
- Cliquez sur le bouton `Installer` de la popup
6161

62-
<br>
62+
<br />
6363
<p align="center">
64-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_install_integration_02.png" width="600"/>
65-
<br>
64+
<img src="./img/HACS_install_integration_02.png" width="600"/>
65+
<br />
6666
</p>
6767

6868
- La carte de l'intégration est maintenant rouge, signifiant qu'un
6969
redémarrage du serveur Home Assistant est nécessaire
7070

71-
<br>
71+
<br />
7272
<p align="center">
73-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_install_integration_03.png" width="400"/>
74-
<br>
73+
<img src="./img/HACS_install_integration_03.png" width="400"/>
74+
<br />
7575
</p>
7676

7777
- Accédez à la vue `Contrôle du serveur` (`Configuration` ->
7878
`Contrôle du serveur`), puis cliquez sur le bouton `Redémarrer` dans la
7979
zone `Gestion du serveur`
8080

81-
<br>
81+
<br />
8282
<p align="center">
83-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_install_integration_04.png" width="400"/>
84-
<br>
83+
<img src="./img/HACS_install_integration_04.png" width="400"/>
84+
<br />
8585
</p>
8686

8787
</details>
@@ -108,19 +108,19 @@ Avant de pouvoir utiliser cette intégration, assurez vous :
108108
- Appuyez sur le bouton bleu `Ajouter l'intégration` en bas à droite de la
109109
vue
110110

111-
<br>
111+
<br />
112112
<p align="center">
113-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_add_integration_01.png" height="500"/>
114-
<br>
113+
<img src="./img/HACS_add_integration_01.png" height="500"/>
114+
<br />
115115
</p>
116116

117117
- Tapez dans le champ de recherche qui vient d'apparaître : `myenedis` et
118118
cliquez sur l'intégration
119119

120-
<br>
120+
<br />
121121
<p align="center">
122-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_add_integration_02.png" height="300"/>
123-
<br>
122+
<img src="./img/HACS_add_integration_02.png" height="300"/>
123+
<br />
124124
</p>
125125

126126
- Renseigner :
@@ -137,10 +137,10 @@ Avant de pouvoir utiliser cette intégration, assurez vous :
137137

138138
- Validez la saisie avec le bouton `Soumettre`
139139

140-
<br>
140+
<br />
141141
<p align="center">
142-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/HACS_add_integration_03.png" width="300"/>
143-
<br>
142+
<img src="./img/HACS_add_integration_03.png" width="300"/>
143+
<br />
144144
</p>
145145

146146
- Fermez la popup de confirmation en cliquant sur le bouton `Terminer`
@@ -160,12 +160,16 @@ votre navigateur en faisant la combinaison de touche `CTRL+F5` ou
160160

161161
L'intégration crée l'entité `sensor.myenedis_<<votrecode>>`
162162

163-
<br>
163+
<br />
164164
<p align="center">
165-
<img src="https://raw.githubusercontent.com/saniho/apiEnedis/main/img/sensor_v2.png"/>
166-
<br>
165+
<img src="./img/sensor_v2.png"/>
166+
<br />
167167
</p>
168168

169+
## "Ca ne marche pas"
170+
171+
### Forum HACF.FR
172+
169173
______________________________________________________________________
170174

171175
N'hésitez pas à aller faire un tour sur ce forum ou vous trouverez pleins
@@ -175,6 +179,54 @@ https://forum.hacf.fr/t/hacs-ajoutez-des-modules-et-des-cartes-personnalisees/35
175179

176180
______________________________________________________________________
177181

182+
183+
### Activer le débogue
184+
185+
<details>
186+
<summary><b>Via l'interface, temporairement : </b></summary><br />
187+
188+
Avec des traces de débogue il est généralement plus facile d'identifier
189+
la cause d'un problème de fonctionnement.
190+
191+
Une manière c'est d'activer les messages de débogue dans le fichier
192+
`config/home-assistant.log` en l'activant depuis l'interface. Pour cela allez
193+
directement vers [Outils de Développement > Services](https://my.home-assistant.io/redirect/developer_services/).
194+
195+
Puis, passer en mode `YAML` et copiez-collez le code suivant, puis cliquez
196+
`APPELER LE SERVICE`:
197+
198+
```yaml
199+
service: logger.set_level
200+
data:
201+
myEnedis: debug
202+
203+
```
204+
205+
En image:
206+
207+
<p align="center"><img src="./img/ha_debug.png" width="300"/></p>
208+
209+
Le fichier `config/home-assistant.log` se remplit alors de pleins de traces
210+
de débogue lié à `apiEnedis` connu comme `myEnedis` dans Home Assistant.
211+
212+
213+
Ceci continue jusqu'au redémarrage de Home Assistant ou jusqu'à ce que vous
214+
exécutez de la même manière que pour l'activation:
215+
216+
217+
```yaml
218+
service: logger.set_level
219+
data:
220+
myEnedis: warning
221+
222+
```
223+
</details>
224+
225+
226+
## Historique des versions
227+
228+
Cette partie n'est pas tenu à jour.
229+
178230
<details>
179231
<summary><b>VERSION</b></summary>
180232

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Constants for API Json
2+
3+
# Error code - string
4+
ERROR_CODE = "error_code"
5+
# Error string - string
6+
ERROR = "error"
7+
8+
# Note: both of these fields occur - same meaning:
9+
# Alert user - bool
10+
ALERT_USER = "alert_user"
11+
# User alert - bool
12+
USER_ALERT = "user_alert"
13+
14+
# tag
15+
TAG = "tag"
16+
# description - string
17+
DESCRIPTION = "description"
18+
19+
# Structure with meter reading data
20+
METER_READING = "meter_reading"
21+
22+
ENEDIS_RETURN = "enedis_return"
23+
# Fields in "enedis_return"
24+
ENEDIS_RETURN_ERROR = "error"
25+
ENEDIS_RETURN_MESSAGE = "message"

custom_components/apiEnedis/const.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ def __init__(self):
6363
PLATFORMS = ["sensor"]
6464

6565
_formatDateYmd = "%Y-%m-%d"
66-
_formatDateYm01 = "%Y-%m-01"
67-
_formatDateY0101 = "%Y-01-01"
6866

6967
ENTITY_NAME = "name"
7068
ENTITY_DELAI = "delai"

custom_components/apiEnedis/myCall.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import re
1616
import sys
1717

18+
from . import apiconst as API
19+
1820
_LOGGER = logging.getLogger(__nameMyEnedis__)
1921

2022
# Min and Max Delays for initial call and subsequent calls.
@@ -134,13 +136,18 @@ def post_and_get_json(self, url, params=None, data=None, headers=None):
134136
minDelay: float = INITIAL_CALL_DELAY
135137
while maxTriesToGo > 0:
136138
maxTriesToGo -= 1
139+
hasError = True # Suppose error, will be set to False if no exception
137140
try:
138141
if not myCall.isAvailable():
142+
_LOGGER.warning(
143+
"Nombre d'appels à l'API dépassé,"
144+
" ou dernière erreur trop récente"
145+
)
139146
dataAnswer = {
140-
"error_code": "UNAVAILABLE",
141-
"enedis_return": {
142-
"error": "UNAVAILABLE",
143-
"message": "Indisponible, essayez plus tard",
147+
API.ERROR_CODE: "UNAVAILABLE",
148+
API.ENEDIS_RETURN: {
149+
API.ENEDIS_RETURN_ERROR: "UNAVAILABLE",
150+
API.ENEDIS_RETURN_MESSAGE: "Indisponible, essayez plus tard",
144151
},
145152
}
146153
self.setLastAnswer(dataAnswer)
@@ -162,15 +169,14 @@ def post_and_get_json(self, url, params=None, data=None, headers=None):
162169
logPrefix = f"====== Appel http #{counter} !!! "
163170
_LOGGER.info(f"{logPrefix}=====")
164171

165-
# raise(requests.exceptions.Timeout) # pour raiser un timeout de test ;)
166172
response = session.post(
167173
url,
168174
params=params,
169175
data=json.dumps(data),
170176
headers=headers,
171177
timeout=30,
172178
)
173-
# Generate test data with next line
179+
# Write API result to file (test generation, debug)
174180
self.saveApiReturn(counter, response.text)
175181

176182
response.raise_for_status()
@@ -180,14 +186,15 @@ def post_and_get_json(self, url, params=None, data=None, headers=None):
180186
_LOGGER.info(f"{logPrefix}data : {data} =====")
181187
_LOGGER.info(f"{logPrefix}reponse : {dataAnswer} =====")
182188
maxTriesToGo = 0 # Done
189+
hasError = False
183190
except requests.exceptions.Timeout:
184191
myCall.handleTimeout()
185192
# a ajouter raison de l'erreur !!!
186193
_LOGGER.error(f"{logPrefix}requests.exceptions.Timeout")
187194
dataAnswer = {
188-
"enedis_return": {
189-
"error": "UNKERROR_TIMEOUT",
190-
"message": "Timeout",
195+
API.ENEDIS_RETURN: {
196+
API.ENEDIS_RETURN_ERROR: "UNKERROR_TIMEOUT",
197+
API.ENEDIS_RETURN_MESSAGE: "Timeout",
191198
}
192199
}
193200
self.setLastAnswer(dataAnswer)
@@ -202,8 +209,7 @@ def post_and_get_json(self, url, params=None, data=None, headers=None):
202209
_LOGGER.error(f"data : {json.dumps(data)} ")
203210
_LOGGER.error(f"Error JSON : {response.text} ")
204211
_LOGGER.error("*" * 60)
205-
# with open('error.json', 'w') as outfile:
206-
# json.dump(response.json(), outfile)
212+
207213
dataAnswer = response.json()
208214
self.setLastAnswer(dataAnswer)
209215

@@ -215,12 +221,13 @@ def post_and_get_json(self, url, params=None, data=None, headers=None):
215221
):
216222
maxTriesToGo = 0 # Fatal error, do not try again
217223

218-
_LOGGER.debug("Data answer: %r", dataAnswer)
219-
# if ( "enedis_return" in dataAnswer.keys() ):
220-
# if ( type( dataAnswer["enedis_return"] ) is dict ):
221-
# if ( "error" in dataAnswer["enedis_return"].keys()):
222-
# if ( dataAnswer["enedis_return"]["error"] == "UNKERROR_TIMEOUT"):
223-
# raise error
224+
# Log result as error in case of exception, or as debug otherwise
225+
_LOGGER.log(
226+
logging.ERROR if hasError else logging.DEBUG,
227+
"Data answer: %r",
228+
dataAnswer,
229+
)
230+
224231
return dataAnswer
225232

226233
def getDataPeriod(self, deb: str, fin: str | None) -> tuple[str, bool]:

0 commit comments

Comments
 (0)