mirror of
https://github.com/home-assistant/core.git
synced 2025-08-13 07:20:01 +00:00
.github
docs
homeassistant
script
tests
components
alarm_control_panel
automation
binary_sensor
calendar
camera
climate
config
cover
device_tracker
emulated_hue
fan
http
image_processing
light
lock
media_player
mqtt
notify
recorder
remote
scene
sensor
__init__.py
test_api_streams.py
test_command_line.py
test_darksky.py
test_dsmr.py
test_dyson.py
test_efergy.py
test_file.py
test_history_stats.py
test_imap_email_content.py
test_kira.py
test_mfi.py
test_mhz19.py
test_min_max.py
test_moldindicator.py
test_moon.py
test_mqtt.py
test_mqtt_room.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_sleepiq.py
test_sonarr.py
test_statistics.py
test_tcp.py
test_template.py
test_time_date.py
test_worldclock.py
test_wsdot.py
test_wunderground.py
test_yahoo_finance.py
test_yr.py
test_zwave.py
switch
tts
weather
zwave
__init__.py
test_alert.py
test_alexa.py
test_api.py
test_apiai.py
test_configurator.py
test_conversation.py
test_datadog.py
test_demo.py
test_device_sun_light_trigger.py
test_discovery.py
test_dyson.py
test_ffmpeg.py
test_frontend.py
test_google.py
test_graphite.py
test_group.py
test_hassio.py
test_history.py
test_influxdb.py
test_init.py
test_input_boolean.py
test_input_select.py
test_input_slider.py
test_introduction.py
test_kira.py
test_litejet.py
test_logbook.py
test_logentries.py
test_logger.py
test_microsoft_face.py
test_mqtt_eventstream.py
test_panel_custom.py
test_panel_iframe.py
test_persistent_notification.py
test_pilight.py
test_plant.py
test_proximity.py
test_python_script.py
test_rest_command.py
test_rflink.py
test_rfxtrx.py
test_ring.py
test_rss_feed_template.py
test_script.py
test_shell_command.py
test_sleepiq.py
test_spc.py
test_splunk.py
test_statsd.py
test_sun.py
test_updater.py
test_weblink.py
test_websocket_api.py
test_zone.py
fixtures
helpers
mock
resources
scripts
test_util
testing_config
util
__init__.py
common.py
conftest.py
test_bootstrap.py
test_config.py
test_core.py
test_loader.py
test_main.py
test_remote.py
test_setup.py
virtualization
.coveragerc
.dockerignore
.gitignore
.gitmodules
.hound.yml
.ignore
.travis.yml
CLA.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE.md
MANIFEST.in
README.rst
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini

* Added 'change' field to statistics sensor * Updated statistics sensor test * Updated statistics sensor test complaint
103 lines
3.9 KiB
Python
103 lines
3.9 KiB
Python
"""The test for the statistics sensor platform."""
|
|
import unittest
|
|
import statistics
|
|
|
|
from homeassistant.setup import setup_component
|
|
from homeassistant.const import (ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS)
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
class TestStatisticsSensor(unittest.TestCase):
|
|
"""Test the Statistics sensor."""
|
|
|
|
def setup_method(self, method):
|
|
"""Setup 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 = self.values[-1] - self.values[0]
|
|
self.average_change = self.change / (len(self.values) - 1)
|
|
|
|
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 = [1, 0, 1, 0, 1, 0, 1]
|
|
assert setup_component(self.hass, 'sensor', {
|
|
'sensor': {
|
|
'platform': 'statistics',
|
|
'name': 'test',
|
|
'entity_id': 'binary_sensor.test_monitored',
|
|
}
|
|
})
|
|
|
|
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')
|
|
|
|
self.assertEqual(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',
|
|
}
|
|
})
|
|
|
|
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')
|
|
|
|
self.assertEqual(str(self.mean), state.state)
|
|
self.assertEqual(self.min, state.attributes.get('min_value'))
|
|
self.assertEqual(self.max, state.attributes.get('max_value'))
|
|
self.assertEqual(self.variance, state.attributes.get('variance'))
|
|
self.assertEqual(self.median, state.attributes.get('median'))
|
|
self.assertEqual(self.deviation,
|
|
state.attributes.get('standard_deviation'))
|
|
self.assertEqual(self.mean, state.attributes.get('mean'))
|
|
self.assertEqual(self.count, state.attributes.get('count'))
|
|
self.assertEqual(self.total, state.attributes.get('total'))
|
|
self.assertEqual('°C', state.attributes.get('unit_of_measurement'))
|
|
self.assertEqual(self.change, state.attributes.get('change'))
|
|
self.assertEqual(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,
|
|
}
|
|
})
|
|
|
|
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')
|
|
|
|
self.assertEqual(3.8, state.attributes.get('min_value'))
|
|
self.assertEqual(14, state.attributes.get('max_value'))
|