Accessory

Base class for HAP Accessories.

class pyhap.accessory.Accessory(driver, display_name, aid=None, iid_manager=None)[source]

A representation of a HAP accessory.

Inherit from this class to build your own accessories.

add_info_service()[source]

Helper method to add the required AccessoryInformation service.

Called in __init__ to be sure that it is the first service added. May be overridden.

add_preload_service(service, chars=None, unique_id=None)[source]

Create a service with the given name and add it to this acc.

add_protocol_version_service()[source]

Helper method to add the required HAP Protocol Information service

add_service(*servs)[source]

Add the given services to this Accessory.

This also assigns unique IIDS to the services and their Characteristics.

Note

Do not add or remove characteristics from services that have been added to an Accessory, as this will lead to inconsistent IIDs.

Parameters:servs – Variable number of services to add to this Accessory.
Type:Service
available

Accessory is available.

If available is False, get_characteristics will return SERVICE_COMMUNICATION_FAILURE for the accessory which will show as unavailable.

Expected to be overridden.

get_characteristic(aid, iid)[source]

Get the characteristic for the given IID.

The AID is used to verify if the search is in the correct accessory.

get_service(name)[source]

Return a Service with the given name.

A single Service is returned even if more than one Service with the same name are present.

Parameters:name (str) – The display_name of the Service to search for.
Returns:A Service with the given name or None if no such service exists in this Accessory.
Return type:Service
publish(value, sender, sender_client_addr=None, immediate=False)[source]

Append AID and IID of the sender and forward it to the driver.

Characteristics call this method to send updates.

Parameters:
  • data (dict) – Data to publish, usually from a Characteristic.
  • sender – The Service or Characteristic from which the call originated.
Type:

Service or Characteristic

run()[source]

Called when the Accessory should start doing its thing.

Called when HAP server is running, advertising is set, etc. Can be overridden with a normal or async method.

static run_at_interval(seconds)[source]

Decorator that runs decorated method every x seconds, until stopped.

Can be used with normal and async methods.

@Accessory.run_at_interval(3)
def run(self):
    print("Hello again world!")
Parameters:seconds (float) – The amount of seconds to wait for the event to be set. Determines the interval on which the decorated method will be called.
set_info_service(firmware_revision=None, manufacturer=None, model=None, serial_number=None)[source]

Quick assign basic accessory information.

set_primary_service(primary_service)[source]

Set the primary service of the acc.

setup_message()[source]

Print setup message to console.

For QRCode base36, pyqrcode are required. Installation through pip install HAP-python[QRCode]

stop()[source]

Called when the Accessory should stop what is doing and clean up any resources.

Can be overridden with a normal or async method.

to_HAP()[source]

A HAP representation of this Accessory.

Returns:A HAP representation of this accessory. For example:
{ "aid": 1,
    "services": [{
        "iid" 2,
        "type": ...,
        ...
    }]
}
Return type:dict
xhm_uri()[source]

Generates the X-HM:// uri (Setup Code URI)

Return type:str