Skip to content

Commit

Permalink
fixes for changes in API
Browse files Browse the repository at this point in the history
  • Loading branch information
pwesters committed Aug 15, 2022
1 parent a40e550 commit 89a7efa
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 69 deletions.
8 changes: 4 additions & 4 deletions custom_components/watts_vision/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ async def async_setup_entry(

if smartHomes is not None:
for y in range(len(smartHomes)):
if smartHomes[str(y)]["devices"] is not None:
for x in range(len(smartHomes[str(y)]["devices"])):
if smartHomes[y]["devices"] is not None:
for x in range(len(smartHomes[y]["devices"])):
sensors.append(
WattsVisionHeatingBinarySensor(
wattsClient,
smartHomes[str(y)]["smarthome_id"],
smartHomes[str(y)]["devices"][str(x)]["id"],
smartHomes[y]["smarthome_id"],
smartHomes[y]["devices"][x]["id"],
)
)

Expand Down
48 changes: 24 additions & 24 deletions custom_components/watts_vision/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ async def async_setup_entry(
devices = []

for y in range(len(smartHomes)):
for x in range(len(smartHomes[str(y)]["devices"])):
for x in range(len(smartHomes[y]["devices"])):
devices.append(
WattsThermostat(
wattsClient,
smartHomes[str(y)]["smarthome_id"],
smartHomes[str(y)]["devices"][str(x)]["id"],
smartHomes[str(y)]["devices"][str(x)]["id_device"],
smartHomes[y]["smarthome_id"],
smartHomes[y]["devices"][x]["id"],
smartHomes[y]["devices"][x]["id_device"],
)
)

Expand Down Expand Up @@ -236,16 +236,16 @@ async def async_set_hvac_mode(self, hvac_mode):

# reloading the devices may take some time, meanwhile set the new values manually
for y in range(len(self.client._smartHomeData)):
if self.client._smartHomeData[str(y)]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[str(y)]["devices"])):
if self.client._smartHomeData[y]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[y]["devices"])):
if (
self.client._smartHomeData[str(y)]["devices"][str(x)]["id"]
self.client._smartHomeData[y]["devices"][x]["id"]
== self.id
):
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"gv_mode"
] = self._attr_extra_state_attributes["previous_gv_mode"]
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"consigne_manuel"
] = value
if (
Expand Down Expand Up @@ -291,16 +291,16 @@ async def async_set_hvac_mode(self, hvac_mode):

# reloading the devices may take some time, meanwhile set the new values manually
for y in range(len(self.client._smartHomeData)):
if self.client._smartHomeData[str(y)]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[str(y)]["devices"])):
if self.client._smartHomeData[y]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[y]["devices"])):
if (
self.client._smartHomeData[str(y)]["devices"][str(x)]["id"]
self.client._smartHomeData[y]["devices"][x]["id"]
== self.id
):
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"gv_mode"
] = PRESET_MODE_REVERSE_MAP[PRESET_OFF]
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"consigne_manuel"
] = "0"

Expand Down Expand Up @@ -332,16 +332,16 @@ async def async_set_preset_mode(self, preset_mode):

# reloading the devices may take some time, meanwhile set the new values manually
for y in range(len(self.client._smartHomeData)):
if self.client._smartHomeData[str(y)]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[str(y)]["devices"])):
if self.client._smartHomeData[y]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[y]["devices"])):
if (
self.client._smartHomeData[str(y)]["devices"][str(x)]["id"]
self.client._smartHomeData[y]["devices"][x]["id"]
== self.id
):
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"gv_mode"
] = PRESET_MODE_REVERSE_MAP[preset_mode]
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"consigne_manuel"
] = value

Expand All @@ -360,16 +360,16 @@ async def async_set_preset_mode(self, preset_mode):

# reloading the devices may take some time, meanwhile set the new values manually
for y in range(len(self.client._smartHomeData)):
if self.client._smartHomeData[str(y)]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[str(y)]["devices"])):
if self.client._smartHomeData[y]["smarthome_id"] == self.smartHome:
for x in range(len(self.client._smartHomeData[y]["devices"])):
if (
self.client._smartHomeData[str(y)]["devices"][str(x)]["id"]
self.client._smartHomeData[y]["devices"][x]["id"]
== self.id
):
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"gv_mode"
] = PRESET_MODE_REVERSE_MAP[PRESET_OFF]
self.client._smartHomeData[str(y)]["devices"][str(x)][
self.client._smartHomeData[y]["devices"][x][
"consigne_manuel"
] = "0"

Expand Down
2 changes: 1 addition & 1 deletion custom_components/watts_vision/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"name": "Watts Vision",
"requirements": [],
"version": "0.2.4"
"version": "0.2.5"
}
18 changes: 9 additions & 9 deletions custom_components/watts_vision/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,33 @@ async def async_setup_entry(

if smartHomes is not None:
for y in range(len(smartHomes)):
if smartHomes[str(y)]["devices"] is not None:
for x in range(len(smartHomes[str(y)]["devices"])):
if smartHomes[y]["devices"] is not None:
for x in range(len(smartHomes[y]["devices"])):
sensors.append(
WattsVisionThermostatSensor(
wattsClient,
smartHomes[str(y)]["smarthome_id"],
smartHomes[str(y)]["devices"][str(x)]["id"],
smartHomes[y]["smarthome_id"],
smartHomes[y]["devices"][x]["id"],
)
)
sensors.append(
WattsVisionTemperatureSensor(
wattsClient,
smartHomes[str(y)]["smarthome_id"],
smartHomes[str(y)]["devices"][str(x)]["id"],
smartHomes[y]["smarthome_id"],
smartHomes[y]["devices"][x]["id"],
)
)
sensors.append(
WattsVisionSetTemperatureSensor(
wattsClient,
smartHomes[str(y)]["smarthome_id"],
smartHomes[str(y)]["devices"][str(x)]["id"],
smartHomes[y]["smarthome_id"],
smartHomes[y]["devices"][x]["id"],
)
)
sensors.append(
WattsVisionLastCommunicationSensor(
wattsClient,
smartHomes[str(y)]["smarthome_id"]
smartHomes[y]["smarthome_id"]
)
)

Expand Down
125 changes: 94 additions & 31 deletions custom_components/watts_vision/watts_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def loadData(self):
"""load devices for each smart home"""
if self._smartHomeData is not None:
for y in range(len(self._smartHomeData)):
devices = self.loadDevices(self._smartHomeData[str(y)]["smarthome_id"])
self._smartHomeData[str(y)]["devices"] = devices
devices = self.loadDevices(self._smartHomeData[y]["smarthome_id"])
self._smartHomeData[y]["devices"] = devices

return True

Expand All @@ -85,9 +85,9 @@ def loadSmartHomes(self, firstTry: bool = True):

if user_data_result.status_code == 200:
if (
user_data_result.json()["code"]["code"] == "1"
and user_data_result.json()["code"]["key"] == "OK"
and user_data_result.json()["code"]["value"] == "OK"
user_data_result.json()["code"]["code"] == "8"
and user_data_result.json()["code"]["key"] == "OK_SET"
and user_data_result.json()["code"]["value"] == "Insert / update success"
):
return user_data_result.json()["data"]["smarthomes"]
else:
Expand All @@ -106,12 +106,25 @@ def loadSmartHomes(self, firstTry: bool = True):
)
return None
else:
_LOGGER.error(
"Something went wrong fetching user data: {}".format(
user_data_result.status_code
if user_data_result.status_code == 401:
if firstTry:
# Token may be expired, try to fetch new token
self.getLoginToken()
return self.loadSmartHomes(firstTry=False)
else:
_LOGGER.error(
"Something went wrong fetching user data: {}".format(
user_data_result.status_code
)
)
return None
else:
_LOGGER.error(
"Something went wrong fetching user data: {}".format(
user_data_result.status_code
)
)
)
return None
return None

def loadDevices(self, smarthome: str, firstTry: bool = True):
"""Load devices for smart home"""
Expand Down Expand Up @@ -148,19 +161,35 @@ def loadDevices(self, smarthome: str, firstTry: bool = True):
)
return None
else:
_LOGGER.error(
"Something went wrong fetching devices: {}".format(
devices_result.status_code
if devices_result.status_code == 401:
if firstTry:
# Token may be expired, try to fetch new token
self.getLoginToken()
return self.loadDevices(smarthome, firstTry=False)
else:
_LOGGER.error(
"Something went wrong fetching user data. Code: {}, Key: {}, Value: {}, Data: {}".format(
devices_result.json()["code"]["code"],
devices_result.json()["code"]["key"],
devices_result.json()["code"]["value"],
devices_result.json()["data"],
)
)
return None
else:
_LOGGER.error(
"Something went wrong fetching devices: {}".format(
devices_result.status_code
)
)
)
return None
return None

def reloadDevices(self):
"""load devices for each smart home"""
if self._smartHomeData is not None:
for y in range(len(self._smartHomeData)):
devices = self.loadDevices(self._smartHomeData[str(y)]["smarthome_id"])
self._smartHomeData[str(y)]["devices"] = devices
devices = self.loadDevices(self._smartHomeData[y]["smarthome_id"])
self._smartHomeData[y]["devices"] = devices

return True

Expand All @@ -171,10 +200,10 @@ def getSmartHomes(self):
def getDevice(self, smarthome: str, deviceId: str):
"""Get specific device"""
for y in range(len(self._smartHomeData)):
if self._smartHomeData[str(y)]["smarthome_id"] == smarthome:
for x in range(len(self._smartHomeData[str(y)]["devices"])):
if self._smartHomeData[str(y)]["devices"][str(x)]["id"] == deviceId:
return self._smartHomeData[str(y)]["devices"][str(x)]
if self._smartHomeData[y]["smarthome_id"] == smarthome:
for x in range(len(self._smartHomeData[y]["devices"])):
if self._smartHomeData[y]["devices"][x]["id"] == deviceId:
return self._smartHomeData[y]["devices"][x]

return None

Expand Down Expand Up @@ -301,12 +330,30 @@ def pushTemperature(
)
return False
else:
_LOGGER.error(
"Something went wrong updating the device: {}".format(
push_result.status_code
if push_result.status_code == 401:
if firstTry:
# Token may be expired, try to fetch new token
self.getLoginToken()
return self.pushTemperature(
smarthome, deviceID, value, gvMode, firstTry=False
)
else:
_LOGGER.error(
"Something went wrong updating the device. Code: {}, Key: {}, Value: {}, Data: {}".format(
push_result.json()["code"]["code"],
push_result.json()["code"]["key"],
push_result.json()["code"]["value"],
push_result.json()["data"],
)
)
return False
else:
_LOGGER.error(
"Something went wrong updating the device: {}".format(
push_result.status_code
)
)
)
return False
return False

def getLastCommunication(self, smarthome: str, firstTry: bool = True):
headers = {"Authorization": f"Bearer {self._token}"}
Expand Down Expand Up @@ -345,9 +392,25 @@ def getLastCommunication(self, smarthome: str, firstTry: bool = True):
)
return None
else:
_LOGGER.error(
"Something went wrong fetching devices: {}".format(
last_connection_result.status_code
if last_connection_result.status_code == 401:
if firstTry:
# Token may be expired, try to fetch new token
self.getLoginToken()
return self.getLastCommunication(smarthome, firstTry=False)
else:
_LOGGER.error(
"Something went wrong fetching user data. Code: {}, Key: {}, Value: {}, Data: {}".format(
last_connection_result.json()["code"]["code"],
last_connection_result.json()["code"]["key"],
last_connection_result.json()["code"]["value"],
last_connection_result.json()["data"],
)
)
return None
else:
_LOGGER.error(
"Something went wrong fetching devices: {}".format(
last_connection_result.status_code
)
)
)
return None
return None

0 comments on commit 89a7efa

Please sign in to comment.