Skip to content

Modified to allow for setting sensors based on IDs #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."""