I’m fairly new to Home Assistant, but I’m still using the Vera for my Zwave switches.
Things work well for the most part, but I’ve found that after a power outage, when the power comes back on, none of my Vera devices get added, and consequently none of my automations will run until I reset Home Assistant. This seems to be because the Vera component only detects and adds devices once when Home Assistant starts up.
I could get around this by writing some code that delays the startup of the Hass systemd service until the Vera is up and running, but that feels a little ugly to me, especially if there’s something wrong with the Vera and it never boots up.
Is there any way around this? Like, have the Vera component periodically poll the Vera in the background and add the devices when it’s detected?
Are all components limited to discovering devices only at startup, or is there a mechanism for writing components that poll and add devices whenever they appear? I’m wondering if it’s possible to modify the Vera component to work this way.
I’d like to know if there’s a way to add/remove (mainly add, as removing has potential issues) entities at runtime for a component. I have a custom component that could benefit from adding at runtime but I haven’t found how to do it yet.
However, I do know a lot of effort is going into reworking discovery, so it may be possible in the future as part of that if it isn’t possible currently. I haven’t looked into using discovery since I personally have it disabled.
I made an automation to restart home Assistant if it couldn’t discover vera or MQTT on the startup. Not sure if you want to go that route, but it still handles it pretty fast
I used one of the binary sensors it has , and did a “not is_state” for the on and off possibililities, so if it’s not detected or showing as “unknown” it’ll restart. I’m sure you can do the same for lights/locks/anything with a set number of possible states
I spent some time digging around the Hass source, and figured some things out.
It does seem to be possible to add devices at runtime. The ‘discovery’ component does exactly that, running a timer every 5 minutes and adding new devices that are discovered. I haven’t looked into how to remove devices at runtime.
Vera is handled through its own component, so it doesn’t benefit from the discovery component–instead the Vera component simply queries the Vera URL 1 time in the initial component setup. I hacked together a monkey patch that changes the Vera component’s setup() method to keep retrying on an increasing timer as long as the query keeps failing, and it seems to work. Here’s a summary of what I did:
Created a custom component custom_components/vera_patcher.py which overwrites homeassistant.components.vera.setup on import, like I described–retrying the Vera queries on a timer, and calling discovery.load_platform() when it finally succeeds. There’s also a dummy vera_patcher.setup() method which does nothing. I don’t know if there’s a better alternative than creating a custom component–I just wanted to have my code run before the Vera component is loaded.
In my config yaml, declare the vera_patcher component before the vera component:
I could also have just copied the entire builtin Vera component code to /custom_components/vera (which would override the builtin one) and modified the setup() method, which would remove the need to change the config yaml.
I actually like the simplicity of mrneilix’s solution too, I’d just be worried about hass constantly restarting if something is wrong with the Vera and it never boots up. That’s never happened as far as I know though so maybe it’s a non-issue.
This is cool. I’ve extended functionality of core classes for custom components but never thought to completely replace core methods. I might have to play around with this and see what’s possible.
Of course, the recommended approach is probably, like you said, to just copy the entire Vera component and modify the setup method there. But this option is still pretty neat.