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

Conversation

ferbar
Copy link
Contributor

@ferbar 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.
@homeassistant
Copy link
Contributor

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!

@project-bot project-bot bot added this to Needs review in Dev Nov 30, 2019
@probot-home-assistant probot-home-assistant bot added integration: miflora small-pr PRs with less than 30 lines. labels Nov 30, 2019
@probot-home-assistant
Copy link

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use available property.

Signal valid data via available()
@ferbar
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 MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ChristianKuehnel
Copy link
Contributor

@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
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
Copy link
Contributor

ChristianKuehnel commented Dec 1, 2019 via email

@balloob balloob merged commit 1804c6e into home-assistant:dev Dec 2, 2019
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.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

6 participants