Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions accessories/AudioReceiver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""A sample audio receiver (AV receiver) accessory.

Demonstrates the ``CATEGORY_AUDIO_RECEIVER`` category together with the
``Speaker`` service, so the accessory shows up with the receiver tile in
the Home app.
"""
import logging
import signal

from pyhap.accessory import Accessory
from pyhap.accessory_driver import AccessoryDriver
from pyhap.const import CATEGORY_AUDIO_RECEIVER


class AudioReceiver(Accessory):
"""Fake audio receiver, logs what the client sets."""

category = CATEGORY_AUDIO_RECEIVER

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.set_info_service(
manufacturer='HaPK',
model='Sample AV Receiver',
firmware_revision='1.0',
serial_number='1'
)

speaker_service = self.add_preload_service(
'Speaker', ['Mute', 'Volume', 'Active'],
)
self._mute = speaker_service.configure_char(
'Mute', value=False, setter_callback=self._on_mute,
)
self._volume = speaker_service.configure_char(
'Volume', value=25, setter_callback=self._on_volume,
)
speaker_service.configure_char('Active', value=1)

def _on_mute(self, value):
print('Mute' if value else 'Unmute')

def _on_volume(self, value):
print('Set volume to %s' % value)


def main():
logging.basicConfig(level=logging.INFO)

driver = AccessoryDriver(port=51826)
driver.add_accessory(accessory=AudioReceiver(driver, 'Audio Receiver'))

signal.signal(signal.SIGTERM, driver.signal_handler)
driver.start()


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions docs/source/intro/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ device.
changes its value from a background ``run`` task.
* - ``TV.py``, ``FakeFan.py``, ``DisplaySwitch.py``
- TVs, fans and switches: services with several linked characteristics.
* - ``AudioReceiver.py``
- An audio receiver: the ``CATEGORY_AUDIO_RECEIVER`` category with a
``Speaker`` service for volume and mute.
* - ``BMP180.py``, ``AM2302.py``, ``TSL2591.py``, ``SDS011.py``
- Reading real hardware (I²C, GPIO, serial) and publishing it, polled
from ``run`` with ``run_at_interval``.
Expand Down
1 change: 1 addition & 0 deletions pyhap/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
CATEGORY_SHOWER_HEAD = 30
CATEGORY_TELEVISION = 31
CATEGORY_TARGET_CONTROLLER = 32 # Remote Controller
CATEGORY_AUDIO_RECEIVER = 34 # Audio Receiver


# ### HAP Permissions ###
Expand Down
8 changes: 8 additions & 0 deletions tests/test_accessory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pyhap.accessory import Accessory, Bridge
from pyhap.accessory_driver import AccessoryDriver
from pyhap.const import (
CATEGORY_AUDIO_RECEIVER,
CATEGORY_CAMERA,
CATEGORY_TARGET_CONTROLLER,
CATEGORY_TELEVISION,
Expand Down Expand Up @@ -202,6 +203,13 @@ def test_xhm_uri(mock_driver: Any) -> None:
mock_driver.state.setup_id = "BBBB"
assert acc_1.xhm_uri() == "X-HM://00VPU8UEKBBBB"

acc_1.category = CATEGORY_AUDIO_RECEIVER
mock_driver.state = State(
address="1.2.3.4", mac="AA::BB::CC::DD::EE", pincode=b"323-23-1212", port=44
)
mock_driver.state.setup_id = "BBBB"
assert acc_1.xhm_uri() == "X-HM://00XOVCWDOBBBB"


def test_set_info_service(mock_driver: Any) -> None:
acc_1 = Accessory(mock_driver, "Test Accessory 1", aid=2)
Expand Down
Loading