hass-core/tests/components/hddtemp/test_sensor.py
Penny Wood f195ecca4b Consolidate all platforms that have tests (#22109)
* Moved climate components with tests into platform dirs.

* Updated tests from climate component.

* Moved binary_sensor components with tests into platform dirs.

* Updated tests from binary_sensor component.

* Moved calendar components with tests into platform dirs.

* Updated tests from calendar component.

* Moved camera components with tests into platform dirs.

* Updated tests from camera component.

* Moved cover components with tests into platform dirs.

* Updated tests from cover component.

* Moved device_tracker components with tests into platform dirs.

* Updated tests from device_tracker component.

* Moved fan components with tests into platform dirs.

* Updated tests from fan component.

* Moved geo_location components with tests into platform dirs.

* Updated tests from geo_location component.

* Moved image_processing components with tests into platform dirs.

* Updated tests from image_processing component.

* Moved light components with tests into platform dirs.

* Updated tests from light component.

* Moved lock components with tests into platform dirs.

* Moved media_player components with tests into platform dirs.

* Updated tests from media_player component.

* Moved scene components with tests into platform dirs.

* Moved sensor components with tests into platform dirs.

* Updated tests from sensor component.

* Moved switch components with tests into platform dirs.

* Updated tests from sensor component.

* Moved vacuum components with tests into platform dirs.

* Updated tests from vacuum component.

* Moved weather components with tests into platform dirs.

* Fixed __init__.py files

* Fixes for stuff moved as part of this branch.

* Fix stuff needed to merge with balloob's branch.

* Formatting issues.

* Missing __init__.py files.

* Fix-ups

* Fixup

* Regenerated requirements.

* Linting errors fixed.

* Fixed more broken tests.

* Missing init files.

* Fix broken tests.

* More broken tests

* There seems to be a thread race condition.
I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages.
Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe.

* Disabled tests, will remove sensor in #22147

* Updated coverage and codeowners.
2019-03-18 23:07:39 -07:00

216 lines
7.3 KiB
Python

"""The tests for the hddtemp platform."""
import socket
import unittest
from unittest.mock import patch
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant
VALID_CONFIG_MINIMAL = {
'sensor': {
'platform': 'hddtemp',
}
}
VALID_CONFIG_NAME = {
'sensor': {
'platform': 'hddtemp',
'name': 'FooBar',
}
}
VALID_CONFIG_ONE_DISK = {
'sensor': {
'platform': 'hddtemp',
'disks': [
'/dev/sdd1',
],
}
}
VALID_CONFIG_WRONG_DISK = {
'sensor': {
'platform': 'hddtemp',
'disks': [
'/dev/sdx1',
],
}
}
VALID_CONFIG_MULTIPLE_DISKS = {
'sensor': {
'platform': 'hddtemp',
'host': 'foobar.local',
'disks': [
'/dev/sda1',
'/dev/sdb1',
'/dev/sdc1',
],
}
}
VALID_CONFIG_HOST = {
'sensor': {
'platform': 'hddtemp',
'host': 'alice.local',
}
}
VALID_CONFIG_HOST_UNREACHABLE = {
'sensor': {
'platform': 'hddtemp',
'host': 'bob.local',
}
}
class TelnetMock():
"""Mock class for the telnetlib.Telnet object."""
def __init__(self, host, port, timeout=0):
"""Initialize Telnet object."""
self.host = host
self.port = port
self.timeout = timeout
self.sample_data = bytes('|/dev/sda1|WDC WD30EZRX-12DC0B0|29|C|' +
'|/dev/sdb1|WDC WD15EADS-11P7B2|32|C|' +
'|/dev/sdc1|WDC WD20EARX-22MMMB0|29|C|' +
'|/dev/sdd1|WDC WD15EARS-00Z5B1|89|F|',
'ascii')
def read_all(self):
"""Return sample values."""
if self.host == 'alice.local':
raise ConnectionRefusedError
elif self.host == 'bob.local':
raise socket.gaierror
else:
return self.sample_data
return None
class TestHDDTempSensor(unittest.TestCase):
"""Test the hddtemp sensor."""
def setUp(self):
"""Set up things to run when tests begin."""
self.hass = get_test_home_assistant()
self.config = VALID_CONFIG_ONE_DISK
self.reference = {'/dev/sda1': {'device': '/dev/sda1',
'temperature': '29',
'unit_of_measurement': '°C',
'model': 'WDC WD30EZRX-12DC0B0', },
'/dev/sdb1': {'device': '/dev/sdb1',
'temperature': '32',
'unit_of_measurement': '°C',
'model': 'WDC WD15EADS-11P7B2', },
'/dev/sdc1': {'device': '/dev/sdc1',
'temperature': '29',
'unit_of_measurement': '°C',
'model': 'WDC WD20EARX-22MMMB0', },
'/dev/sdd1': {'device': '/dev/sdd1',
'temperature': '32',
'unit_of_measurement': '°C',
'model': 'WDC WD15EARS-00Z5B1', }, }
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_min_config(self):
"""Test minimal hddtemp configuration."""
assert setup_component(self.hass, 'sensor', VALID_CONFIG_MINIMAL)
entity = self.hass.states.all()[0].entity_id
state = self.hass.states.get(entity)
reference = self.reference[state.attributes.get('device')]
assert state.state == reference['temperature']
assert state.attributes.get('device') == reference['device']
assert state.attributes.get('model') == reference['model']
assert state.attributes.get('unit_of_measurement') == \
reference['unit_of_measurement']
assert state.attributes.get('friendly_name') == \
'HD Temperature ' + reference['device']
@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_rename_config(self):
"""Test hddtemp configuration with different name."""
assert setup_component(self.hass, 'sensor', VALID_CONFIG_NAME)
entity = self.hass.states.all()[0].entity_id
state = self.hass.states.get(entity)
reference = self.reference[state.attributes.get('device')]
assert state.attributes.get('friendly_name') == \
'FooBar ' + reference['device']
@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_one_disk(self):
"""Test hddtemp one disk configuration."""
assert setup_component(self.hass, 'sensor', VALID_CONFIG_ONE_DISK)
state = self.hass.states.get('sensor.hd_temperature_dev_sdd1')
reference = self.reference[state.attributes.get('device')]
assert state.state == reference['temperature']
assert state.attributes.get('device') == reference['device']
assert state.attributes.get('model') == reference['model']
assert state.attributes.get('unit_of_measurement') == \
reference['unit_of_measurement']
assert state.attributes.get('friendly_name') == \
'HD Temperature ' + reference['device']
@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_wrong_disk(self):
"""Test hddtemp wrong disk configuration."""
assert setup_component(self.hass, 'sensor', VALID_CONFIG_WRONG_DISK)
assert len(self.hass.states.all()) == 1
state = self.hass.states.get('sensor.hd_temperature_dev_sdx1')
assert state.attributes.get('friendly_name') == \
'HD Temperature ' + '/dev/sdx1'
@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_multiple_disks(self):
"""Test hddtemp multiple disk configuration."""
assert setup_component(self.hass,
'sensor', VALID_CONFIG_MULTIPLE_DISKS)
for sensor in ['sensor.hd_temperature_dev_sda1',
'sensor.hd_temperature_dev_sdb1',
'sensor.hd_temperature_dev_sdc1']:
state = self.hass.states.get(sensor)
reference = self.reference[state.attributes.get('device')]
assert state.state == \
reference['temperature']
assert state.attributes.get('device') == \
reference['device']
assert state.attributes.get('model') == \
reference['model']
assert state.attributes.get('unit_of_measurement') == \
reference['unit_of_measurement']
assert state.attributes.get('friendly_name') == \
'HD Temperature ' + reference['device']
@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_host_refused(self):
"""Test hddtemp if host unreachable."""
assert setup_component(self.hass, 'sensor', VALID_CONFIG_HOST)
assert len(self.hass.states.all()) == 0
@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_host_unreachable(self):
"""Test hddtemp if host unreachable."""
assert setup_component(self.hass, 'sensor',
VALID_CONFIG_HOST_UNREACHABLE)
assert len(self.hass.states.all()) == 0