Skip to content

Commit

Permalink
Add hddtemp sensor device even if unreachable. (home-assistant#10623)
Browse files Browse the repository at this point in the history
* Add hddtemp sensor device even if unreachable.

* Removed old commented code.

* Move unit detection logic into update.
  • Loading branch information
cgtobi committed Nov 20, 2017
1 parent cb6b3ec commit 3bf3e99
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
32 changes: 18 additions & 14 deletions homeassistant/components/sensor/hddtemp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
from datetime import timedelta
from telnetlib import Telnet
import socket

import voluptuous as vol

Expand Down Expand Up @@ -46,16 +47,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
hddtemp = HddTempData(host, port)
hddtemp.update()

if hddtemp.data is None:
return False

if not disks:
disks = [next(iter(hddtemp.data)).split('|')[0]]

dev = []
for disk in disks:
if disk in hddtemp.data:
dev.append(HddTempSensor(name, disk, hddtemp))
dev.append(HddTempSensor(name, disk, hddtemp))

add_devices(dev, True)

Expand All @@ -70,6 +67,7 @@ def __init__(self, name, disk, hddtemp):
self._name = '{} {}'.format(name, disk)
self._state = None
self._details = None
self._unit = None

@property
def name(self):
Expand All @@ -84,17 +82,16 @@ def state(self):
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
if self._details[3] == 'C':
return TEMP_CELSIUS
return TEMP_FAHRENHEIT
return self._unit

@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return {
ATTR_DEVICE: self._details[0],
ATTR_MODEL: self._details[1],
}
if self._details is not None:
return {
ATTR_DEVICE: self._details[0],
ATTR_MODEL: self._details[1],
}

def update(self):
"""Get the latest data from HDDTemp daemon and updates the state."""
Expand All @@ -103,6 +100,10 @@ def update(self):
if self.hddtemp.data and self.disk in self.hddtemp.data:
self._details = self.hddtemp.data[self.disk].split('|')
self._state = self._details[2]
if self._details is not None and self._details[3] == 'F':
self._unit = TEMP_FAHRENHEIT
else:
self._unit = TEMP_CELSIUS
else:
self._state = None

Expand All @@ -126,6 +127,9 @@ def update(self):
self.data = {data[i].split('|')[0]: data[i]
for i in range(0, len(data), 1)}
except ConnectionRefusedError:
_LOGGER.error(
"HDDTemp is not available at %s:%s", self.host, self.port)
_LOGGER.error("HDDTemp is not available at %s:%s",
self.host, self.port)
self.data = None
except socket.gaierror:
_LOGGER.error("HDDTemp host not found %s:%s", self.host, self.port)
self.data = None
25 changes: 23 additions & 2 deletions tests/components/sensor/test_hddtemp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""The tests for the hddtemp platform."""
import socket

import unittest
from unittest.mock import patch

Expand Down Expand Up @@ -56,6 +58,13 @@
}
}

VALID_CONFIG_HOST_UNREACHABLE = {
'sensor': {
'platform': 'hddtemp',
'host': 'bob.local',
}
}


class TelnetMock():
"""Mock class for the telnetlib.Telnet object."""
Expand All @@ -75,6 +84,8 @@ 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
Expand Down Expand Up @@ -161,7 +172,10 @@ def test_hddtemp_wrong_disk(self):
"""Test hddtemp wrong disk configuration."""
assert setup_component(self.hass, 'sensor', VALID_CONFIG_WRONG_DISK)

self.assertEqual(len(self.hass.states.all()), 0)
self.assertEqual(len(self.hass.states.all()), 1)
state = self.hass.states.get('sensor.hd_temperature_devsdx1')
self.assertEqual(state.attributes.get('friendly_name'),
'HD Temperature ' + '/dev/sdx1')

@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_multiple_disks(self):
Expand Down Expand Up @@ -189,7 +203,14 @@ def test_hddtemp_multiple_disks(self):
'HD Temperature ' + reference['device'])

@patch('telnetlib.Telnet', new=TelnetMock)
def test_hddtemp_host_unreachable(self):
def test_hddtemp_host_refused(self):
"""Test hddtemp if host unreachable."""
assert setup_component(self.hass, 'sensor', VALID_CONFIG_HOST)
self.assertEqual(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)
self.assertEqual(len(self.hass.states.all()), 0)

0 comments on commit 3bf3e99

Please sign in to comment.