Skip to content

Commit

Permalink
Add support for MySuperior stoves
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwolsink committed Oct 2, 2024
1 parent 0bce314 commit 08871df
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 33 deletions.
24 changes: 13 additions & 11 deletions custom_components/aguaiot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
CONF_CUSTOMER_CODE,
CONF_LOGIN_API_URL,
CONF_UUID,
CONF_BRAND_ID,
CONF_BRAND,
DOMAIN,
PLATFORMS,
UPDATE_INTERVAL,
Expand Down Expand Up @@ -52,19 +54,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
email = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]
gen_uuid = entry.data[CONF_UUID]
login_api_url = (
entry.data.get(CONF_LOGIN_API_URL)
if entry.data.get(CONF_LOGIN_API_URL) != ""
else None
)
login_api_url = entry.data.get(CONF_LOGIN_API_URL)
brand_id = entry.data.get(CONF_BRAND_ID)
brand = entry.data.get(CONF_BRAND)

agua = aguaiot(
api_url,
customer_code,
email,
password,
gen_uuid,
login_api_url,
api_url=api_url,
customer_code=customer_code,
email=email,
password=password,
unique_id=gen_uuid,
login_api_url=login_api_url,
brand_id=brand_id,
brand=brand,
)

try:
Expand Down
54 changes: 43 additions & 11 deletions custom_components/aguaiot/aguaiot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
API_PATH_DEVICE_BUFFER_READING = "/deviceGetBufferReading"
API_PATH_DEVICE_JOB_STATUS = "/deviceJobStatus/"
API_PATH_DEVICE_WRITING = "/deviceRequestWriting"
API_LOGIN_APPLICATION_VERSION = "1.9.5"
DEFAULT_TIMEOUT_VALUE = 30

HEADER_ACCEPT = "application/json, text/javascript, */*; q=0.01"
Expand All @@ -38,17 +37,19 @@ def __init__(
password,
unique_id,
login_api_url=None,
brand_id=1,
api_login_application_version=API_LOGIN_APPLICATION_VERSION,
brand_id=None,
brand=None,
application_version="1.9.7",
):
self.api_url = api_url.rstrip("/")
self.customer_code = str(customer_code)
self.customer_code = customer_code
self.email = email
self.password = password
self.unique_id = unique_id
self.brand_id = str(brand_id)
self.brand_id = brand_id
self.brand = brand
self.login_api_url = login_api_url
self.api_login_application_version = api_login_application_version
self.application_version = application_version
self.token = None
self.token_expires = None
self.refresh_token = None
Expand All @@ -63,13 +64,17 @@ async def connect(self):
def _headers(self):
"""Correctly set headers for requests to Agua IOT."""

return {
headers = {
"Accept": HEADER_ACCEPT,
"Content-Type": HEADER_CONTENT_TYPE,
"Origin": "file://",
"id_brand": self.brand_id,
"id_brand": self.brand_id if self.brand_id is not None else "1",
"customer_code": self.customer_code,
}
if self.brand is not None:
headers["brand"] = self.brand

return headers

async def register_app_id(self):
"""Register app id with Agua IOT"""
Expand All @@ -87,6 +92,9 @@ async def register_app_id(self):
}

try:
_LOGGER.debug(
"POST Register app - HEADERS: %s DATA: %s", self._headers(), payload
)
async with httpx.AsyncClient() as client:
response = await client.post(
url,
Expand All @@ -95,6 +103,11 @@ async def register_app_id(self):
follow_redirects=False,
timeout=DEFAULT_TIMEOUT_VALUE,
)
_LOGGER.debug(
"RESPONSE Register app - CODE: %s DATA: %s",
response.status_code,
response.text,
)
except httpx.TransportError as e:
raise ConnectionError(f"Connection error to {url}: {e}")

Expand All @@ -121,7 +134,7 @@ async def login(self):

if self.login_api_url is not None:
extra_login_headers = {
"applicationversion": self.api_login_application_version,
"applicationversion": self.application_version,
"url": API_PATH_LOGIN.lstrip("/"),
"userid": "null",
"aguaid": "null",
Expand All @@ -130,6 +143,7 @@ async def login(self):
url = self.login_api_url

try:
_LOGGER.debug("POST Login - HEADERS: %s DATA: ***", headers)
async with httpx.AsyncClient() as client:
response = await client.post(
url,
Expand All @@ -138,6 +152,11 @@ async def login(self):
follow_redirects=False,
timeout=DEFAULT_TIMEOUT_VALUE,
)
_LOGGER.debug(
"RESPONSE Login - CODE: %s DATA: %s",
response.status_code,
response.text,
)
except httpx.TransportError:
raise ConnectionError(f"Connection error to {url}: {e}")

Expand Down Expand Up @@ -168,6 +187,9 @@ async def do_refresh_token(self):
payload = {"refresh_token": self.refresh_token}

try:
_LOGGER.debug(
"POST Refresh token - HEADERS: %s DATA: %s", self._headers(), payload
)
async with httpx.AsyncClient() as client:
response = await client.post(
url,
Expand All @@ -176,6 +198,11 @@ async def do_refresh_token(self):
follow_redirects=False,
timeout=DEFAULT_TIMEOUT_VALUE,
)
_LOGGER.debug(
"RESPONSE Refresh token - CODE: %s DATA: %s",
response.status_code,
response.text,
)
except httpx.TransportError:
raise ConnectionError(f"Connection error to {url}: {e}")

Expand Down Expand Up @@ -208,7 +235,6 @@ async def fetch_devices(self):
url = self.api_url + API_PATH_DEVICE_INFO

payload = {"id_device": dev["id_device"], "id_product": dev["id_product"]}

res2 = await self.handle_webcall("POST", url, payload)
if res2 is False:
raise AguaIOTError("Error while fetching device info")
Expand Down Expand Up @@ -247,6 +273,7 @@ async def handle_webcall(self, method, url, payload):
headers.update(extra_headers)

try:
_LOGGER.debug("%s %s - HEADERS: %s DATA: %s", method, url, headers, payload)
if method == "POST":
async with httpx.AsyncClient() as client:
response = await client.post(
Expand All @@ -265,7 +292,12 @@ async def handle_webcall(self, method, url, payload):
follow_redirects=False,
timeout=DEFAULT_TIMEOUT_VALUE,
)
_LOGGER.debug(f"{response.status_code} - {response.text}")
_LOGGER.debug(
"RESPONSE %s - CODE: %s DATA: %s",
url,
response.status_code,
response.text,
)
except httpx.TransportError:
raise ConnectionError(f"Connection error to {url}: {e}")

Expand Down
26 changes: 15 additions & 11 deletions custom_components/aguaiot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
CONF_LOGIN_API_URL,
CONF_UUID,
CONF_ENDPOINT,
CONF_BRAND_ID,
CONF_BRAND,
DOMAIN,
ENDPOINTS,
)
Expand Down Expand Up @@ -61,24 +63,24 @@ async def async_step_user(self, user_input=None):
endpoint = user_input[CONF_ENDPOINT]
api_url = ENDPOINTS[endpoint][CONF_API_URL]
customer_code = ENDPOINTS[endpoint][CONF_CUSTOMER_CODE]
login_api_url = (
ENDPOINTS[endpoint][CONF_LOGIN_API_URL]
if CONF_LOGIN_API_URL in ENDPOINTS[endpoint]
else None
)
login_api_url = ENDPOINTS[endpoint].get(CONF_LOGIN_API_URL)
brand_id = ENDPOINTS[endpoint].get(CONF_BRAND_ID)
brand = ENDPOINTS[endpoint].get(CONF_BRAND)

if self._entry_in_configuration_exists(user_input):
return self.async_abort(reason="device_already_configured")

try:
gen_uuid = str(uuid.uuid1())
agua = aguaiot(
api_url,
customer_code,
email,
password,
gen_uuid,
login_api_url,
api_url=api_url,
customer_code=customer_code,
email=email,
password=password,
unique_id=gen_uuid,
login_api_url=login_api_url,
brand_id=brand_id,
brand=brand,
)
await agua.connect()
except UnauthorizedError as e:
Expand All @@ -101,6 +103,8 @@ async def async_step_user(self, user_input=None):
CONF_API_URL: api_url,
CONF_CUSTOMER_CODE: customer_code,
CONF_LOGIN_API_URL: login_api_url,
CONF_BRAND_ID: brand_id,
CONF_BRAND: brand,
},
)
else:
Expand Down
9 changes: 9 additions & 0 deletions custom_components/aguaiot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class AguaIOTCanalizationEntityDescription(ClimateEntityDescription):
CONF_LOGIN_API_URL = "login_api_url"
CONF_UUID = "uuid"
CONF_ENDPOINT = "endpoint"
CONF_BRAND_ID = "brand_id"
CONF_BRAND = "brand"

DEVICE_VARIANTS = ["water", "air", "air2", "air_palm"]
MODE_WOOD = "Wood"
Expand Down Expand Up @@ -178,6 +180,13 @@ class AguaIOTCanalizationEntityDescription(ClimateEntityDescription):
CONF_API_URL: "https://piazzetta.agua-iot.com",
CONF_LOGIN_API_URL: "https://piazzetta-iot.app2cloud.it/api/bridge/endpoint/",
},
"MySuperior": {
CONF_CUSTOMER_CODE: "458632",
CONF_API_URL: "https://piazzetta.agua-iot.com",
CONF_LOGIN_API_URL: "https://piazzetta-iot.app2cloud.it/api/bridge/endpoint/",
CONF_BRAND_ID: "2",
CONF_BRAND: "superior",
},
"Nina": {
CONF_CUSTOMER_CODE: "999999",
CONF_API_URL: "https://micronova.agua-iot.com",
Expand Down

0 comments on commit 08871df

Please sign in to comment.