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
8da85d7a
Commit
8da85d7a
authored
8 years ago
by
Heiko Rothe
Committed by
Paulus Schoutsen
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Added away timeout setting for idle devices (#3321)
parent
ac5647a3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homeassistant/components/sensor/mqtt_room.py
+23
-10
23 additions, 10 deletions
homeassistant/components/sensor/mqtt_room.py
with
23 additions
and
10 deletions
homeassistant/components/sensor/mqtt_room.py
+
23
−
10
View file @
8da85d7a
...
@@ -6,13 +6,14 @@ https://home-assistant.io/components/sensor.mqtt_room/
...
@@ -6,13 +6,14 @@ https://home-assistant.io/components/sensor.mqtt_room/
"""
"""
import
logging
import
logging
import
json
import
json
from
datetime
import
timedelta
import
voluptuous
as
vol
import
voluptuous
as
vol
import
homeassistant.components.mqtt
as
mqtt
import
homeassistant.components.mqtt
as
mqtt
from
homeassistant.components.sensor
import
PLATFORM_SCHEMA
from
homeassistant.components.sensor
import
PLATFORM_SCHEMA
from
homeassistant.const
import
(
from
homeassistant.const
import
(
CONF_NAME
,
STATE_UNKNOWN
,
CONF_TIMEOUT
)
CONF_NAME
,
CONF_TIMEOUT
)
from
homeassistant.components.mqtt
import
CONF_STATE_TOPIC
from
homeassistant.components.mqtt
import
CONF_STATE_TOPIC
import
homeassistant.helpers.config_validation
as
cv
import
homeassistant.helpers.config_validation
as
cv
from
homeassistant.helpers.entity
import
Entity
from
homeassistant.helpers.entity
import
Entity
...
@@ -29,16 +30,21 @@ ATTR_ROOM = 'room'
...
@@ -29,16 +30,21 @@ ATTR_ROOM = 'room'
CONF_DEVICE_ID
=
'
device_id
'
CONF_DEVICE_ID
=
'
device_id
'
CONF_ROOM
=
'
room
'
CONF_ROOM
=
'
room
'
CONF_AWAY_TIMEOUT
=
'
away_timeout
'
DEFAULT_NAME
=
'
Room Sensor
'
DEFAULT_NAME
=
'
Room Sensor
'
DEFAULT_TIMEOUT
=
5
DEFAULT_TIMEOUT
=
5
DEFAULT_AWAY_TIMEOUT
=
0
DEFAULT_TOPIC
=
'
room_presence
'
DEFAULT_TOPIC
=
'
room_presence
'
DEPENDENCIES
=
[
'
mqtt
'
]
STATE_AWAY
=
'
away
'
PLATFORM_SCHEMA
=
PLATFORM_SCHEMA
.
extend
({
PLATFORM_SCHEMA
=
PLATFORM_SCHEMA
.
extend
({
vol
.
Required
(
CONF_DEVICE_ID
):
cv
.
string
,
vol
.
Required
(
CONF_DEVICE_ID
):
cv
.
string
,
vol
.
Required
(
CONF_STATE_TOPIC
,
default
=
DEFAULT_TOPIC
):
cv
.
string
,
vol
.
Required
(
CONF_STATE_TOPIC
,
default
=
DEFAULT_TOPIC
):
cv
.
string
,
vol
.
Required
(
CONF_TIMEOUT
,
default
=
DEFAULT_TIMEOUT
):
cv
.
positive_int
,
vol
.
Required
(
CONF_TIMEOUT
,
default
=
DEFAULT_TIMEOUT
):
cv
.
positive_int
,
vol
.
Optional
(
CONF_AWAY_TIMEOUT
,
default
=
DEFAULT_AWAY_TIMEOUT
):
cv
.
positive_int
,
vol
.
Optional
(
CONF_NAME
,
default
=
DEFAULT_NAME
):
cv
.
string
vol
.
Optional
(
CONF_NAME
,
default
=
DEFAULT_NAME
):
cv
.
string
})
})
...
@@ -56,7 +62,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
...
@@ -56,7 +62,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
config
.
get
(
CONF_NAME
),
config
.
get
(
CONF_NAME
),
config
.
get
(
CONF_STATE_TOPIC
),
config
.
get
(
CONF_STATE_TOPIC
),
config
.
get
(
CONF_DEVICE_ID
),
config
.
get
(
CONF_DEVICE_ID
),
config
.
get
(
CONF_TIMEOUT
)
config
.
get
(
CONF_TIMEOUT
),
config
.
get
(
CONF_AWAY_TIMEOUT
)
)])
)])
...
@@ -64,14 +71,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
...
@@ -64,14 +71,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class
MQTTRoomSensor
(
Entity
):
class
MQTTRoomSensor
(
Entity
):
"""
Representation of a room sensor that is updated via MQTT.
"""
"""
Representation of a room sensor that is updated via MQTT.
"""
def
__init__
(
self
,
hass
,
name
,
state_topic
,
device_id
,
timeout
):
def
__init__
(
self
,
hass
,
name
,
state_topic
,
device_id
,
timeout
,
consider_home
):
"""
Initialize the sensor.
"""
"""
Initialize the sensor.
"""
self
.
_state
=
STATE_
UNKNOWN
self
.
_state
=
STATE_
AWAY
self
.
_hass
=
hass
self
.
_hass
=
hass
self
.
_name
=
name
self
.
_name
=
name
self
.
_state_topic
=
'
{}{}
'
.
format
(
state_topic
,
'
/+
'
)
self
.
_state_topic
=
'
{}{}
'
.
format
(
state_topic
,
'
/+
'
)
self
.
_device_id
=
slugify
(
device_id
).
upper
()
self
.
_device_id
=
slugify
(
device_id
).
upper
()
self
.
_timeout
=
timeout
self
.
_timeout
=
timeout
self
.
_consider_home
=
\
timedelta
(
seconds
=
consider_home
)
if
consider_home
\
else
None
self
.
_distance
=
None
self
.
_distance
=
None
self
.
_updated
=
None
self
.
_updated
=
None
...
@@ -109,11 +120,6 @@ class MQTTRoomSensor(Entity):
...
@@ -109,11 +120,6 @@ class MQTTRoomSensor(Entity):
mqtt
.
subscribe
(
hass
,
self
.
_state_topic
,
message_received
,
1
)
mqtt
.
subscribe
(
hass
,
self
.
_state_topic
,
message_received
,
1
)
@property
def
should_poll
(
self
):
"""
No polling needed.
"""
return
False
@property
@property
def
name
(
self
):
def
name
(
self
):
"""
Return the name of the sensor.
"""
"""
Return the name of the sensor.
"""
...
@@ -131,6 +137,13 @@ class MQTTRoomSensor(Entity):
...
@@ -131,6 +137,13 @@ class MQTTRoomSensor(Entity):
"""
Return the current room of the entity.
"""
"""
Return the current room of the entity.
"""
return
self
.
_state
return
self
.
_state
def
update
(
self
):
"""
Update the state for absent devices.
"""
if
self
.
_updated
\
and
self
.
_consider_home
\
and
dt
.
utcnow
()
-
self
.
_updated
>
self
.
_consider_home
:
self
.
_state
=
STATE_AWAY
def
_parse_update_data
(
topic
,
data
):
def
_parse_update_data
(
topic
,
data
):
"""
Parse the room presence update.
"""
"""
Parse the room presence update.
"""
...
...
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