Skip to content

Easy way to detect if gas sensor is available #66

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

Closed
David-Hari opened this issue May 15, 2020 · 3 comments · Fixed by #74
Closed

Easy way to detect if gas sensor is available #66

David-Hari opened this issue May 15, 2020 · 3 comments · Fixed by #74
Assignees
Labels
enhancement New feature or request

Comments

@David-Hari
Copy link

David-Hari commented May 15, 2020

Hello,

I have both an Enviro (for inside) and an Enviro+ (for outside).

I am writing code to collect data from them. The same code will run on each device, with the only difference being that the Enviro+ also collects data from the gas sensor.

At the moment, I am using this rather nasty bit of code to detect if the gas sensor is available:

try:
    gas.read_all()
    hasGasSensor = True
except Exception:
    hasGasSensor = False

This works, however I am worried about what kind of exceptions might be thrown during read_all. Without the try/except, I get "OSError: [Errno 121] Remote I/O error" from the i2c code. I only hope that code is properly cleaning up after it fails to connect to the sensor, or that it doesn't modify anything.

It would be nice if the library provided a cleaner way to detect if the sensor is available. Something like an is_available method, which detects if the sensor is available/active before attempting to do anything with it.

I could be wrong, and my try/except method above is perfectly fine. But if that is the case then the library could just use that as the implementation of is_available.

@angelafevi95
Copy link

Hi Davi-Hari,

Were you able to solve the error mentioned without applying that exception?
I am unable to get any reading of gas.py example due to the same issue.

KR

@David-Hari
Copy link
Author

No, this is a feature request (of sorts).

I can do the check just fine using exception handling. I just find that a bit icky. It would be better if the library could provide a way to check if the sensor is attached without having to try and read from it.

@Gadgetoid Gadgetoid self-assigned this Jun 18, 2020
@Gadgetoid Gadgetoid added the enhancement New feature or request label Jun 18, 2020
@Gadgetoid
Copy link
Member

Insofar as I'm aware there's no way to check for the existence of an i2c device other than to attempt to read/write it- so the code you're using is correct. I agree that it would be worth having as a library feature, though.

Gadgetoid added a commit that referenced this issue Nov 2, 2021
Add available() method to gas sensor for #66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants