Skip to content
This repository was archived by the owner on Dec 23, 2025. It is now read-only.

Commit 3fca1b7

Browse files
author
Richard Pearce
committed
Package correctly for pip
1 parent 357bbf9 commit 3fca1b7

10 files changed

Lines changed: 259 additions & 217 deletions

File tree

.gitignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
cache/
2-
examples/cache
1+
examples/cache
2+
timeguard-supportmaster/cache
3+
cache
4+
build
5+
dist
6+
tg
7+
__pycache__
8+
timeguard_supplymaster.egg-info/

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,28 @@ The software is provided “as is”, without warranty of any kind, express or i
2121
1. Clone the repo
2222

2323
```bash
24-
git clone git@github.com:rjpearce/timeguard-supplymaster-python.git timeguard
24+
pip install timeguard-supplymaster
2525
```
2626

27-
1. Create ~/.timeguard.yaml
27+
2. Create ~/.timeguard.yaml
2828

2929
```bash
3030
---
31-
username: tg-username
32-
password: tg-password
31+
username: your-username
32+
password: your-password
3333
use_cache: False
3434
...
3535
```
36+
3637
## Usage
3738

3839
List all devices, programs and time slots.
3940

4041
```python
41-
tg = TimeGuard()
42-
tg.refresh_devices()
42+
from timeguard_supplymaster import Client
43+
44+
client = Client()
45+
client.refresh_devices()
4346
for device in tg.devices:
4447
print(device.name, device.id)
4548
for program in device.programs:

examples/set_program.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11

22
import sys
3-
sys.path.append("..")
4-
from timeguard import TimeGuard
3+
from timeguard_supplymaster import Client
54

6-
tg = TimeGuard()
7-
tg.refresh_devices()
8-
for device in tg.devices:
5+
client = Client()
6+
client.refresh_devices()
7+
for device in client.devices:
98
print(device.name, device.id)
109
for program in device.programs:
1110
program.reset_all_time_slots()
1211
if program.id == '0':
13-
program.time_slots[0].set_time(tg.EVERYDAY, '02:30', '03:00')
14-
program.time_slots[1].set_time(tg.EVERYDAY, '03:00', '04:00')
15-
program.time_slots[2].set_time(tg.EVERYDAY, '13:30', '14:00')
16-
program.time_slots[3].set_time(tg.EVERYDAY, '14:30', '15:00')
12+
program.time_slots[0].set_time(client.EVERYDAY, '02:30', '03:00')
13+
program.time_slots[1].set_time(client.EVERYDAY, '03:00', '04:00')
14+
program.time_slots[2].set_time(client.EVERYDAY, '13:30', '14:00')
15+
program.time_slots[3].set_time(client.EVERYDAY, '14:30', '15:00')
1716
program.save()
1817
print(program.id, program.name)
1918
print(program.time_slots)

setup.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import setuptools
2+
3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="timeguard-supplymaster",
8+
version="0.0.14",
9+
author="Richard Pearce",
10+
author_email="rjpearce23@gmail.com",
11+
description="Implementation of the API used by the Timeguard's Supplymaster application",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/rjpearce/timeguard-supplymaster-python",
15+
packages=setuptools.find_packages(),
16+
classifiers=[
17+
"Development Status :: 3 - Alpha",
18+
"Programming Language :: Python :: 3",
19+
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
20+
"Operating System :: OS Independent",
21+
"Topic :: Home Automation",
22+
],
23+
python_requires='>=3.6',
24+
)

timeguard.py

Lines changed: 0 additions & 200 deletions
This file was deleted.

timeguard_supplymaster/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .client import Client
2+
from .device import Device
3+
from .program import Program
4+
from .timeslot import TimeSlot

timeguard_supplymaster/client.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import sys
2+
from pprint import pprint
3+
import os
4+
import requests
5+
from requests.auth import HTTPDigestAuth
6+
import yaml
7+
import json
8+
from pathlib import Path
9+
from .device import Device
10+
11+
class Client:
12+
""" Implements the TimeGuard Client """
13+
config = {}
14+
devices = []
15+
connect_timeout = 10
16+
read_timeout = 30
17+
cache_folder = ''
18+
base_url = ''
19+
token = ''
20+
user_id = ''
21+
HEADERS = {'User-Agent': 'okhttp/3.3.1'}
22+
BASE_URL = 'https://www.cloudwarm.net/TimeGuard/api/Android/v_1'
23+
EVERYDAY = { 'Mon': True, 'Tue': True, 'Wed': True, 'Thu': True, 'Fri': True, 'Sat': True, 'Sun': True}
24+
25+
def __init__(self, config_path=f'{str(Path.home())}/.timeguard.yaml', cache_folder=f'{os.getcwd()}/cache'):
26+
self.config = self.read_config(config_path)
27+
self.validate_config()
28+
self.cache_folder = cache_folder
29+
os.makedirs(cache_folder, exist_ok=True)
30+
31+
def read_config(self, config_path):
32+
""" Read the configuration file """
33+
with open(config_path, 'r') as config_file:
34+
return yaml.safe_load(config_file.read())
35+
36+
def validate_config(self):
37+
""" Validate the config """
38+
if 'username' not in self.config:
39+
raise Exception('username missing from config')
40+
if 'password' not in self.config:
41+
raise Exception('password missing from config')
42+
if 'use_cache' not in self.config:
43+
self.config['use_cache'] = False
44+
45+
def api_request(self, type, uri, data=None):
46+
response = None
47+
cache_file = f'{uri.replace(self.token,"").replace("/","_")}.json'
48+
if self.config['use_cache']:
49+
with open(f'{self.cache_folder}/{cache_file}') as data_file:
50+
return json.load(data_file)
51+
timeout = (self.connect_timeout, self.read_timeout)
52+
if type == 'PUT':
53+
print('PUT', f'{self.BASE_URL}/{uri}', data, self.HEADERS, timeout)
54+
response = requests.put(f'{self.BASE_URL}/{uri}', data=data, headers=self.HEADERS, timeout=timeout)
55+
elif type == 'GET':
56+
print('GET', f'{self.BASE_URL}/{uri}', self.HEADERS, timeout)
57+
response = requests.get(f'{self.BASE_URL}/{uri}', headers=self.HEADERS, timeout=timeout)
58+
if response.status_code == 200:
59+
print(response.status_code)
60+
response_json = response.json()
61+
pprint(response_json)
62+
status_file = open(f'{self.cache_folder}/{cache_file}', 'w')
63+
status_file.write(json.dumps(response_json))
64+
status_file.close()
65+
return response_json
66+
else:
67+
raise Exception(f'Login request failed: {response.status_code}')
68+
69+
def refresh_devices(self):
70+
data = f'username={self.config["username"]}&password={self.config["password"]}'
71+
response_json = self.api_request('PUT', 'users/login', data)
72+
self.token = response_json['message']['user']['token']
73+
self.user_id = response_json['message']['user']['id']
74+
for device in response_json['message']['wifi_box']:
75+
self.devices.append(Device(self, device))
76+
return self.devices

0 commit comments

Comments
 (0)