Skip to content

Add aqara lock support#14419

Merged
syssi merged 7 commits intohome-assistant:devfrom
SchumyHao:dev
May 22, 2018
Merged

Add aqara lock support#14419
syssi merged 7 commits intohome-assistant:devfrom
SchumyHao:dev

Conversation

@SchumyHao
Copy link
Copy Markdown
Contributor

@SchumyHao SchumyHao commented May 12, 2018

Signed-off-by: SchumyHao schumyhaojl@126.com

Description:

Add aqara lock support

Related issue (if applicable): fixes #

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (85 > 79 characters)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statement ends with a semicolon

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected 2 blank lines, found 1

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @SchumyHao,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line over-indented for visual indent

@syssi
Copy link
Copy Markdown
Member

syssi commented May 12, 2018

Please fix the lint errors. I'm very happy with your approach!

@syssi
Copy link
Copy Markdown
Member

syssi commented May 12, 2018

I've released PyXiaomiGateway 0.9.2. Please update the requirements!

@SchumyHao
Copy link
Copy Markdown
Contributor Author

Hi @syssi
I have update the requirement.
Thank you for your review.

@syssi
Copy link
Copy Markdown
Member

syssi commented May 12, 2018

requirements_all.txt is not up to date
Please run script/gen_requirements_all.py

@syssi syssi closed this May 12, 2018
@syssi syssi reopened this May 12, 2018
@fabaff fabaff changed the title xiaomi_aqara: add aqara lock support Add aqara lock support May 12, 2018
SchumyHao added 4 commits May 13, 2018 09:31
Signed-off-by: SchumyHao <schumyhaojl@126.com>
Signed-off-by: SchumyHao <schumyhaojl@126.com>
Signed-off-by: SchumyHao <schumyhaojl@126.com>
Aqara lock can not read lock state.
But After verify successfully, lock will be unlock state
for about 5s.
After 5s, lock will auto reset to lock state.

Signed-off-by: SchumyHao <schumyhaojl@126.com>
Copy link
Copy Markdown
Member

@syssi syssi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

self._changed_by = int(value)
self._verified_wrong_times = 0
self._state = STATE_UNLOCKED
async_call_later(self.hass, UNLOCK_MAINTAIN_TIME_S,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the unit (_S).

}
return attributes

@asyncio.coroutine
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a callback instead via the @callback decorator that can be imported from core.py.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you care about this one, too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure for this comment.
This function will be invoked in async_call_later, I need this function called after UNLOCK_MAINTAIN_TIME seconds. but callback function can not do IO operation, I think delay should not called in callback too.

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will be run with async_run_job. It's safe to run callbacks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to change this by using async_run_job, I tested this can work:

@@ -67,8 +68,10 @@ class XiaomiAqaraLock(LockDevice, XiaomiDevice):
         return attributes
 
     @asyncio.coroutine
-    def clear_unlock_state(self, _):
+    def clear_unlock_state(self):
         """Clear unlock state automatically."""
+        yield from asyncio.sleep(UNLOCK_MAINTAIN_TIME)
+
         self._state = STATE_LOCKED
         self.async_schedule_update_ha_state()
 
@@ -85,8 +88,7 @@ class XiaomiAqaraLock(LockDevice, XiaomiDevice):
                 self._changed_by = int(value)
                 self._verified_wrong_times = 0
                 self._state = STATE_UNLOCKED
-                async_call_later(self.hass, UNLOCK_MAINTAIN_TIME,
-                                 self.clear_unlock_state)
+                self.hass.async_run_job(self.clear_unlock_state)
                 return True
 
         return False

But I failed to using @callback
I have tried this

@@ -66,9 +67,12 @@ class XiaomiAqaraLock(LockDevice, XiaomiDevice):
         }
         return attributes
 
+    @callback
     @asyncio.coroutine
-    def clear_unlock_state(self, _):
+    def clear_unlock_state(self):
         """Clear unlock state automatically."""
+        yield from asyncio.sleep(UNLOCK_MAINTAIN_TIME)
+
         self._state = STATE_LOCKED
         self.async_schedule_update_ha_state()
 
@@ -85,8 +89,7 @@ class XiaomiAqaraLock(LockDevice, XiaomiDevice):
                 self._changed_by = int(value)
                 self._verified_wrong_times = 0
                 self._state = STATE_UNLOCKED
-                async_call_later(self.hass, UNLOCK_MAINTAIN_TIME,
-                                 self.clear_unlock_state)
+                self.hass.async_run_job(self.clear_unlock_state)
                 return True
 

But clear_unlock_state(self) will not invoked.

I'm sorry that how to use callback decorator, can you point me out how to do this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MartinHjelmare @syssi
May you help me to improve this code?
I have checked the code in homeassistant, which used async_call_later and callback, but do not find a similar usage in my situation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just change this line to @callback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
Thank you for your advice

UNLOCK_MAINTAIN_TIME_S = 5


def setup_platform(hass, config, add_devices, discovery_info=None):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be made a coroutine via async def to make the call to async_call_later in parse_data, which will be called during entity init, work. I assume the rest of the calls in the base entity XiaomiDevice can be done safely from an async context?

SchumyHao added 3 commits May 13, 2018 22:37
Signed-off-by: SchumyHao <schumyhaojl@126.com>
Signed-off-by: SchumyHao <schumyhaojl@126.com>
Signed-off-by: SchumyHao <schumyhaojl@126.com>
@syssi
Copy link
Copy Markdown
Member

syssi commented May 16, 2018

Could you add some docs?

@SchumyHao
Copy link
Copy Markdown
Contributor Author

OK, I'll do this job.
I have forked https://github.com/SchumyHao/home-assistant.github.io I'll create a PR later.

@syssi
Copy link
Copy Markdown
Member

syssi commented May 17, 2018

I added some basic docs: home-assistant/home-assistant.io#5387

@SchumyHao
Copy link
Copy Markdown
Contributor Author

Thank you @syssi
I add some more explanation on your PR

@syssi syssi mentioned this pull request May 21, 2018
7 tasks
@SchumyHao
Copy link
Copy Markdown
Contributor Author

I'm not sure what should I do in next step?
@syssi Can you show me what should I do to push this PR merged?

@syssi
Copy link
Copy Markdown
Member

syssi commented May 22, 2018

I'm waiting for an review/approval of the docs. As far as the docs are approved I will merge this PR.

@SchumyHao
Copy link
Copy Markdown
Contributor Author

Thank you @syssi. Please let me know if I need do something more.

@syssi syssi merged commit 82770fa into home-assistant:dev May 22, 2018
@balloob balloob mentioned this pull request Jun 8, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants