Fix async IO in Sesame lock component.#11054
Conversation
| def setup_platform(hass, config: ConfigType, | ||
| add_devices: Callable[[list], None], discovery_info=None): | ||
| @asyncio.coroutine | ||
| def async_setup_platform( |
There was a problem hiding this comment.
Don't switch to async_setup_platform. This isn't an async platform. Adding update_before_add=True should be the only necessary change.
There was a problem hiding this comment.
@armills. Got it, so this would only be needed if the actual update methods (and the underlying update in pysesame) was async, correct?
There was a problem hiding this comment.
Exactly. You'd need to switch to the async equivalents if you had other async coroutines you needed to call from setup/update on the external lib.
balloob
left a comment
There was a problem hiding this comment.
This won't work as properties are always evaluated in an asyncio context. The correct fix is to assign all properties that we want to read from self._sesame to instance variables inside the update method.
|
@balloob I'm not sure what won't work. Pysesame defines the properties like this: @property
def nickname(self):
"""Return the Sesame nickname."""
if not self.use_cached_state:
self.update_state(False)
return self._nicknameSo since I'm adding I'm happy to change to caching all of the pysesame properties inside the HA Sesame object, I just want to make sure that I understand why I'm doing it (my first commit to the project an all 😄). |
|
It would be best as @balloob said to use the update method to cache everything into the home assistant class. Although this does correct the problem, it's cleanest not to depend on the upstream behavior of sometimes blocks sometimes returns immediately. |
|
Sounds good. Thanks for the feedback. I'll make the changes. |
emlove
left a comment
There was a problem hiding this comment.
One small request, otherwise looks great!
| _sesame = None | ||
|
|
||
| # Cached sesame properties | ||
| _device_id = None |
There was a problem hiding this comment.
Declare these inside of __init__ instead of here. Variables declared under the class are static for all instances of the class. We can get rid of the _sesame = None above as well. Should just look like this in __init__:
self._sesame = sesame
self._device_id = None
self._nickname = None
...* Call update on Sesame devices to cache initial state * Switch to using async_add_devices * Fix line length * Fix Lint errors * Fix more Lint errors * Cache pysesame properties * Updates from CR feedback
Description:
Switch Sesame lock component to use async_add_devices and force the object to cache all it's properties. Unless someone manually overrides the value of use_cached_state on the internal sesame instance there will be no async IO in the component properties.
Checklist:
If the code communicates with devices, web services, or third-party tools:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable ([example][ex-requir]).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.