-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassExample.py
More file actions
77 lines (64 loc) · 3.63 KB
/
Copy pathClassExample.py
File metadata and controls
77 lines (64 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
from pyhap.accessory import Accessory
from pyhap.const import CATEGORY_SENSOR, CATEGORY_OTHER
import logging, time
import asyncio
from history import FakeGatoHistory
logging.basicConfig(level=logging.INFO, format="[%(module)s] %(message)s")
class mDNSAdvertizer(Accessory):
'''
Use that to advertize continously the mDNS record. to prevent disconnects after network changes
'''
category = CATEGORY_OTHER # Neutral, won’t appear with icons
def __init__(self, driver, *args, **kwargs):
super().__init__(driver, *args, **kwargs)
self.driver = driver
self.set_info_service(firmware_revision="0.0.1", manufacturer="Pythonaire", model="HiddenAdv")
@Accessory.run_at_interval(3600)
async def run(self):
try:
await asyncio.sleep(5) # delay first run slightly
if not self.driver or self.driver.advertiser is None:
logging.warning("[INFO] Advertiser not ready, skipping re-announce.")
return
logging.info("[INFO] Refreshing HomeKit advertisement...")
self.driver.update_advertisement()
except Exception as e:
logging.warning(f"[INFO] Failed to update advertisement: {e}")
class Weather(Accessory):
category = CATEGORY_SENSOR
def __init__(self, node, *args, **kwargs):
super().__init__(*args, **kwargs)
self.name = args[1] # args[1] contained the Sensor Name given
self.node = node # node number of the 433MHz sensor
self.set_info_service(firmware_revision='0.0.2', model='Gardener01', manufacturer= 'Pythonaire', serial_number="Weather-001")
AirTemperature = self.add_preload_service('TemperatureSensor', chars=['Name', 'CurrentTemperature'])
AirTemperature.configure_char('Name', value= 'Air Temperature')
self.AirTemperature = AirTemperature.configure_char('CurrentTemperature', value= 0) #initial
AirHumidity = self.add_preload_service('HumiditySensor', chars=['Name', 'CurrentRelativeHumidity'])
AirHumidity.configure_char('Name', value= 'Air Humidity')
self.AirHumidity = AirHumidity.configure_char('CurrentRelativeHumidity', value = 0) # initial
AirPressure = self.add_preload_service('AtmosphericPressureSensor', chars=['Name', 'AtmosphericPressure'])
AirPressure.configure_char('Name', value= 'Air Pressure')
self.AirPressure = AirPressure.configure_char('AtmosphericPressure', value = 0) # initial
Battery = self.add_preload_service("BatteryService", chars=['ChargingState','StatusLowBattery', 'BatteryLevel'])
self.BattLevel = Battery.configure_char('BatteryLevel', value = 0)
self.BattStatus = Battery.configure_char('StatusLowBattery', value = 1)
Battery.configure_char('ChargingState', value = 2)
self.History = FakeGatoHistory('weather', self)
@Accessory.run_at_interval(600)
def run(self):
'''
in this example a external function getnodeData() pull sensor data from a sensor node, defined by self.node.
'''
nodeBattery = getnodeData('Battery')
nodeHumidity = getnodeData('Humidity')
nodeTemperature = getnodeData('Temperature')
nodePressure = getnodeData('Pressure')
self.BattStatus.set_value(nodeBattery)
self.AirHumidity.set_value(nodeHumidity)
self.AirTemperature.set_value(nodeTemperature)
self.AirPressure.set_value(nodePressure)
self.History.addEntry({'time':int(round(time.time())),'temp':nodeTemperature,'humidity': nodeHumidity, 'pressure':nodePressure})
def stop(self):
logging.info('Stopping accessory.')