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
6ebff3cd
Commit
6ebff3cd
authored
7 years ago
by
Pascal Vizeli
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #10105 from home-assistant/bayesian-sensor
Use constants and update docstrings
parents
70eaa5f1
fc2f41fe
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homeassistant/components/binary_sensor/bayesian.py
+23
-20
23 additions, 20 deletions
homeassistant/components/binary_sensor/bayesian.py
with
23 additions
and
20 deletions
homeassistant/components/binary_sensor/bayesian.py
+
23
−
20
View file @
6ebff3cd
...
@@ -22,6 +22,10 @@ from homeassistant.helpers.event import async_track_state_change
...
@@ -22,6 +22,10 @@ from homeassistant.helpers.event import async_track_state_change
_LOGGER
=
logging
.
getLogger
(
__name__
)
_LOGGER
=
logging
.
getLogger
(
__name__
)
ATTR_OBSERVATIONS
=
'
observations
'
ATTR_PROBABILITY
=
'
probability
'
ATTR_PROBABILITY_THRESHOLD
=
'
probability_threshold
'
CONF_OBSERVATIONS
=
'
observations
'
CONF_OBSERVATIONS
=
'
observations
'
CONF_PRIOR
=
'
prior
'
CONF_PRIOR
=
'
prior
'
CONF_PROBABILITY_THRESHOLD
=
'
probability_threshold
'
CONF_PROBABILITY_THRESHOLD
=
'
probability_threshold
'
...
@@ -29,7 +33,8 @@ CONF_P_GIVEN_F = 'prob_given_false'
...
@@ -29,7 +33,8 @@ CONF_P_GIVEN_F = 'prob_given_false'
CONF_P_GIVEN_T
=
'
prob_given_true
'
CONF_P_GIVEN_T
=
'
prob_given_true
'
CONF_TO_STATE
=
'
to_state
'
CONF_TO_STATE
=
'
to_state
'
DEFAULT_NAME
=
'
BayesianBinary
'
DEFAULT_NAME
=
"
Bayesian Binary Sensor
"
DEFAULT_PROBABILITY_THRESHOLD
=
0.5
NUMERIC_STATE_SCHEMA
=
vol
.
Schema
({
NUMERIC_STATE_SCHEMA
=
vol
.
Schema
({
CONF_PLATFORM
:
'
numeric_state
'
,
CONF_PLATFORM
:
'
numeric_state
'
,
...
@@ -49,16 +54,14 @@ STATE_SCHEMA = vol.Schema({
...
@@ -49,16 +54,14 @@ STATE_SCHEMA = vol.Schema({
},
required
=
True
)
},
required
=
True
)
PLATFORM_SCHEMA
=
PLATFORM_SCHEMA
.
extend
({
PLATFORM_SCHEMA
=
PLATFORM_SCHEMA
.
extend
({
vol
.
Optional
(
CONF_NAME
,
default
=
DEFAULT_NAME
):
vol
.
Optional
(
CONF_NAME
,
default
=
DEFAULT_NAME
):
cv
.
string
,
cv
.
string
,
vol
.
Optional
(
CONF_DEVICE_CLASS
):
cv
.
string
,
vol
.
Optional
(
CONF_DEVICE_CLASS
):
cv
.
string
,
vol
.
Required
(
CONF_OBSERVATIONS
):
vol
.
Schema
(
vol
.
Required
(
CONF_OBSERVATIONS
):
vol
.
All
(
cv
.
ensure_list
,
[
vol
.
Any
(
NUMERIC_STATE_SCHEMA
,
vol
.
Schema
(
vol
.
All
(
cv
.
ensure_list
,
STATE_SCHEMA
)])
[
vol
.
Any
(
NUMERIC_STATE_SCHEMA
,
STATE_SCHEMA
)])),
),
vol
.
Required
(
CONF_PRIOR
):
vol
.
Coerce
(
float
),
vol
.
Required
(
CONF_PRIOR
):
vol
.
Coerce
(
float
),
vol
.
Optional
(
CONF_PROBABILITY_THRESHOLD
):
vol
.
Optional
(
CONF_PROBABILITY_THRESHOLD
,
vol
.
Coerce
(
float
),
default
=
DEFAULT_PROBABILITY_THRESHOLD
):
vol
.
Coerce
(
float
),
})
})
...
@@ -73,16 +76,16 @@ def update_probability(prior, prob_true, prob_false):
...
@@ -73,16 +76,16 @@ def update_probability(prior, prob_true, prob_false):
@asyncio.coroutine
@asyncio.coroutine
def
async_setup_platform
(
hass
,
config
,
async_add_devices
,
discovery_info
=
None
):
def
async_setup_platform
(
hass
,
config
,
async_add_devices
,
discovery_info
=
None
):
"""
Set up the
Threshold
sensor.
"""
"""
Set up the
Bayesian Binary
sensor.
"""
name
=
config
.
get
(
CONF_NAME
)
name
=
config
.
get
(
CONF_NAME
)
observations
=
config
.
get
(
CONF_OBSERVATIONS
)
observations
=
config
.
get
(
CONF_OBSERVATIONS
)
prior
=
config
.
get
(
CONF_PRIOR
)
prior
=
config
.
get
(
CONF_PRIOR
)
probability_threshold
=
config
.
get
(
CONF_PROBABILITY_THRESHOLD
,
0.5
)
probability_threshold
=
config
.
get
(
CONF_PROBABILITY_THRESHOLD
)
device_class
=
config
.
get
(
CONF_DEVICE_CLASS
)
device_class
=
config
.
get
(
CONF_DEVICE_CLASS
)
async_add_devices
([
async_add_devices
([
BayesianBinarySensor
(
name
,
prior
,
observations
,
probability_threshold
,
BayesianBinarySensor
(
device_class
)
name
,
prior
,
observations
,
probability_threshold
,
device_class
)
],
True
)
],
True
)
...
@@ -107,7 +110,7 @@ class BayesianBinarySensor(BinarySensorDevice):
...
@@ -107,7 +110,7 @@ class BayesianBinarySensor(BinarySensorDevice):
self
.
entity_obs
=
dict
.
fromkeys
(
to_observe
,
[])
self
.
entity_obs
=
dict
.
fromkeys
(
to_observe
,
[])
for
ind
,
obs
in
enumerate
(
self
.
_observations
):
for
ind
,
obs
in
enumerate
(
self
.
_observations
):
obs
[
"
id
"
]
=
ind
obs
[
'
id
'
]
=
ind
self
.
entity_obs
[
obs
[
'
entity_id
'
]].
append
(
obs
)
self
.
entity_obs
[
obs
[
'
entity_id
'
]].
append
(
obs
)
self
.
watchers
=
{
self
.
watchers
=
{
...
@@ -117,7 +120,7 @@ class BayesianBinarySensor(BinarySensorDevice):
...
@@ -117,7 +120,7 @@ class BayesianBinarySensor(BinarySensorDevice):
@asyncio.coroutine
@asyncio.coroutine
def
async_added_to_hass
(
self
):
def
async_added_to_hass
(
self
):
"""
Call when entity about to be added
to hass
.
"""
"""
Call when entity about to be added.
"""
@callback
@callback
# pylint: disable=invalid-name
# pylint: disable=invalid-name
def
async_threshold_sensor_state_listener
(
entity
,
old_state
,
def
async_threshold_sensor_state_listener
(
entity
,
old_state
,
...
@@ -135,8 +138,8 @@ class BayesianBinarySensor(BinarySensorDevice):
...
@@ -135,8 +138,8 @@ class BayesianBinarySensor(BinarySensorDevice):
prior
=
self
.
prior
prior
=
self
.
prior
for
obs
in
self
.
current_obs
.
values
():
for
obs
in
self
.
current_obs
.
values
():
prior
=
update_probability
(
prior
,
obs
[
'
prob_true
'
],
prior
=
update_probability
(
obs
[
'
prob_false
'
])
prior
,
obs
[
'
prob_true
'
],
obs
[
'
prob_false
'
])
self
.
probability
=
prior
self
.
probability
=
prior
self
.
hass
.
async_add_job
(
self
.
async_update_ha_state
,
True
)
self
.
hass
.
async_add_job
(
self
.
async_update_ha_state
,
True
)
...
@@ -206,9 +209,9 @@ class BayesianBinarySensor(BinarySensorDevice):
...
@@ -206,9 +209,9 @@ class BayesianBinarySensor(BinarySensorDevice):
def
device_state_attributes
(
self
):
def
device_state_attributes
(
self
):
"""
Return the state attributes of the sensor.
"""
"""
Return the state attributes of the sensor.
"""
return
{
return
{
'
observations
'
:
[
val
for
val
in
self
.
current_obs
.
values
()],
ATTR_OBSERVATIONS
:
[
val
for
val
in
self
.
current_obs
.
values
()],
'
probability
'
:
round
(
self
.
probability
,
2
),
ATTR_PROBABILITY
:
round
(
self
.
probability
,
2
),
'
probability_threshold
'
:
self
.
_probability_threshold
ATTR_PROBABILITY_THRESHOLD
:
self
.
_probability_threshold
,
}
}
@asyncio.coroutine
@asyncio.coroutine
...
...
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