Skip to content

Commit

Permalink
fix(api): Get and Set schedule by day type (#224)
Browse files Browse the repository at this point in the history
* fix(api): Get and Set schedule by day type

* doc
  • Loading branch information
germainlefebvre4 authored Jan 1, 2024
1 parent bbe7a60 commit acb39ea
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 16 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ It shows you how to use the library and expose some structured responses. A more
|----------------|----------------------|
| `3.7` | `2.0.0` > `3.6.x` |
| `3.8` | `3.7.0` > `latest` |
| `3.9` | `3.7.0` > `latest` |
| `3.10` | `3.7.0` > `latest` |

## Development

### Setup

```bash
sudo apt update
sudo apt install python3.7 python3.7-pip
sudo pip install pipenv
pipenv update --dev
# poetry install --with dev
sudo apt install python3.9 python3.9-pip
sudo pip install poetry
poetry install --with dev
# pipenv update --dev
```

### Run application
Expand All @@ -106,9 +108,9 @@ Follow the indications in sectio [Usage](#usage).
### Run tests

```bash
pipenv update --dev
# poetry install --with dev
poetry install --with dev
# pipenv update --dev
pipenv run pytest tests/
# poetry run pytest tests/
poetry run pytest tests/
# pipenv run pytest tests/
```
2 changes: 2 additions & 0 deletions check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
# print(tado.get_consumption_overview("2023-09", "FRA", True))
# print(tado.get_enery_settings())
# print(tado.get_energy_insights("2023-09-01", "2023-09-30", "FRA", True))
# print(tado.get_schedule_blocks(1, 1))
# print(tado.get_schedule_block_by_day_type(1, 1, "MONDAY_TO_FRIDAY"))
2 changes: 2 additions & 0 deletions docs/api/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ import tado.api
api = tado.api('Username', 'Password')
```

For API Reference see [this page](/api/reference)
16 changes: 9 additions & 7 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This library is tested with following python versions:

- `3.7`
- `3.8`
- `3.9`
- `3.10`

## Setup

Expand All @@ -23,16 +25,16 @@ Update your system and install a python version (at least the minimum required)

```bash
sudo apt update
sudo apt install python3.7 python3.7-pip
sudo pip install pipenv
# sudo pip install poetry
sudo apt install python3.9 python3.9-pip
sudo pip install poetry
# sudo pip install pipenv
```

Initialize your `pipenv` (or `poetry`) setup and install all the development libraries.

```bash
pipenv update --dev
# poetry install
poetry install
# pipenv update --dev
```

## Improve the library
Expand All @@ -55,6 +57,6 @@ The tests are written in the following files:
Run the tests inside `pipenv` or `poetry`.

```bash
pipenv run pytest -sv tests/
# poetry run pytest -sv tests/
poetry run pytest -sv tests/
# pipenv run pytest -sv tests/
```
47 changes: 47 additions & 0 deletions libtado/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,52 @@ def energy_savings(tado, month_date, country, ngsw_bypass=True):
click.echo('')


@main.command()
@click.pass_obj
@click.option('--zone', '-z', required=True, type=int, help='Zone ID')
@click.option('--schedule', '-s', required=True, type=int, help='Schedule ID')
def schedule_blocks(tado, zone, schedule):
"""Get the schedule blocks of a zone."""
schedule_blocks = tado.get_schedule_blocks(zone, schedule)

click.echo('Schedule blocks for zone %s and schedule %s' % (zone, schedule))
click.echo('')

click.echo('Schedule blocks:')
for sb in schedule_blocks:
click.echo(' Day type: %s' % (sb['dayType']))
click.echo(' Start: %s' % (sb['start']))
click.echo(' End: %s' % (sb['end']))
click.echo(' Setting:')
click.echo(' Power: %s' % (sb['setting']['power']))
click.echo(' Temperature (celsius): %s' % (sb['setting']['temperature']['celsius']))
click.echo('')


@main.command()
@click.pass_obj
@click.option('--zone', '-z', required=True, type=int, help='Zone ID')
@click.option('--schedule', '-s', required=True, type=int, help='Schedule ID')
@click.option('--day-type', '-d', required=True, type=str, help='Day type')
def schedule_block_day_type(tado, zone, schedule, day_type):
"""Get the day type schedule block of a zone."""
schedule_block_by_day_type = tado.get_schedule_block_by_day_type(zone, schedule, day_type)

click.echo('Schedule blocks for zone %s and schedule %s' % (zone, schedule))
click.echo('')

click.echo('Day type: %s' % (schedule_block_by_day_type[0]['dayType']))
click.echo('')

click.echo('Schedule day type blocks:')
for sb in schedule_block_by_day_type:
click.echo(' Start: %s' % (sb['start']))
click.echo(' End: %s' % (sb['end']))
click.echo(' Setting:')
click.echo(' Power: %s' % (sb['setting']['power']))
click.echo(' Temperature (celsius): %s' % (sb['setting']['temperature']['celsius']))
click.echo('')


if __name__ == "__main__":
main()
122 changes: 121 additions & 1 deletion libtado/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,76 @@ def get_schedule_blocks(self, zone, schedule):
Gets the blocks for the current schedule on a zone
Parameters:
zone (int): The zone ID.
zone (int): The zone ID.
schedule (int): The schedule ID to fetch.
Returns:
(list): The blocks for the requested schedule.
Example:
```json
[
{
"dayType": "MONDAY_TO_FRIDAY",
"start": "00:00",
"end": "06:30",
"geolocationOverride": false,
"setting": {
"type": "HEATING",
"power": "ON",
"temperature": {
"celsius": 21.2,
"fahrenheit": 70.16
}
}
},
{
"dayType": "MONDAY_TO_FRIDAY",
"start": "06:30",
"end": "00:00",
"geolocationOverride": false,
"setting": {
"type": "HEATING",
"power": "ON",
"temperature": {
"celsius": 21.0,
"fahrenheit": 69.8
}
}
},
{
"dayType": "SATURDAY",
"start": "00:00",
"end": "08:00",
"geolocationOverride": false,
"setting": {
"type": "HEATING",
"power": "ON",
"temperature": {
"celsius": 21.0,
"fahrenheit": 69.8
}
}
},
[...]
{
"dayType": "SUNDAY",
"start": "08:00",
"end": "00:00",
"geolocationOverride": false,
"setting": {
"type": "HEATING",
"power": "ON",
"temperature": {
"celsius": 21.0,
"fahrenheit": 69.8
}
}
}
]
```
"""

return self._api_call('homes/%i/zones/%i/schedule/timetables/%i/blocks' % (self.id, zone, schedule))
Expand All @@ -613,6 +678,61 @@ def set_schedule_blocks(self, zone, schedule, blocks):
return self._api_call('homes/%i/zones/%i/schedule/timetables/%i/blocks' % (self.id, zone, schedule), payload, method='PUT')


def get_schedule_block_by_day_type(self, zone, schedule, day_type):
"""
Gets the blocks for the current schedule on a zone
Parameters:
zone (int): The zone ID.
schedule (int): The schedule ID to fetch.
day_type (str): The day_type to fetch. e.g. MONDAY_TO_FRIDAY, "MONDAY", "TUESDAY" etc
Returns:
(list): The blocks for the requested day type schedule.
Example:
```json
[
{
"dayType": "MONDAY_TO_FRIDAY",
"start": "00:00",
"end": "06:30",
"geolocationOverride": false,
"setting": {
"type": "HEATING",
"power": "ON",
"temperature": {
"celsius": 21.2,
"fahrenheit": 70.16
}
}
},
[...]
]
```
"""

return self._api_call('homes/%i/zones/%i/schedule/timetables/%i/blocks/%s' % (self.id, zone, schedule, day_type))


def set_schedule_block_by_day_type(self, zone, schedule, day_type, blocks):
"""
Sets the block for the current schedule on a zone
Parameters:
zone (int): The zone ID.
schedule (int): The schedule ID.
day_type (str): The day_type to fetch. e.g. MONDAY_TO_FRIDAY, "MONDAY", "TUESDAY" etc
blocks (list): The new blocks.
Returns:
(list): The new configuration.
"""

payload = blocks
return self._api_call('homes/%i/zones/%i/schedule/timetables/%i/blocks/%s' % (self.id, zone, schedule, day_type), payload, method='PUT')


def get_state(self, zone):
"""
Get the current state of a zone including its desired and current temperature. Check out the example output for more.
Expand Down

0 comments on commit acb39ea

Please sign in to comment.