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

Add datetime object as valid StateType #52671

Merged
merged 13 commits into from
Nov 18, 2021
Merged

Add datetime object as valid StateType #52671

merged 13 commits into from
Nov 18, 2021

Conversation

frenck
Copy link
Member

@frenck frenck commented Jul 7, 2021

Breaking change

When using datetime and date device classes in sensors, the returned native value must (respectively) be a datetime or date Python object.

Returning an iso formatted date(time) string in these cases is now deprecated and will write a deprecation warning in the logs. This fallback/backward compatibility will be removed in Home Assistant 2022.2.

Proposed change

This PR makes a datetime object a valid native state return type for sensors.

We currently accept: None, str, int and float
This PR adds datetime and date to it.

It is converted as an ISO 8601 string. This prevents issues like #52666.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

Copy link
Contributor

@jjlawren jjlawren left a comment

Choose a reason for hiding this comment

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

Looks good!

@frenck frenck marked this pull request as draft November 4, 2021 20:17
@MartinHjelmare
Copy link
Member

We should probably run this by Paulus since he had some reservations for Joakim's PR.

@frenck
Copy link
Member Author

frenck commented Nov 10, 2021

Interesting find in Z-Wave JS

https://github.com/home-assistant/core/blob/dev/tests/components/zwave_js/fixtures/climate_heatit_z_trm3_no_value_state.json#L1084-L1103

FAILED tests/components/zwave_js/test_climate.py::test_thermostat_heatit_z_trm3_no_value[pyloop] - ValueError: Invalid date/datetime: sensor.floor_thermostat_time_3 provide state '3.2', while it has device class 'timestamp'

Seems like one of the fixtures exposes a bug in the sensors device class handling?

@frenck frenck mentioned this pull request Nov 17, 2021
22 tasks
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.

Looks good!

@frenck frenck merged commit 01efe1e into dev Nov 18, 2021
@frenck frenck deleted the frenck-2021-1962 branch November 18, 2021 13:11
@github-actions github-actions bot locked and limited conversation to collaborators Nov 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants