Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding SpeakerService #210

Open
MarcBook opened this issue Nov 23, 2020 · 5 comments
Open

Adding SpeakerService #210

MarcBook opened this issue Nov 23, 2020 · 5 comments

Comments

@MarcBook
Copy link

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
@jlusiardi
Copy link
Owner

@MarcBook I will have a look at it!

@jlusiardi
Copy link
Owner

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.

@Jc2k
Copy link
Collaborator

Jc2k commented Nov 25, 2020

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
Copy link
Collaborator

Jc2k commented Nov 25, 2020

@MarcBook
Copy link
Author

Thanks for you replies. I'll try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants