Skip to content

Commit d50656a

Browse files
myztillxnkgilley
authored andcommitted
Modified to allow for setting sensors based on IDs
1 parent b7263b6 commit d50656a

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

pyecobee/__init__.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
ECOBEE_OPTIONS_NOTIFICATIONS,
2929
ECOBEE_WEB_CLIENT_ID
3030
)
31-
from .errors import ExpiredTokenError, InvalidTokenError
31+
from .errors import ExpiredTokenError, InvalidSensorError, InvalidTokenError
3232
from .util import config_from_file, convert_to_bool
3333

3434

@@ -784,24 +784,44 @@ def set_aux_cutover_threshold(self, index: int, threshold: int) -> None:
784784
except (ExpiredTokenError, InvalidTokenError) as err:
785785
raise err
786786

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+
789795
programs: dict = self.thermostats[index]["program"]
790796
# Remove currentClimateRef key.
791797
programs.pop("currentClimateRef", None)
792798

793799
for i, climate in enumerate(programs["climates"]):
794800
if climate["name"] == climate_name:
795801
climate_index = i
796-
"""Update climate sensors with sensor_names list."""
802+
sensors = self.get_remote_sensors(index)
797803
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+
805825
try:
806826
programs["climates"][climate_index]["sensors"] = sensor_list
807827
except UnboundLocalError:

pyecobee/errors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ class ExpiredTokenError(EcobeeError):
1515

1616
class InvalidTokenError(EcobeeError):
1717
"""Raised when ecobee API returns a code indicating invalid credentials."""
18+
19+
class InvalidSensorError(EcobeeError):
20+
"""Raised when remote sensor not present on thermostat."""

0 commit comments

Comments
 (0)