Skip to content
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

Clear miflora sensor state on exception #29276

Merged
merged 2 commits into from Dec 2, 2019
Merged

Clear miflora sensor state on exception #29276

merged 2 commits into from Dec 2, 2019

Conversation

@ferbar
Copy link
Contributor

ferbar commented Nov 30, 2019

Breaking Change:

Values for a unresponsive device aren't reported any more as if they were valid, so it's possible to send an alert.

Description:

Report avaiable() = False if querying the device fails. The state is then set to unavailable, so it can be tracked if a miflora device isn't responding any more.

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

Example entry for automations.yaml:

- id: plant_unreachable
  alias: plant unreachable
  trigger:
  - entity_id: sensor.plant_moisture
    for: 01:00:00
    platform: state
    to: unavailable
  action:
...

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

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

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

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

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

Updates:
changed unknown to unavailable in automations.yaml

Clear state if querying the device fails. The state is then set to unknown, so it can be tracked if a miflora device isn't responding any more.
@ferbar ferbar requested a review from Danielhiversen as a code owner Nov 30, 2019
@homeassistant

This comment has been minimized.

Copy link
Contributor

homeassistant commented Nov 30, 2019

Hi @ferbar,

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!

@probot-home-assistant

This comment has been minimized.

Copy link

probot-home-assistant bot commented Nov 30, 2019

Hey there @Danielhiversen, @ChristianKuehnel, mind taking a look at this pull request as its been labeled with a integration (miflora) you are listed as a codeowner for? Thanks!

@@ -158,9 +158,11 @@ def update(self):
data = self.poller.parameter_value(self.parameter)
except OSError as ioerr:
_LOGGER.info("Polling error %s", ioerr)
self._state = None

This comment has been minimized.

Copy link
@balloob

balloob Dec 1, 2019

Member

Instead of self._state = None, we should return False from the available property.

@@ -158,9 +158,11 @@ def update(self):
data = self.poller.parameter_value(self.parameter)
except OSError as ioerr:

This comment has been minimized.

Copy link
@balloob

balloob Dec 1, 2019

Member

You can combine the except clause.

Suggested change
except OSError as ioerr:
except (OSError, BluetoothBackendException) as ioerr:
Dev automation moved this from Needs review to Review in progress Dec 1, 2019
Copy link
Member

balloob left a comment

Use available property.

Signal valid data via available()
@ferbar

This comment has been minimized.

Copy link
Contributor Author

ferbar commented Dec 1, 2019

I think "available()" and "except merge" should be changed in mitemp_bt, too. (?)

  • tested & works
@MartinHjelmare MartinHjelmare changed the title Miflora: Clear sensor state on exception Clear miflora sensor state on exception Dec 1, 2019
Copy link
Member

MartinHjelmare left a comment

Good! Please update the PR description according to latest changes. Then we can merge.

@ChristianKuehnel

This comment has been minimized.

Copy link
Contributor

ChristianKuehnel commented Dec 1, 2019

@ferbar: Great idea!

We have quite a few users who's sensors are at the edge of what's feasible with regards to the range of the Bluetooth devices. If you fail on the first exception, this will cause a lot of errors, as the Bluetooth communication is quite unreliable. So they would get tons of errors this way.

I would recommend something like: "fail if no new data for x hours", where x can be configured.

Rationale:
I do not care about every missed measurement and the data changes quite slowly. It's good enough if the moisture/fertilizer values are "new enough". For me, once a day is good enough. But that might differ from user to user.

@ferbar

This comment has been minimized.

Copy link
Contributor Author

ferbar commented Dec 1, 2019

@ChristianKuehnel: I'm with you. I was playing around with last_updated and last_changed before. However last_updated is changed even if update() was unsuccessful. I think that a timeout for values to get unavailable should be implemented in Entity or a derived class since this would affect many types of sensors and would produce a lot of copy&paste otherwise.

  • I didn't find out why last_updated is changed (I was searching for a long time)
  • I tried to override last_update with device_state_attributes. Worked, but I think this is really ugly 😄 IMHO the current solution is better.
  • I don't like to track an additional last_update timestamp.

Do you have any idea how to prevent last_updated get updated in an error case?

@ChristianKuehnel

This comment has been minimized.

Copy link
Contributor

ChristianKuehnel commented Dec 1, 2019

@balloob balloob merged commit 1804c6e into home-assistant:dev Dec 2, 2019
11 checks passed
11 checks passed
CI #20191201.21 succeeded
Details
CI (FullCheck Mypy) FullCheck Mypy succeeded
Details
CI (FullCheck Pylint) FullCheck Pylint succeeded
Details
CI (Overview CheckFormat) Overview CheckFormat succeeded
Details
CI (Overview Lint) Overview Lint succeeded
Details
CI (Overview Validate) Overview Validate succeeded
Details
CI (Tests PyTest Python36) Tests PyTest Python36 succeeded
Details
CI (Tests PyTest Python37) Tests PyTest Python37 succeeded
Details
cla-bot Everyone involved has signed the CLA
codecov/patch Coverage not affected when comparing bde453b...dc6f3d0
Details
codecov/project 94.44% (target 90%)
Details
Dev automation moved this from Review in progress to Done Dec 2, 2019
@lock lock bot locked and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.