homekit_python icon indicating copy to clipboard operation
homekit_python copied to clipboard

Adding SpeakerService

Open MarcBook opened this issue 5 years ago • 5 comments
trafficstars

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

MarcBook avatar Nov 23 '20 16:11 MarcBook

@MarcBook I will have a look at it!

jlusiardi avatar Nov 25 '20 10:11 jlusiardi

The code looks fine to me and if I pair it using homekit_python i can control it. Seems to be a problem with the homekit app on the ios device somehow.

jlusiardi avatar Nov 25 '20 19:11 jlusiardi

I seem to recall in home assistant seeing quite a few tickets where people couldn't find the volume control for the TV when using the HomeKit bridge. From what I remember it doesn't actually show up in the iOS home app, it's somewhere unexpected. I wonder if something similar is going on here.

Jc2k avatar Nov 25 '20 19:11 Jc2k

E.g. https://www.reddit.com/r/homebridge/comments/ccco92/comment/etmh4xr

Jc2k avatar Nov 25 '20 19:11 Jc2k

Thanks for you replies. I'll try it.

MarcBook avatar Dec 24 '20 01:12 MarcBook