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

Aqara switch #21

Closed
joaoasilva opened this issue Feb 3, 2017 · 5 comments
Closed

Aqara switch #21

joaoasilva opened this issue Feb 3, 2017 · 5 comments

Comments

@joaoasilva
Copy link

joaoasilva commented Feb 3, 2017

Aqara switches should be as the light switches, instead when you click on it on the interface it turns on and then off automatically on the interface but stays on on the physical switch.
I've made a dirty fix on the switch code:

def turn_on(self, **kwargs):
    """Turn the switch on."""
    if self._data_key == 'status':
        if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, 'on'):
            self._state = True
    else:
        if self._state:
            self.turn_off()
        else:
            if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, 'on'):
                self._state = True

But this isn't the solution as they need to behave as a light switch and not toggle automatically on the interface.

Regards,

@joaoasilva
Copy link
Author

def turn_on(self, **kwargs):
    """Turn the switch on."""
    if self._data_key == 'status':
        if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, 'on'):
            self._state = True
    else:
        if self._state:
            self.turn_off()
        else:
            if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, 'on'):
                self._state = True

def turn_off(self):
    """Turn the switch off."""
    if self._data_key == 'status':
        if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, 'off'):
            self._state = False
    else:
        if not self._state:
            self.turn_on()
        else:
            if self.xiaomi_hub.write_to_hub(self._sid, self._data_key, 'off'):
                self._state = False
        
def parse_data(self, data):
    value = data.get(self._data_key)
    if value is None:
        return False
        
    if self._state:
        ztate = 'True'
    else:
        ztate = 'False'

    _LOGGER.error('value:' + value + ' state:' + ztate)
    state = value == 'on'
    if self._data_key == 'status':
        if self._state == state:
            return False
        else:
            self._state = state
            return True
    else:
        if state:
            return True
        else:
            return False

This is the maximum I could go... I never worked with python or know anything about the HA framework so I'm totally blind here.
With that at least it works but the icon colour only changes to the right state the first time I click on it, afterwards it doesn't turn off when I click on the switch again but the physical switch at least it works.
If someone with more experience can spot what's missing I would appreciate.

Thanks.

Regards,

@tam-wh
Copy link
Contributor

tam-wh commented Feb 3, 2017

I'll check the latest code and see if something breaks. The old code I'm using works fine

@Danielhiversen
Copy link
Contributor

Danielhiversen commented Feb 4, 2017

@joaoasilva Why do you have a call to turn_off in the turn_on function?
When should a turn_on call turn the switch off?

@Danielhiversen
Copy link
Contributor

@joaoasilva Could you try this fix: https://github.com/lazcad/homeassistant/pull/22/files

@joaoasilva
Copy link
Author

Ahahaha, this was my first experience with Python and HA, as I said was a dirty hack just to make it work, not a solution.
I can confirm your fix works!
Thanks!

tam-wh added a commit that referenced this issue Feb 4, 2017
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