Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
HomeAssistant
Core
Commits
c87f2027
Unverified
Commit
c87f2027
authored
1 year ago
by
Michael
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Use check_valid_float helper in significant change support of sensor and weather (#106013)
parent
a1614d6b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
homeassistant/components/sensor/significant_change.py
+7
-9
7 additions, 9 deletions
homeassistant/components/sensor/significant_change.py
homeassistant/components/weather/significant_change.py
+6
-12
6 additions, 12 deletions
homeassistant/components/weather/significant_change.py
with
13 additions
and
21 deletions
homeassistant/components/sensor/significant_change.py
+
7
−
9
View file @
c87f2027
...
...
@@ -12,6 +12,7 @@ from homeassistant.core import HomeAssistant, callback
from
homeassistant.helpers.significant_change
import
(
check_absolute_change
,
check_percentage_change
,
check_valid_float
,
)
from
.
import
SensorDeviceClass
...
...
@@ -63,23 +64,20 @@ def async_check_significant_change(
absolute_change
=
1.0
percentage_change
=
2.0
try
:
if
not
check_valid_float
(
new_state
)
:
# New state is invalid, don't report it
new_state_f
=
float
(
new_state
)
except
ValueError
:
return
False
try
:
if
not
check_valid_float
(
old_state
)
:
# Old state was invalid, we should report again
old_state_f
=
float
(
old_state
)
except
ValueError
:
return
True
if
absolute_change
is
not
None
and
percentage_change
is
not
None
:
return
_absolute_and_relative_change
(
old_state
_f
,
new_state
_f
,
absolute_change
,
percentage_change
float
(
old_state
),
float
(
new_state
)
,
absolute_change
,
percentage_change
)
if
absolute_change
is
not
None
:
return
check_absolute_change
(
old_state_f
,
new_state_f
,
absolute_change
)
return
check_absolute_change
(
float
(
old_state
),
float
(
new_state
),
absolute_change
)
return
None
This diff is collapsed.
Click to expand it.
homeassistant/components/weather/significant_change.py
+
6
−
12
View file @
c87f2027
...
...
@@ -5,7 +5,10 @@ from typing import Any
from
homeassistant.const
import
UnitOfPressure
,
UnitOfSpeed
,
UnitOfTemperature
from
homeassistant.core
import
HomeAssistant
,
callback
from
homeassistant.helpers.significant_change
import
check_absolute_change
from
homeassistant.helpers.significant_change
import
(
check_absolute_change
,
check_valid_float
,
)
from
.const
import
(
ATTR_WEATHER_APPARENT_TEMPERATURE
,
...
...
@@ -60,15 +63,6 @@ VALID_CARDINAL_DIRECTIONS: list[str] = [
]
def
_check_valid_float
(
value
:
str
|
int
|
float
)
->
bool
:
"""
Check if given value is a valid float.
"""
try
:
float
(
value
)
except
ValueError
:
return
False
return
True
def
_cardinal_to_degrees
(
value
:
str
|
int
|
float
|
None
)
->
int
|
float
|
None
:
"""
Translate a cardinal direction into azimuth angle (degrees).
"""
if
not
isinstance
(
value
,
str
):
...
...
@@ -109,11 +103,11 @@ def async_check_significant_change(
old_attr_value
=
_cardinal_to_degrees
(
old_attr_value
)
new_attr_value
=
_cardinal_to_degrees
(
new_attr_value
)
if
new_attr_value
is
None
or
not
_
check_valid_float
(
new_attr_value
):
if
new_attr_value
is
None
or
not
check_valid_float
(
new_attr_value
):
# New attribute value is invalid, ignore it
continue
if
old_attr_value
is
None
or
not
_
check_valid_float
(
old_attr_value
):
if
old_attr_value
is
None
or
not
check_valid_float
(
old_attr_value
):
# Old attribute value was invalid, we should report again
return
True
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment