Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1d3083

Browse files
author
Ilya Mordasov
committed
Added Turn On/Off to g201s
1 parent 007bd1b commit c1d3083

File tree

6 files changed

+134
-47
lines changed

6 files changed

+134
-47
lines changed

accessories/G201S.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# An Accessory for a Smart Kettle.
2+
from sensors.g201s import G201S as sensor
3+
from pyhap.accessory import Accessory
4+
from pyhap.const import CATEGORY_SENSOR
5+
6+
class G201S(Accessory):
7+
8+
category = CATEGORY_SENSOR
9+
10+
def __init__(self, *args, **kwargs):
11+
super().__init__(*args, **kwargs)
12+
13+
serv_kettle = self.add_preload_service('SmartKettle')
14+
g201s = sensor()
15+
g201s.set_temperature(4)
16+
self.g201s = g201s
17+
18+
self.char_on = serv_kettle.configure_char('On', setter_callback=self.turn_on)
19+
self.char_set = serv_kettle.configure_char('TargetTemperature', setter_callback=self.set_temperature)
20+
self.char_get = serv_kettle.configure_char('CurrentTemperature', setter_callback=self.get_temperature)
21+
#self.char_get = serv_kettle.get_characteristic('CurrentTemperature')
22+
23+
def turn_on(self, value):
24+
if value:
25+
print("Чайник включен {}".format(value))
26+
else:
27+
print("Чайник выключен {}".format(value))
28+
29+
def set_temperature(self, value):
30+
self.char_set.set_value(value);
31+
self.g201s.set_temperature(value)
32+
print("Чайник установлен на {}°C".format(value))
33+
34+
def get_temperature(self, value):
35+
print("Температура чайник 25°C")
36+
37+
def turn_off(self, value):
38+
super().stop()
39+
print(">> Чайник выключен {}".format(value))

main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
# The below package can be found in the HAP-python github repo under accessories/
1616
from accessories.TemperatureSensor import TemperatureSensor
1717
from accessories.BMP180 import BMP180
18+
from accessories.G201S import G201S
1819

1920
logging.basicConfig(level=logging.INFO)
2021

2122

2223
def get_bridge(driver):
2324
"""Call this method to get a Bridge instead of a standalone accessory."""
2425
bridge = Bridge(driver, 'Bridge')
25-
temp_sensor = TemperatureSensor(driver, 'CurrentTemperature')
26-
bmp180_sensor = BMP180(driver, 'CurrentPressure')
26+
# temp_sensor = TemperatureSensor(driver, 'CurrentTemperature')
27+
# bmp180_sensor = BMP180(driver, 'CurrentPressure')
28+
g201s_sensor = G201S(driver, 'SmartKettle')
2729

28-
bridge.add_accessory(temp_sensor)
29-
bridge.add_accessory(bmp180_sensor)
30+
# bridge.add_accessory(temp_sensor)
31+
# bridge.add_accessory(bmp180_sensor)
32+
bridge.add_accessory(g201s_sensor)
3033

3134
return bridge
3235

pyhap/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
CATEGORY_PROGRAMMABLE_SWITCH = 15
3636
CATEGORY_RANGE_EXTENDER = 16
3737
CATEGORY_CAMERA = 17
38+
CATEGORY_SMARTKETTLE = 18
3839

3940

4041
# ### HAP Permissions ###

pyhap/resources/characteristics.json

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,9 +1304,9 @@
13041304
"ev"
13051305
],
13061306
"UUID": "00000035-0000-1000-8000-0026BB765291",
1307-
"maxValue": 38,
1308-
"minStep": 0.1,
1309-
"minValue": 10,
1307+
"maxValue": 100,
1308+
"minStep": 1,
1309+
"minValue": 0,
13101310
"unit": "celsius"
13111311
},
13121312
"TargetTiltAngle": {
@@ -1344,8 +1344,8 @@
13441344
],
13451345
"UUID": "00000036-0000-1000-8000-0026BB765291",
13461346
"ValidValues": {
1347-
"Celsius": 0,
1348-
"Fahrenheit": 1
1347+
"Celsius": 1,
1348+
"Fahrenheit": 0
13491349
}
13501350
},
13511351
"VOCDensity": {
@@ -1404,17 +1404,5 @@
14041404
"UUID": "000000B5-0000-1000-8000-0026BB765291",
14051405
"maxValue": 100,
14061406
"minValue": 0
1407-
},
1408-
"CurrentPressure": {
1409-
"Format": "float",
1410-
"Permissions": [
1411-
"pr",
1412-
"ev"
1413-
],
1414-
"UUID": "00000015-0000-1000-8000-005656765291",
1415-
"maxValue": 1000,
1416-
"minStep": 0.1,
1417-
"minValue": 0,
1418-
"unit": "mmHg"
14191407
}
14201408
}

pyhap/resources/services.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,5 +539,13 @@
539539
"CurrentPressure"
540540
],
541541
"UUID": "E863F10F-079E-48FF-8F27-9C2605A29F52"
542+
},
543+
"SmartKettle": {
544+
"RequiredCharacteristics": [
545+
"On",
546+
"TargetTemperature",
547+
"CurrentTemperature"
548+
],
549+
"UUID": "00000043-0000-1000-8000-0026BB765291"
542550
}
543551
}

sensors/g201s.py

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,82 @@
11
import pygatt
2+
import sensors.sensorbase as sensorbase
23
from binascii import hexlify
34

45
YOUR_DEVICE_ADDRESS = "E5:FB:01:09:F7:B4"
56
ADDRESS_TYPE = pygatt.BLEAddressType.random
67

78
adapter = pygatt.GATTToolBackend()
9+
index = 0
810

9-
def handle_data(handle, value):
10-
"""
11-
handle -- integer, characteristic read handle the data was received on
12-
value -- bytearray, the data returned in the notification
13-
"""
14-
print("Received data: %s" % hexlify(value))
15-
"""
16-
def write_handle(handle, value):
17-
val = [0x55, i, value]
18-
device.char_write_handle(handle, val, True)
19-
"""
20-
try:
21-
adapter.start()
22-
device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE)
23-
device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data)
24-
25-
device.char_write_handle(0x000e, bytearray([0x55, 0x00, 0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43, 0xAA]), True)
26-
27-
device.char_write_handle(0x000c, bytearray([0x01, 0x00]), True)
28-
29-
device.char_write_handle(0x000e, bytearray([0x55, 0x01, 0x01, 0xAA]), True)
30-
device.char_write_handle(0x000e, bytearray([0x55, 0x02, 0x03, 0xAA]), True)
31-
device.char_write_handle(0x000e, bytearray([0x55, 0x03, 0x06, 0xAA]), True)
32-
33-
finally:
34-
adapter.stop()
11+
class G201S(sensorbase.SensorBase):
12+
def __init__(self):
13+
super(G201S, self).__init__(update_callback = self._update_sensor_data)
14+
15+
@property
16+
def handle_data(handle, value):
17+
"""
18+
handle -- integer, characteristic read handle the data was received on
19+
value -- bytearray, the data returned in the notification
20+
"""
21+
print("Received data: %s" % hexlify(value))
22+
23+
@property
24+
def write_handle(handle, value, increment=True):
25+
global index
26+
if handle == 0x000e:
27+
val = [0x55, bytes(index), value, 0xAA]
28+
else:
29+
val = [bytes(index), value]
30+
31+
device.char_write_handle(handle, val, True)
32+
if increment: index += 1
33+
print(val)
34+
35+
def turn_on(self):
36+
try:
37+
adapter.start()
38+
device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE)
39+
device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data)
40+
41+
write_handle(0x000e, bytearray([0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43]), False)
42+
write_handle(0x000c, bytearray([0x01, 0x00]))
43+
write_handle(0x000e, bytearray([0x01]))
44+
write_handle(0x000e, bytearray([0x03]))
45+
46+
finally:
47+
adapter.stop()
48+
49+
def turn_off(self):
50+
try:
51+
adapter.start()
52+
device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE)
53+
device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data)
54+
55+
write_handle(0x000e, bytearray([0x00, 0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43]), False)
56+
write_handle(0x000c, bytearray([0x01, 0x00]))
57+
write_handle(0x000e, bytearray([0x01]))
58+
write_handle(0x000e, bytearray([0x04]))
59+
60+
finally:
61+
adapter.stop()
62+
63+
def set_temperature(self, value):
64+
print("set temperature {}".format(value))
65+
# try:
66+
# adapter.start()
67+
# device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE)
68+
# device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data)
69+
70+
# write_handle(0x000e, bytearray([0x00, 0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43]), False)
71+
# write_handle(0x000c, bytearray([0x01, 0x00]))
72+
# write_handle(0x000e, bytearray([0x01]))
73+
# write_handle(0x000e, bytearray([0x06]))
74+
75+
# finally:
76+
# adapter.stop()
77+
78+
def _update_sensor_data(self):
79+
print("update")
80+
81+
if __name__ == '__main__':
82+
sensor = G201S(self)

0 commit comments

Comments
 (0)