Schnitzel/hass-miner

Trigger to poll miner (after it's been offline)

Closed this issue · 26 comments

I use a smart switch to power the miner on only when I need it, since even in standby it uses a little bit of power. However (especially after a Home Assistant restart) the miner integration will not detect that the miner is awake.

I can work around the issue by reloading the miner integration, but it would be nice if there was some sort of action (that I can call using an automation) to check if the miner is awake.

(it would be better if the miner itself told home assistant it's awake, but presumably that requires a firmware change)

I'm not 100% certain of this, but its possible that pyasic or hass-miner is crashing due to the miner being completely offline. I'm guessing there would be some way to have the miner show as unavailable, rather than just have unknown values, and just poll it every so often in a safe way once it has been determined to be offline, that may be the best option.

Can you check your logs and see if there are any places that pyasic has raised an error, or anything that would seem to crash the integration when the miner is shut down?

That was a few weeks ago, I'll try to remember to look tomorrow when I plan to turn it off again.

Polling once every minute (or 5) would be fine, it's local network.

I'm indeed seeing some errors 4 minutes after cutting power from the miner (which I do 10 minutes after it stops mining).

Scherm­afbeelding 2024-01-22 om 10 25 09
Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:360
Integration: Sensor (documentation, issues)
First occurred: 5:14:15 AM (1 occurrences)
Last logged: 5:14:15 AM

Error while setting up miner platform for sensor
Traceback (most recent call last):
  File "/config/custom_components/miner/coordinator.py", line 50, in _async_update_data
    self.miner.username = miner_username
    ^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'username'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 300, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/miner/coordinator.py", line 72, in _async_update_data
    raise UpdateFailed("API Error") from err
homeassistant.helpers.update_coordinator.UpdateFailed: API Error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 360, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/miner/sensor.py", line 161, in async_setup_entry
    await coordinator.async_config_entry_first_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 272, in async_config_entry_first_refresh
    raise ex
homeassistant.exceptions.ConfigEntryNotReady: API Error

This is pretty much exactly what I expected, but slightly confusing. It seems like the miner is being re-queried at some point and is failing to be found. I'll have to figure out under what circumstances it re-queries, add a check for none, then let HASS know it is missing, and re-query every once in a while.

From my research, it seems like it should be polling the machine after this anyway, can you try out this release and see if it fixes the issue (as well as #294)?

https://github.com/b-rowan/hass-miner/releases/tag/v1.1.0b1

The number is a bit confusing. I'm currently running v1.1.0beta2

Different repo, sorry. I will update the name though to v1.1.1b1.

https://github.com/b-rowan/hass-miner

Mmm, there's no git on HASS so it's a bit of a pain to switch between repo's, but I'll try.

Anyway I was able to download the zip, extract on my own computer and then drag and drop it into the web based Visual Studio. Ridiculous but works :-)

Mmm, that didn't work...
Scherm­afbeelding 2024-01-22 om 19 18 19
Scherm­afbeelding 2024-01-22 om 19 15 13

This file has 0 bytes: https://github.com/b-rowan/hass-miner/archive/refs/tags/v1.1.1b1.tar.gz

Well then... Try now? I just deleted that release and re-added it, and it now has data in it.

Mmm, there's no git on HASS so it's a bit of a pain to switch between repo's, but I'll try.

You just have to delete the old repo and change the URL, then you can use it. It is definitely a bit annoying, even with hacs, and you may need to reboot to get it to work properly.

Indeed.

It seems that you can't just replace the code, you have to delete it and reinstall.

My guess is it has something to do with how HACS installs packages or something...

The re-install also cause the device to be re-added, with all the entities having _2 appended. But maybe I should have removed the device too during the upgrade. Anyway, I deleted the duplicates, started the miner, re-add it (also providing the new web username and password), then stopped it, cut power and restarted home assistant.

Then it starts like this:

Scherm­afbeelding 2024-01-22 om 19 24 15

That seems reasonable since I haven't turned the miner on yet in this session.

Once I give it power and it boots:

Scherm­afbeelding 2024-01-22 om 19 24 15

A minute later it's still disabled. Meanwhile I can login to the web interface. The miner automatically start mining which is annoying.

Once I reload the integration it shows "active" and I'm able to stop the miner.

Ok, still not polling properly then. It also seems like on the restart it clears and doesn't re-add the entities. I have an idea for this, let me test it here.

Try v1.1.1b2? I tried raising a ConfigEntryNotReady error in async setup entry, but I have no idea how this will work. I dont even have access to my HASS testing instance right now, so if this doesnt work ill have to try to fix it tonight.

Ok, I updated, cut power to the miner and restarted home assistant.

I then restored power to the miner. Ah, this time after about half a minute it showed as active! Nice.

Awesome! That's good to know, home assistant dev docs are quite confusing lol.

Ill do some testing of my own later, plus working on #294, and hopefully I can push something out.

Pushed out v1.1.0 (this repo), can you give it a test? I'm hoping the issues I've had with testing it are just on my end...

I installed the new release, added the miner back, turned off Active and cut the power. I then waited an hour, no crashes in the log.

I then switched the power on. The device immediately shows as active. After a few seconds the Active toggle is turned off. And then turns on again (correctly, because it started mining at boot).

Does it pick up as unavailable while it's offline?

No, it shows as active

Scherm­afbeelding 2024-01-25 om 17 25 53

I made a stupid mistake in the coordinator, I was only updating the miner if it was None, which would never pick up as being "offline". I have pushed a pre-release version (1.1.1rc1), can you try that out?

After the update, while the miner was powered off, it complains:

Scherm­afbeelding 2024-01-25 om 18 43 04

I manually reloaded the integration after switching the power on.

I then deactived the miner, and cut power. After that the control is greyed out, which is nice!

Scherm­afbeelding 2024-01-25 om 17 25 53

When turn it back on it initially stays greyed out, but in less than half a minute it shows as active. Nice!