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
9afabc17
Unverified
Commit
9afabc17
authored
2 years ago
by
epenet
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Use attr in mqtt sensor (#81402)
parent
d385a85c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homeassistant/components/mqtt/sensor.py
+10
-27
10 additions, 27 deletions
homeassistant/components/mqtt/sensor.py
with
10 additions
and
27 deletions
homeassistant/components/mqtt/sensor.py
+
10
−
27
View file @
9afabc17
"""
Support for MQTT sensors.
"""
from
__future__
import
annotations
from
datetime
import
datetime
,
timedelta
from
datetime
import
timedelta
import
functools
import
logging
...
...
@@ -30,7 +30,7 @@ from homeassistant.core import HomeAssistant, callback
import
homeassistant.helpers.config_validation
as
cv
from
homeassistant.helpers.entity_platform
import
AddEntitiesCallback
from
homeassistant.helpers.event
import
async_track_point_in_utc_time
from
homeassistant.helpers.typing
import
ConfigType
,
DiscoveryInfoType
,
StateType
from
homeassistant.helpers.typing
import
ConfigType
,
DiscoveryInfoType
from
homeassistant.util
import
dt
as
dt_util
from
.
import
subscription
...
...
@@ -171,7 +171,6 @@ class MqttSensor(MqttEntity, RestoreSensor):
def
__init__
(
self
,
hass
,
config
,
config_entry
,
discovery_data
):
"""
Initialize the sensor.
"""
self
.
_state
=
None
self
.
_expiration_trigger
=
None
expire_after
=
config
.
get
(
CONF_EXPIRE_AFTER
)
...
...
@@ -201,7 +200,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
_LOGGER
.
debug
(
"
Skip state recovery after reload for %s
"
,
self
.
entity_id
)
return
self
.
_expired
=
False
self
.
_
st
ate
=
last_sensor_data
.
native_value
self
.
_at
tr_native_valu
e
=
last_sensor_data
.
native_value
self
.
_expiration_trigger
=
async_track_point_in_utc_time
(
self
.
hass
,
self
.
_value_is_expired
,
expiration_at
...
...
@@ -227,9 +226,13 @@ class MqttSensor(MqttEntity, RestoreSensor):
"""
Return the config schema.
"""
return
DISCOVERY_SCHEMA
def
_setup_from_config
(
self
,
config
)
:
def
_setup_from_config
(
self
,
config
:
ConfigType
)
->
None
:
"""
(Re)Setup the entity.
"""
self
.
_attr_device_class
=
config
.
get
(
CONF_DEVICE_CLASS
)
self
.
_attr_force_update
=
config
[
CONF_FORCE_UPDATE
]
self
.
_attr_native_unit_of_measurement
=
config
.
get
(
CONF_UNIT_OF_MEASUREMENT
)
self
.
_attr_state_class
=
config
.
get
(
CONF_STATE_CLASS
)
self
.
_template
=
MqttValueTemplate
(
self
.
_config
.
get
(
CONF_VALUE_TEMPLATE
),
entity
=
self
).
async_render_with_possible_json_value
...
...
@@ -259,7 +262,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
self
.
hass
,
self
.
_value_is_expired
,
expiration_at
)
payload
=
self
.
_template
(
msg
.
payload
,
default
=
self
.
_stat
e
)
payload
=
self
.
_template
(
msg
.
payload
,
default
=
self
.
native_valu
e
)
if
payload
is
not
None
and
self
.
device_class
in
(
SensorDeviceClass
.
DATE
,
...
...
@@ -272,7 +275,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
elif
self
.
device_class
==
SensorDeviceClass
.
DATE
:
payload
=
payload
.
date
()
self
.
_
st
ate
=
payload
self
.
_at
tr_native_valu
e
=
payload
def
_update_last_reset
(
msg
):
payload
=
self
.
_last_reset_template
(
msg
.
payload
)
...
...
@@ -342,26 +345,6 @@ class MqttSensor(MqttEntity, RestoreSensor):
self
.
_expired
=
True
self
.
async_write_ha_state
()
@property
def
native_unit_of_measurement
(
self
)
->
str
|
None
:
"""
Return the unit this state is expressed in.
"""
return
self
.
_config
.
get
(
CONF_UNIT_OF_MEASUREMENT
)
@property
def
native_value
(
self
)
->
StateType
|
datetime
:
"""
Return the state of the entity.
"""
return
self
.
_state
@property
def
device_class
(
self
)
->
str
|
None
:
"""
Return the device class of the sensor.
"""
return
self
.
_config
.
get
(
CONF_DEVICE_CLASS
)
@property
def
state_class
(
self
)
->
str
|
None
:
"""
Return the state class of the sensor.
"""
return
self
.
_config
.
get
(
CONF_STATE_CLASS
)
@property
def
available
(
self
)
->
bool
:
"""
Return true if the device is available and value has not expired.
"""
...
...
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