8000 Add hddtemp sensor device even if unreachable. by cgtobi · Pull Request #10623 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add hddtemp sensor device even if unreachable. #10623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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