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

Make AccessoryEncoder easier to subclass. #80

Open
schinckel opened this issue Apr 7, 2018 · 3 comments
Open

Make AccessoryEncoder easier to subclass. #80

schinckel opened this issue Apr 7, 2018 · 3 comments

Comments

@schinckel
Copy link
Contributor

Currently, it's really not possible to extend the encoder: you need to copy-paste the method body, and then add your extra stuff you want to store in the state before you dump it to json.

I think each operation should be split into two, so it's possible to add/change the data that will be persisted (and handle that extra data on the way in), but also more easily switch the encoding method (for instance, to be able to use a non-JSON encoder).

class Encoder(object):
    def prepare_state(self, accessory):
        return { ... }

    def persist(self, fp, accessory):
        json.dump(self.prepare_state(accessory), fp)

    def restore_state(self, accessory, state):
        ...

    def load_into(self, fp, accessory):
        self.restore_state(accessory, json.load(fp))

I'm not sure if those are the best names, but hopefully get the idea.

@schinckel
Copy link
Contributor Author

In my case, I wanted to store/restore the associated accessories that were added to a bridge dynamically, and needed to rewrite the entire class - there was not even a simple way to use the existing AccessoryEncoder class.

@ikalchev
Copy link
Owner

Thanks for the feedback, I will try to think of something in the weekend and get back to you

@maximkulkin
Copy link
Contributor

It would be nice to allow accessories to expose their state and provide notifications on when this state changes so it can be persisted. I see that Accessory class implements getstate method, but I do not see it being used anywhere (directly or indirectly - can find any pickling or similar).

My use case is TV accessory providing "ConfiguredName" characteristics which is a way to assign custom names to your TV and input sources, and they are supposed to be persisted across restarts.

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