Skip to content

Commit

Permalink
Modified to allow for setting sensors based on IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
myztillx authored and nkgilley committed Oct 2, 2024
1 parent b7263b6 commit d50656a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
42 changes: 31 additions & 11 deletions pyecobee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
ECOBEE_OPTIONS_NOTIFICATIONS,
ECOBEE_WEB_CLIENT_ID
)
from .errors import ExpiredTokenError, InvalidTokenError
from .errors import ExpiredTokenError, InvalidSensorError, InvalidTokenError
from .util import config_from_file, convert_to_bool


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

def update_climate_sensors(self, index: int, climate_name: str, sensor_names: list) -> None:
"""Get current climate program."""
def update_climate_sensors(self, index: int, climate_name: str, sensor_names: Optional[list]=None, sensor_ids: Optional[list]=None) -> None:
"""Get current climate program. Must provide either `sensor_names` or `ids`."""
# Ensure only either `sensor_names` or `ids` was provided.
if sensor_names is None and sensor_ids is None:
raise ValueError("Need to provide either `sensor_names` or `ids`.")
if sensor_names and sensor_ids:
raise ValueError("Either `sensor_names` or `ids` should be provided, not both.")

programs: dict = self.thermostats[index]["program"]
# Remove currentClimateRef key.
programs.pop("currentClimateRef", None)

for i, climate in enumerate(programs["climates"]):
if climate["name"] == climate_name:
climate_index = i
"""Update climate sensors with sensor_names list."""
sensors = self.get_remote_sensors(index)
sensor_list = []
for name in sensor_names:
"""Find the sensor id from the name."""
sensors = self.get_remote_sensors(index)
for sensor in sensors:
if sensor["name"] == name:
sensor_list.append(
{"id": "{}:1".format(sensor["id"]), "name": name})

if sensor_ids:
"""Update climate sensors with sensor_ids list."""
for id in sensor_ids:
for sensor in sensors:
if sensor["id"] == id:
sensor_list.append(
{"id": "{}:1".format(id), "name": sensor["name"]})

if sensor_names:
"""Update climate sensors with sensor_names list."""
for name in sensor_names:
"""Find the sensor id from the name."""
for sensor in sensors:
if sensor["name"] == name:
sensor_list.append(
{"id": "{}:1".format(sensor["id"]), "name": name})

if len(sensor_list) == 0:
raise InvalidSensorError("no sensor matching provided ids or names on thermostat")

try:
programs["climates"][climate_index]["sensors"] = sensor_list
except UnboundLocalError:
Expand Down
3 changes: 3 additions & 0 deletions pyecobee/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ class ExpiredTokenError(EcobeeError):

class InvalidTokenError(EcobeeError):
"""Raised when ecobee API returns a code indicating invalid credentials."""

class InvalidSensorError(EcobeeError):
"""Raised when remote sensor not present on thermostat."""

0 comments on commit d50656a

Please sign in to comment.