diff --git a/custom_components/watts_vision/binary_sensor.py b/custom_components/watts_vision/binary_sensor.py index a9129a7..d686c4c 100644 --- a/custom_components/watts_vision/binary_sensor.py +++ b/custom_components/watts_vision/binary_sensor.py @@ -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"], ) ) diff --git a/custom_components/watts_vision/climate.py b/custom_components/watts_vision/climate.py index 053192b..4e7c579 100644 --- a/custom_components/watts_vision/climate.py +++ b/custom_components/watts_vision/climate.py @@ -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"], ) ) @@ -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 ( @@ -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" @@ -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 @@ -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" diff --git a/custom_components/watts_vision/manifest.json b/custom_components/watts_vision/manifest.json index d6197f4..b2643ee 100644 --- a/custom_components/watts_vision/manifest.json +++ b/custom_components/watts_vision/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "name": "Watts Vision", "requirements": [], - "version": "0.2.4" + "version": "0.2.5" } diff --git a/custom_components/watts_vision/sensor.py b/custom_components/watts_vision/sensor.py index 93cb930..6d94e14 100644 --- a/custom_components/watts_vision/sensor.py +++ b/custom_components/watts_vision/sensor.py @@ -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"] ) ) diff --git a/custom_components/watts_vision/watts_api.py b/custom_components/watts_vision/watts_api.py index edd66aa..c81eb21 100644 --- a/custom_components/watts_vision/watts_api.py +++ b/custom_components/watts_vision/watts_api.py @@ -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 @@ -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: @@ -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""" @@ -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 @@ -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 @@ -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}"} @@ -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