Skip to content

Commit c853dfa

Browse files
committed
Fix in gettiong devicelist.
1 parent 46636b6 commit c853dfa

30 files changed

+91
-123
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ create `settings.json` file, and fill token and secret.
3838
Run example script.
3939

4040
```python
41-
from switchbot_utility.switchbot import Switchbot
41+
import switchbot_utility.switchbot as sbu
4242

43-
switchbot = Switchbot()
43+
switchbot = sbu.Switchbot()
4444
switchbot.devicelist()
4545
```
4646

@@ -51,18 +51,18 @@ Scripts makes `deviceList.txt`. You can manipulate device using diviceId in this
5151
### Get temperature from SwitchbotMeter
5252

5353
```python
54-
from switchbot_utility.switchbot_meter import SwitchbotMeter
54+
import switchbot_utility as sbu
5555

56-
meter = SwitchbotMeter("meterDeviceId")
56+
meter = sbu.SwitchbotMeter("meterDeviceId")
5757
print(meter.get_temperature())
5858
```
5959

6060
### Unlock SwitchbotLock
6161

6262
```python
63-
from switchbot_utility.switchbot_lock import SwitchbotLock
63+
import switchbot_utility as sbu
6464

65-
lock = SwitchbotLock("lockDeviceId")
65+
lock = sbu.SwitchbotLock("lockDeviceId")
6666
lock.unlock()
6767
```
6868

src/switchbot_utility/battery_mixin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class BatteryMixin:
2+
"""Switchbot battery mixin"""
23
def get_battery(self):
34
"""Returns battery level"""
45
status = self.get_status()
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
2+
import sys
23

34
import requests
5+
from requests.exceptions import Timeout
46

57

68
class CommandMixin:
@@ -11,8 +13,12 @@ def command(self, deviceId: str, body: dict):
1113
"""Send command"""
1214

1315
header = self.gen_sign()
14-
return requests.post(
15-
self._baseurl + deviceId + "/commands",
16-
headers=header,
17-
data=json.dumps(body),
18-
)
16+
try:
17+
return requests.post(
18+
self._baseurl + deviceId + "/commands",
19+
headers=header,
20+
data=json.dumps(body),
21+
timeout=(3.0, 7.5)
22+
)
23+
except Timeout:
24+
sys.exit("Timeout")

src/switchbot_utility/switchbot.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
import uuid
88

99
import requests
10+
from requests.exceptions import Timeout
1011

1112

1213
class Switchbot:
1314
"""Switchbot Utility class"""
1415

1516
def __init__(self):
1617
"""Constructor"""
17-
pass
1818

1919
def read_token(self) -> tuple:
2020
"""Import access token and secret from settings.json"""
2121
try:
22-
with open("settings.json", "r") as f:
22+
with open("settings.json", "r", encoding="utf8") as f:
2323
settings = json.load(f)
2424
token = settings["token"]
2525
secret = settings["secret"]
@@ -61,33 +61,64 @@ def gen_sign(self) -> dict:
6161
def devicelist(self) -> None:
6262
"""Create all Switchbot device list as deviceList.txt"""
6363
header = self.gen_sign()
64-
response = requests.get(
65-
"https://api.switch-bot.com/v1.1/devices", headers=header
66-
)
64+
try:
65+
response = requests.get(
66+
"https://api.switch-bot.com/v1.1/devices", headers=header,
67+
timeout=(3.0, 7.5)
68+
)
69+
except Timeout:
70+
sys.exit("Timeout")
71+
6772
devices = json.loads(response.text)
6873

6974
with open("deviceList.txt", "w", encoding="utf-8", newline="\n") as f:
70-
try:
71-
for device in devices["body"]["deviceList"]:
75+
for device in devices["body"]["deviceList"]:
76+
try:
7277
f.write(device["deviceId"] + ", ")
78+
except KeyError:
79+
f.write(", ")
80+
try:
7381
f.write(device["deviceName"] + ", ")
82+
except KeyError:
83+
f.write(", ")
84+
try:
7485
f.write(device["deviceType"] + ", ")
86+
except KeyError:
87+
f.write(", ")
88+
try:
7589
f.write(device["hubDeviceId"] + "\n")
90+
except KeyError:
91+
f.write("\n")
7692

77-
for device in devices["body"]["infraredRemoteList"]:
93+
for device in devices["body"]["infraredRemoteList"]:
94+
try:
7895
f.write(device["deviceId"] + ", ")
96+
except KeyError:
97+
f.write(", ")
98+
try:
7999
f.write(device["deviceName"] + ", ")
100+
except KeyError:
101+
f.write(", ")
102+
try:
80103
f.write(device["remoteType"] + ", ")
104+
except KeyError:
105+
f.write(", ")
106+
try:
81107
f.write(device["hubDeviceId"] + "\n")
82-
except KeyError:
83-
sys.exit("Something wrong")
108+
except KeyError:
109+
f.write("\n")
84110

85111
def get_scene_list(self) -> None:
86112
"""Get scene List as sceneList.txt"""
87113
header = self.gen_sign()
88-
response = requests.get(
89-
"https://api.switch-bot.com/v1.1/scenes", headers=header
90-
)
114+
try:
115+
response = requests.get(
116+
"https://api.switch-bot.com/v1.1/scenes", headers=header,
117+
timeout=(3.0, 7.5),
118+
)
119+
except Timeout:
120+
sys.exit("Timeout")
121+
91122
scenes = json.loads(response.text)
92123

93124
if scenes["message"] != "success":
@@ -104,5 +135,10 @@ def scene_execute(self, sceneId: str) -> str:
104135
"""Execute scene"""
105136
header = self.gen_sign()
106137
url = "https://api.switch-bot.com/v1.1/scenes/" + sceneId + "/execute"
107-
response = requests.post(url=url, headers=header)
138+
try:
139+
response = requests.post(url=url, headers=header,
140+
timeout=(3.0, 7.5))
141+
except Timeout:
142+
sys.exit("Timeout")
143+
108144
return response.text

src/switchbot_utility/switchbot_blind_tilt.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
class SwitchbotBlindTilt(SwitchbotDevice, OnOffMixin):
66
"""Switchbot Blind Tilt class"""
77

8-
def __init(self, deviceId):
9-
"""Constructor"""
10-
super().__init__(deviceId)
11-
128
def set_position(self, direction: str, position: int) -> str:
139
"""Set blind position.
1410

src/switchbot_utility/switchbot_bot.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
class SwitchbotBot(SwitchbotDevice, OnOffMixin, BatteryMixin):
77
"""Switchbot bot class"""
88

9-
def __init__(self, deviceId):
10-
"""Constructor"""
11-
super().__init__(deviceId)
12-
139
def get_power(self) -> dict:
1410
"""Returns ON/OFF state"""
1511
status = self.get_status()

src/switchbot_utility/switchbot_ceiling_light.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
class SwitchbotCeilingLight(SwitchbotColorBulb):
55
"""Switchbot Ceiling Light class"""
66

7-
def __init__(self, deviceId):
8-
"""Constructor"""
9-
super().__init__(deviceId)
10-
117
def set_color(self, r, g, b):
128
"""Do nothing"""
13-
pass
149

1510
def get_power(self):
1611
"""Returns ON/OFF state"""

src/switchbot_utility/switchbot_ceiling_light_pro.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@
33

44
class SwithbotCeilingLightPro(SwitchbotCeilingLight):
55
"""Switchbot Ceiling Light class"""
6-
7-
def __init__(self, deviceId):
8-
"""Constructor"""
9-
super().__init__(deviceId)

src/switchbot_utility/switchbot_color_bulb.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33

44
class SwitchbotColorBulb(SwitchbotStripLight):
5-
"""Constructor"""
6-
7-
def __init__(self, deviceId):
8-
super().__init__(deviceId)
5+
"""Switchbot Color Bulb class"""
96

107
def set_color_temperature(self, temperature: int) -> str:
118
"""Set color temperature"""

src/switchbot_utility/switchbot_contact_sensor.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
class SwitchbotContactSensor(SwitchbotMotionSensor):
55
"""Switchbot Contact Sensor class"""
66

7-
def __init__(self, deviceId):
8-
"""Constructor"""
9-
super().__init__(deviceId)
10-
117
def get_open_state(self) -> dict:
128
"""Returns the open state of the sensor"""
139
status = self.get_status()

0 commit comments

Comments
 (0)