def get_bridge(): """Call this method to get a Bridge instead of a standalone accessory.""" devices = json.loads(open("devices.json",'r').read()) bridge = json.loads(open("bridge.json",'r').read()) bridge = Bridge(display_name=bridge['name'],mac=util.generate_mac(), pincode=b"111-11-111") #bridge = Bridge(display_name="Bridge", mac=util.generate_mac(),pincode=b"203-23-999") #bridge.add_accessory(LightBulb("Luz",server=server_auth,device_id="F9-CB-E6-6D-F4-00")) #bridge.add_accessory(LightBulbDimmer("Luz",server=server_auth,device_id="F9-CB-E6-6D-F4-02")) i = 2 for device in devices: i += 1 if (device['service'] == "LIGHTBULB"): if(device['dimmer']): bridge.add_accessory(LightBulbDimmer(device['name'],aid=i ,server=server_auth,device_id=device['device_id'])) else: bridge.add_accessory(LightBulb(device['name'],aid=i ,server=server_auth,device_id=device['device_id'])) elif (device['service'] == "GARAGE_DOOR_OPENER"): bridge.add_accessory(GarageDoor(device['name'],aid=i ,server=server_auth,device_id=device['device_id'])) else: pass return bridge
def __init__(self, accessory, port, address=None, persist_file="accessory.state", encoder=None): """ :param accessory: The `Accessory` to be managed by this driver. The `Accessory` must have the standalone AID (`pyhap.accessory.STANDALONE_AID`). If the AID of the `Accessory` is None, the standalone AID will be assigned to it. Also, if the mac of the `Accessory` is None, a randomly-generated one will be assigned to it. :type accessory: Accessory :param port: The local port on which the accessory will be accessible. In other words, this is the port of the HAPServer. :type port: int :param address: The local address on which the accessory will be accessible. In other words, this is the address of the HAPServer. If not given, the driver will try to select an address. :type address: str :param persist_file: The file name in which the state of the accessory will be persisted. This uses `expandvars`, so may contain `~` to refer to the user's home directory. :type persist_file: str :param encoder: The encoder to use when persisting/loading the Accessory state. :type encoder: AccessoryEncoder """ if accessory.aid is None: accessory.aid = STANDALONE_AID elif accessory.aid != STANDALONE_AID: raise ValueError("Top-level accessory must have the standalone AID.") if accessory.mac is None: accessory.mac = util.generate_mac() self.accessory = accessory self.address = address or util.get_local_address() self.http_server = HAPServer((self.address, port), self) self.http_server_thread = None self.advertiser = Zeroconf() self.port = port self.persist_file = os.path.expanduser(persist_file) self.encoder = encoder or AccessoryEncoder() if os.path.exists(self.persist_file): logger.info("Loading Accessory state from `%s`", self.persist_file) self.load() else: logger.info("Storing Accessory state in `%s`", self.persist_file) self.persist() self.topics = {} # topic: set of (address, port) of subscribed clients self.topic_lock = threading.Lock() # for exclusive access to the topics self.event_queue = queue.Queue() # (topic, bytes) self.send_event_thread = None # the event dispatch thread self.sent_events = 0 self.accumulated_qsize = 0 self.accessory.set_broker(self) self.mdns_service_info = None self.srp_verifier = None self.run_sentinel = None self.accessory_thread = None
def __init__(self, display_name, mac=None, pincode=None, iid_manager=None): aid = STANDALONE_AID # A Bridge cannot be Bridge, hence talks directly to HAP clients. # Thus, we need a mac. mac = mac or util.generate_mac() super(Bridge, self).__init__(display_name, aid=aid, mac=mac, pincode=pincode, iid_manager=iid_manager) self.accessories = {} # aid: acc
def __init__(self, *, address=None, mac=None, pincode=None, port=None): """Initialize a new object. Create key pair. Must be called with keyword arguments. """ self.address = address or util.get_local_address() self.mac = mac or util.generate_mac() self.pincode = pincode or util.generate_pincode() self.port = port or DEFAULT_PORT self.setup_id = util.generate_setup_id() self.config_version = DEFAULT_CONFIG_VERSION self.paired_clients = {} sk, vk = ed25519.create_keypair() self.private_key = sk self.public_key = vk
def test_persist_and_load(): """Stores an Accessory and then loads the stored state into another Accessory. Tests if the two accessories have the same property values. """ mac = generate_mac() _pk, sample_client_pk = ed25519.create_keypair() state = State(mac=mac) state.add_paired_client(uuid.uuid1(), sample_client_pk.to_bytes()) config_loaded = State() config_loaded.config_version += 2 # change the default state. enc = encoder.AccessoryEncoder() with tempfile.TemporaryFile(mode="r+") as fp: enc.persist(fp, state) fp.seek(0) enc.load_into(fp, config_loaded) assert state.mac == config_loaded.mac assert state.private_key == config_loaded.private_key assert state.public_key == config_loaded.public_key assert state.config_version == config_loaded.config_version assert state.paired_clients == config_loaded.paired_clients
def create(cls, display_name, pincode, aid=STANDALONE_AID): mac = util.generate_mac() return cls(display_name, aid=aid, mac=mac, pincode=pincode)
import pyhap.util as util from pyhap.accessory import Bridge from pyhap.accessory_driver import AccessoryDriver from accessories.air_quality_monitor import AirQualityMonitor from accessories.vacuum import Vacuum from accessories.air_fresh import AirFresh from accessories.presence import Presence from accessories.dummy_switch import DummySwitch logging.basicConfig(level=logging.DEBUG) driver = AccessoryDriver(port=54321, pincode=bytearray(b'111-11-111'), mac=util.generate_mac()) bridge = Bridge(driver, 'Bridge') air_quality = AirQualityMonitor(driver, 'Air Monitor', ip='192.168.1.1', token='XXX') bridge.add_accessory(air_quality) vacuum = Vacuum(driver, 'Vacuum', ip='192.168.1.2', token='XXX') bridge.add_accessory(vacuum) air_fresh = AirFresh(driver, 'Air Fresh', ip='192.168.1.3', token='XXX') bridge.add_accessory(air_fresh) nikolay_at_home = Presence(driver, 'Nikolay', hostname='192.168.1.4') bridge.add_accessory(nikolay_at_home)
def get_accessory(): """Call this method to get a standalone Accessory.""" acc = TemperatureSensor("MyTempSensor", pincode=b"203-23-999", mac=util.generate_mac()) return acc