TomFaulkner / SenseMe

Python Library for Haiku SenseMe app controlled fans/lights
GNU General Public License v3.0
21 stars 10 forks source link

Update cache when properties are changed #23

Closed mikelawrence closed 6 years ago

mikelawrence commented 6 years ago

When monitoring is enabled the cache is used to speed up status requests via get_attribute(). However when a property (i.e. brightness) is changed the cache is not updated until the next status update from the fan which could be 45 seconds later.

An example of why this is needed is Home Assistant automation software. It separates the command path from the status path. So when I turn on the fan in Home Assistant it takes upwards of 45 seconds before the on state of the fan is accurately reflected in Home Assistant.

I propose that the cache be updated along with any successful property change. Below is an example of changes to your brightness setter property:

    @brightness.setter
    def brightness(self, light):
        if light > 16:
            light = 16
        elif light < 0:
            light = 0
        self._send_command('<%s;LIGHT;LEVEL;SET;%s>' % (self.name, light))
        # put new brightness in cache if monitoring
        if self._monitoring and self._all_cache:
            self._all_cache['LIGHT;LEVEL;ACTUAL'] = str(light)
            # changes to brightness also changes light power
            if light > 0:
                self._all_cache['LIGHT;PWR'] = 'ON'
            else:
                self._all_cache['LIGHT;PWR'] = 'OFF'

I have made these changes already. Would like me to issue a Pull Request? I have never done a PR and may need some help.

TomFaulkner commented 6 years ago

I hadn't thought about this issue when I added the monitoring. I would be glad to take a PR. However, if we add your example here to every function it gets very long and a lot of repeated code. An internal functional _update_cache seems like it would be a better approach.

A PR would be appreciated.

mikelawrence commented 6 years ago

I believe this issue was fixed when you merged my PR #26.