Hydro-Quebec-API-Wrapper 4.3.1__tar.gz → 4.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.pylintrc +2 -2
  2. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0/Hydro_Quebec_API_Wrapper.egg-info}/PKG-INFO +1 -1
  3. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/Hydro_Quebec_API_Wrapper.egg-info/SOURCES.txt +3 -1
  4. {hydro_quebec_api_wrapper-4.3.1/Hydro_Quebec_API_Wrapper.egg-info → hydro_quebec_api_wrapper-4.5.0}/PKG-INFO +1 -1
  5. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/hydro_api/client.py +143 -31
  6. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/hydro_api/consts.py +74 -0
  7. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/webuser.py +12 -1
  8. hydro_quebec_api_wrapper-4.5.0/tests/unit/test_mobile_client.py +180 -0
  9. hydro_quebec_api_wrapper-4.5.0/tests/unit/test_portal_unavailability.py +132 -0
  10. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.coveragerc +0 -0
  11. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.devcontainer/Dockerfile +0 -0
  12. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.devcontainer/devcontainer.json +0 -0
  13. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.flake8 +0 -0
  14. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.github/scripts/home_assistant_versions.py +0 -0
  15. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.github/workflows/ci.yml +0 -0
  16. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.github/workflows/release.yml +0 -0
  17. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.gitignore +0 -0
  18. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.gitlab-ci.yml +0 -0
  19. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/.pre-commit-config.yaml +0 -0
  20. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/Hydro_Quebec_API_Wrapper.egg-info/dependency_links.txt +0 -0
  21. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/Hydro_Quebec_API_Wrapper.egg-info/entry_points.txt +0 -0
  22. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/Hydro_Quebec_API_Wrapper.egg-info/requires.txt +0 -0
  23. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/Hydro_Quebec_API_Wrapper.egg-info/top_level.txt +0 -0
  24. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/LICENSE +0 -0
  25. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/MANIFEST.in +0 -0
  26. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/README.md +0 -0
  27. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/config.default.yaml +0 -0
  28. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/conflict_test_requirements.txt +0 -0
  29. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/Makefile +0 -0
  30. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/make.bat +0 -0
  31. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/_static/custom.css +0 -0
  32. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/_static/hydroqc-logo-128.png +0 -0
  33. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/_static/hydroqc-logo.svg +0 -0
  34. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/conf.py +0 -0
  35. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/external/description-des-codes-interruption.pdf +0 -0
  36. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/index.rst +0 -0
  37. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/hydroqc.contract.rst +0 -0
  38. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/hydroqc.hydro_api.rst +0 -0
  39. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/hydroqc.peak.cpc.rst +0 -0
  40. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/hydroqc.peak.dpc.rst +0 -0
  41. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/hydroqc.peak.rst +0 -0
  42. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/hydroqc.rst +0 -0
  43. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/hydroqc.types.rst +0 -0
  44. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/reference/modules.rst +0 -0
  45. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/docs/source/todo.rst +0 -0
  46. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/examples/hydro.py +0 -0
  47. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/__init__.py +0 -0
  48. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/account.py +0 -0
  49. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/consts.py +0 -0
  50. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/__init__.py +0 -0
  51. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/common.py +0 -0
  52. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/contract_d.py +0 -0
  53. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/contract_d_cpc.py +0 -0
  54. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/contract_dpc.py +0 -0
  55. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/contract_dt.py +0 -0
  56. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/contract_m.py +0 -0
  57. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/contract_m_gdp.py +0 -0
  58. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/contract_residential.py +0 -0
  59. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/contract/outage.py +0 -0
  60. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/customer.py +0 -0
  61. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/diagnostic.py +0 -0
  62. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/error.py +0 -0
  63. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/hydro_api/__init__.py +0 -0
  64. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/hydro_api/cache.py +0 -0
  65. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/hydro_api/mfa.py +0 -0
  66. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/logger.py +0 -0
  67. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/__init__.py +0 -0
  68. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/basepeak.py +0 -0
  69. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/basepeakhandler.py +0 -0
  70. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/consts.py +0 -0
  71. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/cpc/__init__.py +0 -0
  72. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/cpc/consts.py +0 -0
  73. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/cpc/handler.py +0 -0
  74. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/cpc/peak.py +0 -0
  75. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/dpc/__init__.py +0 -0
  76. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/dpc/consts.py +0 -0
  77. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/dpc/handler.py +0 -0
  78. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/peak/dpc/peak.py +0 -0
  79. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/public_client.py +0 -0
  80. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/py.typed +0 -0
  81. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/timerange.py +0 -0
  82. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/__init__.py +0 -0
  83. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/account.py +0 -0
  84. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/common.py +0 -0
  85. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/consump.py +0 -0
  86. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/contract.py +0 -0
  87. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/cpc.py +0 -0
  88. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/dpc.py +0 -0
  89. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/dt.py +0 -0
  90. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/types/outage.py +0 -0
  91. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/hydroqc/utils.py +0 -0
  92. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/logo/hydroqc-logo-128.png +0 -0
  93. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/logo/hydroqc-logo.svg +0 -0
  94. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/pyproject.toml +0 -0
  95. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/renovate.json5 +0 -0
  96. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/setup.cfg +0 -0
  97. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/test_requirements.txt +0 -0
  98. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/__init__.py +0 -0
  99. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/conftest.py +0 -0
  100. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/subtest_common.py +0 -0
  101. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/subtest_contract_d.py +0 -0
  102. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/subtest_contract_d_cpc.py +0 -0
  103. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/subtest_contract_dpc.py +0 -0
  104. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/subtest_contract_dt.py +0 -0
  105. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/subtest_contract_m.py +0 -0
  106. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/subtest_contract_m_gdp.py +0 -0
  107. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/test_basic.py +0 -0
  108. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/test_timerange.py +0 -0
  109. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/tools.py +0 -0
  110. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/unit/__init__.py +0 -0
  111. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/unit/test_login_flow.py +0 -0
  112. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tests/unit/test_mfa.py +0 -0
  113. {hydro_quebec_api_wrapper-4.3.1 → hydro_quebec_api_wrapper-4.5.0}/tox.ini +0 -0
@@ -201,7 +201,7 @@ ignore-long-lines=^\s*(# )?<?https?://\S+>?$
201
201
  single-line-if-stmt=no
202
202
 
203
203
  # Maximum number of lines in a module
204
- max-module-lines=1500
204
+ max-module-lines=1700
205
205
 
206
206
  # String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
207
207
  # tab).
@@ -245,7 +245,7 @@ max-statements=70
245
245
  max-parents=7
246
246
 
247
247
  # Maximum number of attributes for a class (see R0902).
248
- max-attributes=25
248
+ max-attributes=30
249
249
 
250
250
  # Minimum number of public methods for a class (see R0903).
251
251
  min-public-methods=1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Hydro-Quebec-API-Wrapper
3
- Version: 4.3.1
3
+ Version: 4.5.0
4
4
  Summary: A wrapper library to access hydro quebec API and more
5
5
  Home-page: https://hydroqc.ca
6
6
  Author-email: Hydroqc Team <info@hydroqc.ca>
@@ -107,4 +107,6 @@ tests/test_timerange.py
107
107
  tests/tools.py
108
108
  tests/unit/__init__.py
109
109
  tests/unit/test_login_flow.py
110
- tests/unit/test_mfa.py
110
+ tests/unit/test_mfa.py
111
+ tests/unit/test_mobile_client.py
112
+ tests/unit/test_portal_unavailability.py
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Hydro-Quebec-API-Wrapper
3
- Version: 4.3.1
3
+ Version: 4.5.0
4
4
  Summary: A wrapper library to access hydro quebec API and more
5
5
  Home-page: https://hydroqc.ca
6
6
  Author-email: Hydroqc Team <info@hydroqc.ca>
@@ -31,12 +31,9 @@ from hydroqc.error import (
31
31
  from hydroqc.hydro_api.cache import CCached
32
32
  from hydroqc.hydro_api.consts import (
33
33
  ANNUAL_DATA_URL,
34
- AUTH_CALLBACK_URL,
35
- AUTH_URL,
36
- AUTHORIZE_URL,
34
+ AUTH_CLIENT_WEB,
35
+ AUTH_CLIENTS,
37
36
  AZB2C_CLIENT_ID_WEB,
38
- AZB2C_POLICY,
39
- AZB2C_SCOPE_WEB,
40
37
  AZB2C_TIMEOUT_SKEW_SECS,
41
38
  CONSO_CSV_URL,
42
39
  CONSO_OVERVIEW_CSV_URL,
@@ -56,11 +53,13 @@ from hydroqc.hydro_api.consts import (
56
53
  OPEN_DATA_PEAK_URL,
57
54
  OUTAGES,
58
55
  PERIOD_DATA_URL,
56
+ PORTAL_UNAVAILABILITY_DATE_FORMAT,
57
+ PORTAL_UNAVAILABILITY_SERVICES,
58
+ PORTAL_UNAVAILABILITY_URL,
59
59
  PORTRAIT_URL,
60
60
  RELATION_URL,
61
61
  REQUESTS_TIMEOUT,
62
62
  SESSION_URL,
63
- TOKEN_URL,
64
63
  )
65
64
  from hydroqc.hydro_api.mfa import (
66
65
  B2CPage,
@@ -128,6 +127,7 @@ class HydroClient:
128
127
  cookie_jar: aiohttp.CookieJar | None = None,
129
128
  refresh_token: str = "",
130
129
  refresh_token_expiry: datetime | None = None,
130
+ auth_client: str = AUTH_CLIENT_WEB,
131
131
  ):
132
132
  """Initialize the client object.
133
133
 
@@ -153,8 +153,17 @@ class HydroClient:
153
153
  needed. Defaults to "".
154
154
  refresh_token_expiry: Expiry of ``refresh_token``. Defaults to None,
155
155
  which marks the session as already expired.
156
+ auth_client: B2C client to log in with, ``"web"`` (the Espace
157
+ client) or ``"mobile"`` (the Android app, whose refresh token
158
+ lasts 90 days). Defaults to ``"web"``.
159
+
160
+ Raises:
161
+ ValueError: ``auth_client`` is not a known client.
156
162
 
157
163
  """
164
+ if auth_client not in AUTH_CLIENTS:
165
+ raise ValueError(f"Unknown auth_client: {auth_client!r}")
166
+ self._auth = AUTH_CLIENTS[auth_client]
158
167
  self.username: str = username
159
168
  self.password: str = password
160
169
  self._timeout: int = timeout
@@ -400,8 +409,77 @@ class HydroClient:
400
409
  await self._session.close()
401
410
  self._session = None
402
411
 
412
+ async def get_portal_unavailability(self) -> dict[str, str] | None:
413
+ """Return the planned unavailability window in effect, if any.
414
+
415
+ Reads the feed the Espace client uses to show its maintenance page. The
416
+ result holds ``service``, ``from``, ``to`` (Hydro-Quebec local time) and
417
+ the ``messages_fr``/``messages_en`` HTML. ``None`` when no window covers
418
+ the current time or when the feed cannot be read.
419
+ """
420
+ await self._get_httpsession()
421
+ try:
422
+ # Same cache buster as the Espace client, so an early end shows up.
423
+ params = {"date": str(int(datetime.now().timestamp() * 1000))}
424
+ response = await self.http_request(
425
+ PORTAL_UNAVAILABILITY_URL, "get", params=params, status=None
426
+ )
427
+ if response.status != 200:
428
+ self._logger.debug(
429
+ "Unavailability feed answered HTTP %s", response.status
430
+ )
431
+ return None
432
+ feed = await response.json(content_type=None)
433
+ except (HydroQcError, aiohttp.ClientError, asyncio.TimeoutError, ValueError):
434
+ self._logger.debug("Could not read the unavailability feed", exc_info=True)
435
+ return None
436
+ if not isinstance(feed, dict):
437
+ return None
438
+
439
+ now = datetime.now(EST_TIMEZONE)
440
+ for service in PORTAL_UNAVAILABILITY_SERVICES:
441
+ windows = feed.get(service)
442
+ if not isinstance(windows, list):
443
+ continue
444
+ for window in windows:
445
+ try:
446
+ start = EST_TIMEZONE.localize(
447
+ datetime.strptime(
448
+ window["from"], PORTAL_UNAVAILABILITY_DATE_FORMAT
449
+ )
450
+ )
451
+ end = EST_TIMEZONE.localize(
452
+ datetime.strptime(
453
+ window["to"], PORTAL_UNAVAILABILITY_DATE_FORMAT
454
+ )
455
+ )
456
+ except (KeyError, TypeError, ValueError):
457
+ continue
458
+ if start <= now < end:
459
+ return {
460
+ "service": service,
461
+ "from": window["from"],
462
+ "to": window["to"],
463
+ "messages_fr": str(window.get("messages_fr", "")),
464
+ "messages_en": str(window.get("messages_en", "")),
465
+ }
466
+ return None
467
+
403
468
  async def check_portal_status(self) -> bool:
404
- """Check if Hydro Quebec portal is UP."""
469
+ """Check if Hydro Quebec portal is UP.
470
+
471
+ The portal page keeps answering 200 during a planned maintenance, so the
472
+ unavailability feed is checked first.
473
+ """
474
+ if (unavailability := await self.get_portal_unavailability()) is not None:
475
+ self._logger.info(
476
+ "Hydro Quebec announces a maintenance of %s from %s to %s",
477
+ unavailability["service"],
478
+ unavailability["from"],
479
+ unavailability["to"],
480
+ )
481
+ return False
482
+
405
483
  # Get http session
406
484
  await self._get_httpsession()
407
485
  self._logger.info("Checking if the Hydro Quebec portal is UP")
@@ -482,10 +560,10 @@ class HydroClient:
482
560
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
483
561
  }
484
562
  params = {
485
- "redirect_uri": AUTH_CALLBACK_URL,
486
- "client_id": AZB2C_CLIENT_ID_WEB,
563
+ "redirect_uri": self._auth.redirect_uri,
564
+ "client_id": self._auth.client_id,
487
565
  "response_type": "code",
488
- "scope": AZB2C_SCOPE_WEB,
566
+ "scope": self._auth.scope,
489
567
  "prompt": prompt,
490
568
  "ui_locales": "fr",
491
569
  "code_challenge": code_challenge,
@@ -496,7 +574,11 @@ class HydroClient:
496
574
  # Allow redirects for the initial authorize call
497
575
  # (Azure B2C sometimes issues a 302 before the final HTML)
498
576
  res = await self.http_request(
499
- AUTHORIZE_URL, "get", headers=headers, params=params, allow_redirects=True
577
+ self._auth.authorize_url,
578
+ "get",
579
+ headers=headers,
580
+ params=params,
581
+ allow_redirects=True,
500
582
  )
501
583
  if res.status != 200:
502
584
  self._logger.error(
@@ -514,7 +596,7 @@ class HydroClient:
514
596
  ``signInName`` and ``password``, the ``x-csrf-token`` header set to the
515
597
  value extracted from the sign-in page and the session cookies.
516
598
  """
517
- url = AUTH_URL + "?tx=" + transid + "&p=" + AZB2C_POLICY
599
+ url = self._auth.auth_url + "?tx=" + transid + "&p=" + self._auth.policy
518
600
  headers = {
519
601
  "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
520
602
  "accept": "application/json, text/javascript, */*; q=0.01",
@@ -556,7 +638,7 @@ class HydroClient:
556
638
  ``SelfAsserted`` when the call follows the verification page.
557
639
  """
558
640
  url = (
559
- AUTH_URL.rsplit("/", 1)[0]
641
+ self._auth.auth_url.rsplit("/", 1)[0]
560
642
  + "/api/"
561
643
  + api
562
644
  + "/confirmed?csrf_token="
@@ -564,7 +646,7 @@ class HydroClient:
564
646
  + "&tx="
565
647
  + transid
566
648
  + "&p="
567
- + AZB2C_POLICY
649
+ + self._auth.policy
568
650
  )
569
651
  if api == "CombinedSigninAndSignup":
570
652
  url += "&rememberMe=false"
@@ -846,10 +928,10 @@ class HydroClient:
846
928
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
847
929
  }
848
930
  params = {
849
- "redirect_uri": AUTH_CALLBACK_URL,
850
- "client_id": AZB2C_CLIENT_ID_WEB,
931
+ "redirect_uri": self._auth.redirect_uri,
932
+ "client_id": self._auth.client_id,
851
933
  "response_type": "code",
852
- "scope": AZB2C_SCOPE_WEB,
934
+ "scope": self._auth.scope,
853
935
  "prompt": "none",
854
936
  "ui_locales": "fr",
855
937
  "code_challenge": code_challenge,
@@ -857,7 +939,7 @@ class HydroClient:
857
939
  "mobile": "false",
858
940
  }
859
941
  res = await self.http_request(
860
- AUTHORIZE_URL,
942
+ self._auth.authorize_url,
861
943
  "get",
862
944
  headers=headers,
863
945
  params=params,
@@ -878,13 +960,15 @@ class HydroClient:
878
960
  headers = {"content-type": "application/x-www-form-urlencoded", "accept": "*/*"}
879
961
  data = {
880
962
  "grant_type": "authorization_code",
881
- "client_id": AZB2C_CLIENT_ID_WEB,
882
- "redirect_uri": AUTH_CALLBACK_URL,
963
+ "client_id": self._auth.client_id,
964
+ "redirect_uri": self._auth.redirect_uri,
883
965
  "code": code,
884
966
  "code_verifier": self._code_verifier,
885
967
  }
886
968
 
887
- res = await self.http_request(TOKEN_URL, "post", headers=headers, data=data)
969
+ res = await self.http_request(
970
+ self._auth.token_url, "post", headers=headers, data=data
971
+ )
888
972
  payload = await self._json_or_empty(res)
889
973
  if not payload.get("access_token"):
890
974
  self._logger.error(
@@ -934,6 +1018,7 @@ class HydroClient:
934
1018
  sign-on cookie is still valid.
935
1019
  """
936
1020
  return {
1021
+ "client_id": self._auth.client_id,
937
1022
  "refresh_token": self.refresh_token,
938
1023
  "refresh_token_expiry": self.refresh_token_expiry.isoformat(),
939
1024
  "access_token": self.access_token,
@@ -946,7 +1031,23 @@ class HydroClient:
946
1031
  }
947
1032
 
948
1033
  def import_session_state(self, state: Mapping[str, Any]) -> None:
949
- """Restore a session produced by :meth:`export_session_state`."""
1034
+ """Restore a session produced by :meth:`export_session_state`.
1035
+
1036
+ The tokens are restored only when the state records the current client
1037
+ id. A state with no ``client_id`` predates this field and comes from
1038
+ the web client. Tokens of another client cannot be refreshed here, so
1039
+ they are dropped and the caller gets a plain interactive login. The
1040
+ login host cookies are restored either way, as they belong to the host
1041
+ and not to one client.
1042
+ """
1043
+ if state.get("client_id", AZB2C_CLIENT_ID_WEB) != self._auth.client_id:
1044
+ self._logger.info(
1045
+ "Ignoring the tokens of a stored session issued for another "
1046
+ "B2C client (an interactive login is required)"
1047
+ )
1048
+ self._restore_session_cookies(state)
1049
+ return
1050
+
950
1051
  refresh_token = state.get("refresh_token")
951
1052
  if isinstance(refresh_token, str):
952
1053
  self.refresh_token = refresh_token
@@ -963,6 +1064,10 @@ class HydroClient:
963
1064
  if isinstance(access_expiry, str):
964
1065
  self.access_token_expiry = self._parse_datetime(access_expiry)
965
1066
 
1067
+ self._restore_session_cookies(state)
1068
+
1069
+ def _restore_session_cookies(self, state: Mapping[str, Any]) -> None:
1070
+ """Restore the login host cookies of a stored session."""
966
1071
  cookies = state.get("cookies")
967
1072
  if isinstance(cookies, list):
968
1073
  if self._cookie_jar is None:
@@ -989,15 +1094,15 @@ class HydroClient:
989
1094
  """Refresh current session."""
990
1095
  self._logger.debug("Refreshing access token")
991
1096
 
992
- url = TOKEN_URL
1097
+ url = self._auth.token_url
993
1098
  headers = {
994
1099
  "Content-Type": "application/x-www-form-urlencoded",
995
1100
  "Accept": "application/json, text/plain, */*",
996
1101
  }
997
1102
  data = {
998
1103
  "grant_type": "refresh_token",
999
- "scope": AZB2C_SCOPE_WEB,
1000
- "client_id": AZB2C_CLIENT_ID_WEB,
1104
+ "scope": self._auth.scope,
1105
+ "client_id": self._auth.client_id,
1001
1106
  "refresh_token": self.refresh_token,
1002
1107
  }
1003
1108
 
@@ -1113,7 +1218,9 @@ class HydroClient:
1113
1218
  "Content-Type": "application/json",
1114
1219
  "Authorization": "Bearer " + await self._get_access_token(),
1115
1220
  }
1116
- res = await self.http_request(RELATION_URL, "get", headers=headers)
1221
+ res = await self.http_request(
1222
+ self._auth.api_url(RELATION_URL), "get", headers=headers
1223
+ )
1117
1224
  # TODO http errors
1118
1225
  data: list[AccountTyping] = await res.json()
1119
1226
  return data
@@ -1127,7 +1234,7 @@ class HydroClient:
1127
1234
  headers = await self._get_customer_http_headers(applicant_id, customer_id)
1128
1235
  params = {"withCredentials": "true"}
1129
1236
  api_call_response = await self.http_request(
1130
- CUSTOMER_INFO_URL,
1237
+ self._auth.api_url(CUSTOMER_INFO_URL),
1131
1238
  "get",
1132
1239
  headers=headers,
1133
1240
  params=params,
@@ -1173,7 +1280,9 @@ class HydroClient:
1173
1280
  ) -> AccountContractSummaryTyping:
1174
1281
  """Get all account_contract linked to a customer."""
1175
1282
  headers = await self._get_customer_http_headers(applicant_id, customer_id)
1176
- res = await self.http_request(CONTRACT_SUMMARY_URL, "get", headers=headers)
1283
+ res = await self.http_request(
1284
+ self._auth.api_url(CONTRACT_SUMMARY_URL), "get", headers=headers
1285
+ )
1177
1286
  data: AccountContractSummaryTyping = await res.json()
1178
1287
  return data
1179
1288
 
@@ -1201,7 +1310,10 @@ class HydroClient:
1201
1310
  }
1202
1311
  post_data_str = json_dumps(post_data)
1203
1312
  res = await self.http_request(
1204
- CONTRACT_LIST_URL, "post", headers=headers, data=post_data_str
1313
+ self._auth.api_url(CONTRACT_LIST_URL),
1314
+ "post",
1315
+ headers=headers,
1316
+ data=post_data_str,
1205
1317
  )
1206
1318
  data: ListContractsTyping = await res.json()
1207
1319
  # We ask only one contract, so we should have only one
@@ -1221,7 +1333,7 @@ class HydroClient:
1221
1333
  headers = await self._get_customer_http_headers(applicant_id, customer_id)
1222
1334
  params = {"noContrat": contract_id}
1223
1335
  api_call_response = await self.http_request(
1224
- GET_CPC_API_URL,
1336
+ self._auth.api_url(GET_CPC_API_URL),
1225
1337
  "get",
1226
1338
  headers=headers,
1227
1339
  params=params,
@@ -1396,7 +1508,7 @@ class HydroClient:
1396
1508
  headers = await self._get_customer_http_headers(applicant_id, customer_id)
1397
1509
  params = {"noContrat": contract_id}
1398
1510
  api_call_response = await self.http_request(
1399
- FLEXD_DATA_URL, "get", params=params, headers=headers
1511
+ self._auth.api_url(FLEXD_DATA_URL), "get", params=params, headers=headers
1400
1512
  )
1401
1513
  # We can not use res.json() because the response header are not application/json
1402
1514
  data: DPCDataTyping = self._load_json(await api_call_response.text())
@@ -5,6 +5,8 @@
5
5
  avoid all the /portail/ URLs
6
6
  """
7
7
 
8
+ from dataclasses import dataclass
9
+
8
10
  # Always get the time using HydroQuebec Local Time
9
11
  REQUESTS_TIMEOUT = 30
10
12
  REQUESTS_TTL = 1
@@ -26,6 +28,11 @@ AZB2C_RESPONSE_TYPE = "code"
26
28
  AZB2C_SCOPE_WEB = (
27
29
  "openid https://connexionhq.onmicrosoft.com/hq-clientele/Espace.Client"
28
30
  )
31
+ AZB2C_POLICY_MOBILE = "B2C_1A_PRD_signup_signin_mobile"
32
+ AZB2C_SCOPE_MOBILE = (
33
+ "openid offline_access "
34
+ "https://connexionhq.onmicrosoft.com/hq-clientele-mobile/Espace.Client"
35
+ )
29
36
  AZB2C_CODE_CHALLENGE_METHOD = "S256"
30
37
 
31
38
  # Time to remove from the token expiration time to avoid calls to fail
@@ -39,6 +46,9 @@ AUTHORIZE_URL = (
39
46
  )
40
47
  AUTH_URL = f"{HOST_LOGIN}/{AZB2C_TENANT_ID}/{AZB2C_POLICY}/SelfAsserted"
41
48
  AUTH_CALLBACK_URL = f"{HOST_SESSION}/oauth2/callback"
49
+ AUTH_CALLBACK_URL_MOBILE = (
50
+ "msauth://com.hydroquebec.mf_android/2TD6UgHUYzHyQirztabBgWTrJNs%3D"
51
+ )
42
52
  TOKEN_URL = f"{HOST_LOGIN}/{AZB2C_TENANT_ID}/{AZB2C_POLICY.lower()}/oauth2/v2.0/token"
43
53
 
44
54
  CHECK_SESSION_URL = f"{HOST_LOGIN}/hqam/oauth2/connect/checkSession"
@@ -130,3 +140,67 @@ GET_CPC_API_URL = (
130
140
  )
131
141
  # IS PORTAL RUNNING
132
142
  IS_HYDRO_PORTAL_UP_URL = f"{HOST_SESSION}/portail/fr/group/clientele/gerer-mon-compte"
143
+
144
+
145
+ @dataclass(frozen=True)
146
+ class AuthClient:
147
+ """Azure B2C client used to log in, with the private API it can call."""
148
+
149
+ client_id: str
150
+ policy: str
151
+ scope: str
152
+ redirect_uri: str
153
+ # Path segment of the private API: /wsapi/<api_channel>/prive/...
154
+ api_channel: str
155
+
156
+ @property
157
+ def authorize_url(self) -> str:
158
+ """Return the authorize endpoint of the policy."""
159
+ return (
160
+ f"{HOST_LOGIN}/{AZB2C_TENANT_ID}/{self.policy.lower()}"
161
+ "/oauth2/v2.0/authorize"
162
+ )
163
+
164
+ @property
165
+ def auth_url(self) -> str:
166
+ """Return the SelfAsserted endpoint of the policy."""
167
+ return f"{HOST_LOGIN}/{AZB2C_TENANT_ID}/{self.policy}/SelfAsserted"
168
+
169
+ @property
170
+ def token_url(self) -> str:
171
+ """Return the token endpoint of the policy."""
172
+ return f"{HOST_LOGIN}/{AZB2C_TENANT_ID}/{self.policy.lower()}/oauth2/v2.0/token"
173
+
174
+ def api_url(self, url: str) -> str:
175
+ """Return a private API URL (declared on /wsapi/web/) for this client."""
176
+ return url.replace("/wsapi/web/", f"/wsapi/{self.api_channel}/", 1)
177
+
178
+
179
+ AUTH_CLIENT_WEB = "web"
180
+ AUTH_CLIENT_MOBILE = "mobile"
181
+ AUTH_CLIENTS = {
182
+ AUTH_CLIENT_WEB: AuthClient(
183
+ client_id=AZB2C_CLIENT_ID_WEB,
184
+ policy=AZB2C_POLICY,
185
+ scope=AZB2C_SCOPE_WEB,
186
+ redirect_uri=AUTH_CALLBACK_URL,
187
+ api_channel="web",
188
+ ),
189
+ # Client of the official Android app: offline_access gives it a 90-day
190
+ # sliding refresh token, so the e-mail code is not asked every day.
191
+ AUTH_CLIENT_MOBILE: AuthClient(
192
+ client_id=AZB2C_CLIENT_ID_MOBILE,
193
+ policy=AZB2C_POLICY_MOBILE,
194
+ scope=AZB2C_SCOPE_MOBILE,
195
+ redirect_uri=AUTH_CALLBACK_URL_MOBILE,
196
+ api_channel="mobile",
197
+ ),
198
+ }
199
+
200
+ # Maintenance windows the Espace client reads (indisponibiliteUrl in its config/app.json).
201
+ PORTAL_UNAVAILABILITY_URL = (
202
+ "https://clientele-statique.solutions.hydroquebec.com/config/indispo/"
203
+ "indisponibilites.json"
204
+ )
205
+ PORTAL_UNAVAILABILITY_SERVICES = ("libreService", "appMobileAuthentifiee")
206
+ PORTAL_UNAVAILABILITY_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
@@ -12,6 +12,7 @@ from hydroqc.consts import LoginStatus
12
12
  from hydroqc.customer import Customer
13
13
  from hydroqc.error import HydroQcError
14
14
  from hydroqc.hydro_api.client import HydroClient
15
+ from hydroqc.hydro_api.consts import AUTH_CLIENT_WEB
15
16
  from hydroqc.logger import get_logger
16
17
 
17
18
 
@@ -35,8 +36,13 @@ class WebUser:
35
36
  cookie_jar: aiohttp.CookieJar | None = None,
36
37
  refresh_token: str = "",
37
38
  refresh_token_expiry: datetime | None = None,
39
+ auth_client: str = AUTH_CLIENT_WEB,
38
40
  ):
39
- """Create a new Hydroquebec webuser."""
41
+ """Create a new Hydroquebec webuser.
42
+
43
+ ``auth_client`` selects the B2C client, ``"web"`` (default) or
44
+ ``"mobile"``, see :class:`HydroClient`.
45
+ """
40
46
  self._hydro_client = HydroClient(
41
47
  username,
42
48
  password,
@@ -47,6 +53,7 @@ class WebUser:
47
53
  cookie_jar=cookie_jar,
48
54
  refresh_token=refresh_token,
49
55
  refresh_token_expiry=refresh_token_expiry,
56
+ auth_client=auth_client,
50
57
  )
51
58
  self._username: str = username
52
59
  self._log_level: str | None = log_level
@@ -58,6 +65,10 @@ class WebUser:
58
65
  """Check if the Hydro Quebec website/portal is available."""
59
66
  return await self._hydro_client.check_portal_status()
60
67
 
68
+ async def get_hq_portal_unavailability(self) -> dict[str, str] | None:
69
+ """Return the planned Hydro Quebec unavailability in effect, if any."""
70
+ return await self._hydro_client.get_portal_unavailability()
71
+
61
72
  async def login(self) -> LoginStatus:
62
73
  """Login to Hydroquebec website.
63
74