1
0
mirror of https://github.com/home-assistant/core.git synced 2025-08-10 22:10:02 +00:00
Files
.github
docs
homeassistant
script
tests
auth
components
air_quality
alarm_control_panel
alert
alexa
ambient_station
api
arlo
auth
automation
binary_sensor
calendar
camera
canary
cast
climate
cloud
config
configurator
conversation
counter
cover
daikin
datadog
deconz
default_config
demo
device_sun_light_trigger
device_tracker
dialogflow
discovery
duckdns
dyson
ecobee
emulated_hue
emulated_roku
esphome
fan
feedreader
ffmpeg
folder_watcher
freedns
fritzbox
frontend
geo_location
geofency
google
google_assistant
google_domains
google_pubsub
gpslogger
graphite
group
hangouts
hassio
history
history_graph
homekit
homekit_controller
homematicip_cloud
http
huawei_lte
hue
ifttt
image_processing
influxdb
init
input_boolean
input_datetime
input_number
input_select
input_text
intent_script
introduction
ios
ipma
kira
light
litejet
locative
lock
logbook
logentries
logger
lovelace
luftdaten
mailbox
mailgun
media_player
melissa
microsoft_face
mochad
mqtt
mqtt_eventstream
mqtt_statestream
mythicbeastsdns
namecheapdns
ness_alarm
nest
no_ip
notify
nuheat
onboarding
openuv
owntracks
panel_custom
panel_iframe
persistent_notification
person
pilight
plant
point
prometheus
proximity
ps4
python_script
qwikswitch
rainmachine
recorder
remember_the_milk
remote
rest_command
rflink
rfxtrx
ring
rss_feed_template
scene
script
sensor
__init__.py
test_api_streams.py
test_awair.py
test_bom.py
test_canary.py
test_coinmarketcap.py
test_command_line.py
test_darksky.py
test_dsmr.py
test_dte_energy_bridge.py
test_dyson.py
test_efergy.py
test_entur_public_transport.py
test_fail2ban.py
test_fido.py
test_file.py
test_filesize.py
test_filter.py
test_folder.py
test_foobot.py
test_geo_rss_events.py
test_google_wifi.py
test_hddtemp.py
test_history_stats.py
test_hydroquebec.py
test_imap_email_content.py
test_integration.py
test_islamic_prayer_times.py
test_jewish_calendar.py
test_london_air.py
test_mfi.py
test_mhz19.py
test_min_max.py
test_moldindicator.py
test_moon.py
test_mqtt_room.py
test_nsw_fuel_station.py
test_openhardwaremonitor.py
test_pilight.py
test_radarr.py
test_random.py
test_rest.py
test_rflink.py
test_rfxtrx.py
test_ring.py
test_rmvtransport.py
test_season.py
test_sigfox.py
test_simulated.py
test_sleepiq.py
test_sonarr.py
test_sql.py
test_srp_energy.py
test_startca.py
test_statistics.py
test_tcp.py
test_teksavvy.py
test_template.py
test_time_date.py
test_transport_nsw.py
test_uk_transport.py
test_uptime.py
test_version.py
test_vultr.py
test_worldclock.py
test_wsdot.py
test_wunderground.py
test_yr.py
test_yweather.py
shell_command
shopping_list
simplisafe
sleepiq
smartthings
smhi
snips
sonos
spaceapi
spc
splunk
statsd
sun
switch
system_health
system_log
tellduslive
timer
toon
tplink
tradfri
tts
twilio
unifi
updater
upnp
utility_meter
vacuum
verisure
vultr
wake_on_lan
water_heater
weather
webhook
weblink
webostv
websocket_api
xiaomi_miio
zha
zone
zwave
__init__.py
conftest.py
fixtures
helpers
mock
resources
scripts
test_util
testing_config
util
__init__.py
common.py
conftest.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_loader.py
test_main.py
test_requirements.py
test_setup.py
virtualization
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hound.yml
.ignore
.readthedocs.yml
.travis.yml
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE.md
MANIFEST.in
README.rst
mypy.ini
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini
core/tests/components/sensor/test_statistics.py
ehendrix23 aadf72d445 Fix statistics for binary sensor ()
* Fix statistics for binary sensor

-) Binary sensors have 'on' and 'off' for state resulting in issue as numbers were expected. Fixed so that it works with non-numeric states as well.
-) Added check to skip unknown states.
-) Updates test so that binary sensor test will use non-numeric values for states.

* Using guard clause and changed debug to error

Changed to use a guard clause for state unknown.
Writing error on value error instead of debug.

* Add docstring
2018-11-29 09:01:56 +01:00

321 lines
12 KiB
Python

"""The test for the statistics sensor platform."""
import unittest
import statistics
from homeassistant.setup import setup_component
from homeassistant.components.sensor.statistics import StatisticsSensor
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, STATE_UNKNOWN)
from homeassistant.util import dt as dt_util
from tests.common import get_test_home_assistant
from unittest.mock import patch
from datetime import datetime, timedelta
from tests.common import init_recorder_component
from homeassistant.components import recorder
class TestStatisticsSensor(unittest.TestCase):
"""Test the Statistics sensor."""
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.values = [17, 20, 15.2, 5, 3.8, 9.2, 6.7, 14, 6]
self.count = len(self.values)
self.min = min(self.values)
self.max = max(self.values)
self.total = sum(self.values)
self.mean = round(sum(self.values) / len(self.values), 2)
self.median = round(statistics.median(self.values), 2)
self.deviation = round(statistics.stdev(self.values), 2)
self.variance = round(statistics.variance(self.values), 2)
self.change = round(self.values[-1] - self.values[0], 2)
self.average_change = round(self.change / (len(self.values) - 1), 2)
self.change_rate = round(self.average_change / (60 * (self.count - 1)),
2)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_binary_sensor_source(self):
"""Test if source is a sensor."""
values = ['on', 'off', 'on', 'off', 'on', 'off', 'on']
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'binary_sensor.test_monitored',
}
})
self.hass.start()
self.hass.block_till_done()
for value in values:
self.hass.states.set('binary_sensor.test_monitored', value)
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_count')
assert str(len(values)) == state.state
def test_sensor_source(self):
"""Test if source is a sensor."""
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
}
})
self.hass.start()
self.hass.block_till_done()
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_mean')
assert str(self.mean) == state.state
assert self.min == state.attributes.get('min_value')
assert self.max == state.attributes.get('max_value')
assert self.variance == state.attributes.get('variance')
assert self.median == state.attributes.get('median')
assert self.deviation == \
state.attributes.get('standard_deviation')
assert self.mean == state.attributes.get('mean')
assert self.count == state.attributes.get('count')
assert self.total == state.attributes.get('total')
assert '°C' == state.attributes.get('unit_of_measurement')
assert self.change == state.attributes.get('change')
assert self.average_change == \
state.attributes.get('average_change')
def test_sampling_size(self):
"""Test rotation."""
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
'sampling_size': 5,
}
})
self.hass.start()
self.hass.block_till_done()
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_mean')
assert 3.8 == state.attributes.get('min_value')
assert 14 == state.attributes.get('max_value')
def test_sampling_size_1(self):
"""Test validity of stats requiring only one sample."""
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
'sampling_size': 1,
}
})
self.hass.start()
self.hass.block_till_done()
for value in self.values[-3:]: # just the last 3 will do
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_mean')
# require only one data point
assert self.values[-1] == state.attributes.get('min_value')
assert self.values[-1] == state.attributes.get('max_value')
assert self.values[-1] == state.attributes.get('mean')
assert self.values[-1] == state.attributes.get('median')
assert self.values[-1] == state.attributes.get('total')
assert 0 == state.attributes.get('change')
assert 0 == state.attributes.get('average_change')
# require at least two data points
assert STATE_UNKNOWN == state.attributes.get('variance')
assert STATE_UNKNOWN == \
state.attributes.get('standard_deviation')
def test_max_age(self):
"""Test value deprecation."""
mock_data = {
'return_time': datetime(2017, 8, 2, 12, 23, tzinfo=dt_util.UTC),
}
def mock_now():
return mock_data['return_time']
with patch('homeassistant.components.sensor.statistics.dt_util.utcnow',
new=mock_now):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
'max_age': {'minutes': 3}
}
})
self.hass.start()
self.hass.block_till_done()
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
# insert the next value one minute later
mock_data['return_time'] += timedelta(minutes=1)
state = self.hass.states.get('sensor.test_mean')
assert 6 == state.attributes.get('min_value')
assert 14 == state.attributes.get('max_value')
def test_change_rate(self):
"""Test min_age/max_age and change_rate."""
mock_data = {
'return_time': datetime(2017, 8, 2, 12, 23, 42,
tzinfo=dt_util.UTC),
}
def mock_now():
return mock_data['return_time']
with patch('homeassistant.components.sensor.statistics.dt_util.utcnow',
new=mock_now):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored'
}
})
self.hass.start()
self.hass.block_till_done()
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
# insert the next value one minute later
mock_data['return_time'] += timedelta(minutes=1)
state = self.hass.states.get('sensor.test_mean')
assert datetime(2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC) == \
state.attributes.get('min_age')
assert datetime(2017, 8, 2, 12, 23 + self.count - 1, 42,
tzinfo=dt_util.UTC) == \
state.attributes.get('max_age')
assert self.change_rate == state.attributes.get('change_rate')
def test_initialize_from_database(self):
"""Test initializing the statistics from the database."""
# enable the recorder
init_recorder_component(self.hass)
# store some values
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
# wait for the recorder to really store the data
self.hass.data[recorder.DATA_INSTANCE].block_till_done()
# only now create the statistics component, so that it must read the
# data from the database
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
'sampling_size': 100,
}
})
self.hass.start()
self.hass.block_till_done()
# check if the result is as in test_sensor_source()
state = self.hass.states.get('sensor.test_mean')
assert str(self.mean) == state.state
def test_initialize_from_database_with_maxage(self):
"""Test initializing the statistics from the database."""
mock_data = {
'return_time': datetime(2017, 8, 2, 12, 23, 42,
tzinfo=dt_util.UTC),
}
def mock_now():
return mock_data['return_time']
# Testing correct retrieval from recorder, thus we do not
# want purging to occur within the class itself.
def mock_purge(self):
return
# Set maximum age to 3 hours.
max_age = 3
# Determine what our minimum age should be based on test values.
expected_min_age = mock_data['return_time'] + \
timedelta(hours=len(self.values) - max_age)
# enable the recorder
init_recorder_component(self.hass)
with patch('homeassistant.components.sensor.statistics.dt_util.utcnow',
new=mock_now), \
patch.object(StatisticsSensor, '_purge_old', mock_purge):
# store some values
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
# insert the next value 1 hour later
mock_data['return_time'] += timedelta(hours=1)
# wait for the recorder to really store the data
self.hass.data[recorder.DATA_INSTANCE].block_till_done()
# only now create the statistics component, so that it must read
# the data from the database
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
'sampling_size': 100,
'max_age': {'hours': max_age}
}
})
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
# check if the result is as in test_sensor_source()
state = self.hass.states.get('sensor.test_mean')
assert expected_min_age == state.attributes.get('min_age')
# The max_age timestamp should be 1 hour before what we have right
# now in mock_data['return_time'].
assert mock_data['return_time'] == state.attributes.get('max_age') +\
timedelta(hours=1)