Skip to content

Adding SpeakerService #210

@MarcBook

Description

@MarcBook

Hi, I tried to add a SpeakerService to my homekit python project. I created the service and added my MuteCharacteristicMixin as well as the VolumeCharacteristicMixin from this project. After I added the speaker to the my homekit app it looks like this. I'd expect to see a mute switch and a volume control. Am I doing something wrong? Once I fixed it should I contribute the service and the characteristics to this project or is this only a implementation of the API and everyone should do it by his own?

My code:
speaker_service.py

from homekit.model.characteristics import VolumeCharacteristicMixin
from homekit.model.services import ServicesTypes, AbstractService

from model.characteristics.mute import MuteCharacteristicMixin


class SpeakerService(AbstractService, MuteCharacteristicMixin, VolumeCharacteristicMixin):
    def __init__(self):
        AbstractService.__init__(self, ServicesTypes.get_uuid('public.hap.service.speaker'), get_id())
        MuteCharacteristicMixin.__init__(self, get_id())

        # optional
        VolumeCharacteristicMixin.__init__(self, get_id())

mute.py

from homekit.model.characteristics import CharacteristicsTypes, CharacteristicFormats, CharacteristicPermissions, \
    AbstractCharacteristic


class MuteCharacteristic(AbstractCharacteristic):
    """
    Defined on page 145
    """

    def __init__(self, iid):
        AbstractCharacteristic.__init__(self, iid, CharacteristicsTypes.MUTE, CharacteristicFormats.bool)
        self.description = 'Mute (on/off)'
        self.perms = [CharacteristicPermissions.paired_write, CharacteristicPermissions.paired_read,
                      CharacteristicPermissions.events]
        self.value = False


class MuteCharacteristicMixin(object):
    def __init__(self, iid):
        self._muteCharacteristic = MuteCharacteristic(iid)
        self.characteristics.append(self._muteCharacteristic)

    def set_mute_set_callback(self, callback):
        self._muteCharacteristic.set_set_value_callback(callback)

    def set_mute_get_callback(self, callback):
        self._muteCharacteristic.set_get_value_callback(callback)

part of the main.py

    try:
        httpd = AccessoryServer(os.path.expanduser('~/.homekit/demoserver.json'))

        lightAccessory = Accessory('test_light', 'homekit_python', 'Demoserver', '0001', '0.1')
        lightService = BHSLightBulbService()
        lightService.set_on_set_callback(bhs_light_bulb_callbacks.on)
        lightService.set_brightness_set_callback(bhs_light_bulb_callbacks.on_set_brightness)
        lightService.set_hue_set_callback(bhs_light_bulb_callbacks.on_set_hue)
        lightService.set_saturation_set_callback(bhs_light_bulb_callbacks.on_set_saturation)
        lightAccessory.services.append(lightService)

        tempHumAccessory = Accessory('test_temp_hum_sensor', 'homekit_python', 'Demoserver', '0001', '0.1')
        temperature = TemperatureSensorService()
        humidity = HumiditySensorService()
        tempHumAccessory.services.append(temperature)
        tempHumAccessory.services.append(humidity)

        speakerAccessory = Accessory('test_speaker', 'homekit_python', 'Demoserver', '0001', '0.1')
        speaker = SpeakerService()
        speaker.set_mute_get_callback(speaker_callbacks.mute_get)
        speaker.set_mute_set_callback(speaker_callbacks.mute_set)
        speaker.set_volume_get_callback(speaker_callbacks.volume_get)
        speaker.set_volume_set_callback(speaker_callbacks.volume_set)
        speakerAccessory.to_accessory_and_service_list()
        speakerAccessory.services.append(speaker)

        httpd.accessories.add_accessory(lightAccessory)
        httpd.accessories.add_accessory(tempHumAccessory)
        httpd.accessories.add_accessory(speakerAccessory)

        httpd.publish_device()
        print('published device and start serving')
        httpd.serve_forever()

    except KeyboardInterrupt:
        print('unpublish device')
        httpd.unpublish_device()
```

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions