|
28 | 28 | ECOBEE_OPTIONS_NOTIFICATIONS, |
29 | 29 | ECOBEE_WEB_CLIENT_ID |
30 | 30 | ) |
31 | | -from .errors import ExpiredTokenError, InvalidTokenError |
| 31 | +from .errors import ExpiredTokenError, InvalidSensorError, InvalidTokenError |
32 | 32 | from .util import config_from_file, convert_to_bool |
33 | 33 |
|
34 | 34 |
|
@@ -784,24 +784,44 @@ def set_aux_cutover_threshold(self, index: int, threshold: int) -> None: |
784 | 784 | except (ExpiredTokenError, InvalidTokenError) as err: |
785 | 785 | raise err |
786 | 786 |
|
787 | | - def update_climate_sensors(self, index: int, climate_name: str, sensor_names: list) -> None: |
788 | | - """Get current climate program.""" |
| 787 | + def update_climate_sensors(self, index: int, climate_name: str, sensor_names: Optional[list]=None, sensor_ids: Optional[list]=None) -> None: |
| 788 | + """Get current climate program. Must provide either `sensor_names` or `ids`.""" |
| 789 | + # Ensure only either `sensor_names` or `ids` was provided. |
| 790 | + if sensor_names is None and sensor_ids is None: |
| 791 | + raise ValueError("Need to provide either `sensor_names` or `ids`.") |
| 792 | + if sensor_names and sensor_ids: |
| 793 | + raise ValueError("Either `sensor_names` or `ids` should be provided, not both.") |
| 794 | + |
789 | 795 | programs: dict = self.thermostats[index]["program"] |
790 | 796 | # Remove currentClimateRef key. |
791 | 797 | programs.pop("currentClimateRef", None) |
792 | 798 |
|
793 | 799 | for i, climate in enumerate(programs["climates"]): |
794 | 800 | if climate["name"] == climate_name: |
795 | 801 | climate_index = i |
796 | | - """Update climate sensors with sensor_names list.""" |
| 802 | + sensors = self.get_remote_sensors(index) |
797 | 803 | sensor_list = [] |
798 | | - for name in sensor_names: |
799 | | - """Find the sensor id from the name.""" |
800 | | - sensors = self.get_remote_sensors(index) |
801 | | - for sensor in sensors: |
802 | | - if sensor["name"] == name: |
803 | | - sensor_list.append( |
804 | | - {"id": "{}:1".format(sensor["id"]), "name": name}) |
| 804 | + |
| 805 | + if sensor_ids: |
| 806 | + """Update climate sensors with sensor_ids list.""" |
| 807 | + for id in sensor_ids: |
| 808 | + for sensor in sensors: |
| 809 | + if sensor["id"] == id: |
| 810 | + sensor_list.append( |
| 811 | + {"id": "{}:1".format(id), "name": sensor["name"]}) |
| 812 | + |
| 813 | + if sensor_names: |
| 814 | + """Update climate sensors with sensor_names list.""" |
| 815 | + for name in sensor_names: |
| 816 | + """Find the sensor id from the name.""" |
| 817 | + for sensor in sensors: |
| 818 | + if sensor["name"] == name: |
| 819 | + sensor_list.append( |
| 820 | + {"id": "{}:1".format(sensor["id"]), "name": name}) |
| 821 | + |
| 822 | + if len(sensor_list) == 0: |
| 823 | + raise InvalidSensorError("no sensor matching provided ids or names on thermostat") |
| 824 | + |
805 | 825 | try: |
806 | 826 | programs["climates"][climate_index]["sensors"] = sensor_list |
807 | 827 | except UnboundLocalError: |
|
0 commit comments