Skip to content

Replace Climate HVAC_MODE_* constants with HVACMode enum#70286

Merged
frenck merged 7 commits intodevfrom
frenck-2022-0921
Apr 20, 2022
Merged

Replace Climate HVAC_MODE_* constants with HVACMode enum#70286
frenck merged 7 commits intodevfrom
frenck-2022-0921

Conversation

@frenck
Copy link
Copy Markdown
Member

@frenck frenck commented Apr 19, 2022

Breaking change

Developers only: All HVAC_MODE_* constants from the Climate platform, have been deprecated. Use the new HVACMode enum instead.

Proposed change

See above.

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

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.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • 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:

@probot-home-assistant
Copy link
Copy Markdown

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (demo) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-home-assistant
Copy link
Copy Markdown

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (climate) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@frenck frenck marked this pull request as ready for review April 19, 2022 17:11
@frenck frenck requested a review from a team as a code owner April 19, 2022 17:11
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Apr 20, 2022

Looks like there are quite a few that expect None aka STATE_UNKNOWN is a valid state.

I can think of two solutions that don't involve changing all the legacy right now:

--- a/homeassistant/components/climate/__init__.py
+++ b/homeassistant/components/climate/__init__.py
@@ -211,8 +211,10 @@ class ClimateEntity(Entity):
 
     @final
     @property
-    def state(self) -> str:
+    def state(self) -> str | None:
         """Return the current state."""
+        if self.hvac_mode is None:
+            return None
         if not isinstance(self.hvac_mode, HVACMode):
             return HVACMode(self.hvac_mode).value
         return self.hvac_mode.value
--- a/homeassistant/components/climate/__init__.py
+++ b/homeassistant/components/climate/__init__.py
@@ -9,6 +9,7 @@ from typing import Any, final
 
 import voluptuous as vol
 
+from homeassistant.helpers.frame import report
 from homeassistant.config_entries import ConfigEntry
 from homeassistant.const import (
     ATTR_TEMPERATURE,
@@ -208,11 +209,20 @@ class ClimateEntity(Entity):
     _attr_target_temperature_step: float | None = None
     _attr_target_temperature: float | None = None
     _attr_temperature_unit: str
+    _reported_invalid_state = False
 
     @final
     @property
-    def state(self) -> str:
+    def state(self) -> str | None:
         """Return the current state."""
+        if self.hvac_mode is None:
+            if not self._reported_invalid_state:
+                self._reported_invalid_state = True
+                report(
+                    "Provided an HVACMode of 'None'; This will raise a ValueError exception in 2022.10 and later.",
+                    exclude_integrations={"climate"},
+                )
+            return None
         if not isinstance(self.hvac_mode, HVACMode):
             return HVACMode(self.hvac_mode).value
         return self.hvac_mode.value

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Apr 20, 2022

The changes look good 👍

Resolving the None case seems like the sticking point here.

@MartinHjelmare
Copy link
Copy Markdown
Member

I think we should allow returning None to represent unknown state. It makes it consistent with almost all other entities.

@frenck
Copy link
Copy Markdown
Member Author

frenck commented Apr 20, 2022

I think we should allow returning None to represent unknown state. It makes it consistent with almost all other entities.

Agreed. Will adjust to that.

@MartinHjelmare
Copy link
Copy Markdown
Member

Does the frontend handle unknown state ok in the climate card?

@frenck
Copy link
Copy Markdown
Member Author

frenck commented Apr 20, 2022

Does the frontend handle unknown state ok in the climate card?

It does that perfectly actually, when the state becomes None (unknown) the UI disables the card:

image

Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

Nice!

@frenck frenck requested a review from emontnemery as a code owner April 20, 2022 09:17
@frenck frenck merged commit a22f361 into dev Apr 20, 2022
@frenck frenck deleted the frenck-2022-0921 branch April 20, 2022 11:20
@github-actions github-actions bot locked and limited conversation to collaborators Apr 21, 2022
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.

4 participants