core/homeassistant/components/hddtemp/sensor.py

138 lines
3.9 KiB
Python
Raw Normal View History

"""Support for getting the disk temperature of a host."""
2016-10-29 02:06:24 +00:00
from datetime import timedelta
import logging
import socket
from telnetlib import Telnet
2016-10-29 02:06:24 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_DISKS,
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_NAME,
2019-07-31 19:25:30 +00:00
CONF_PORT,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
import homeassistant.helpers.config_validation as cv
2017-10-31 12:31:12 +00:00
from homeassistant.helpers.entity import Entity
2016-10-29 02:06:24 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_DEVICE = "device"
ATTR_MODEL = "model"
2016-10-29 02:06:24 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_HOST = "localhost"
2016-10-29 02:06:24 +00:00
DEFAULT_PORT = 7634
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "HD Temperature"
2016-10-29 02:06:24 +00:00
DEFAULT_TIMEOUT = 5
SCAN_INTERVAL = timedelta(minutes=1)
2016-10-29 02:06:24 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_DISKS, default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
2016-10-29 02:06:24 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the HDDTemp sensor."""
2016-10-29 02:06:24 +00:00
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
disks = config.get(CONF_DISKS)
2016-10-29 02:06:24 +00:00
hddtemp = HddTempData(host, port)
hddtemp.update()
if not disks:
2019-07-31 19:25:30 +00:00
disks = [next(iter(hddtemp.data)).split("|")[0]]
dev = []
for disk in disks:
dev.append(HddTempSensor(name, disk, hddtemp))
add_entities(dev, True)
2016-10-29 02:06:24 +00:00
class HddTempSensor(Entity):
"""Representation of a HDDTemp sensor."""
def __init__(self, name, disk, hddtemp):
2016-10-29 02:06:24 +00:00
"""Initialize a HDDTemp sensor."""
self.hddtemp = hddtemp
self.disk = disk
self._name = f"{name} {disk}"
self._state = None
2016-10-29 02:06:24 +00:00
self._details = None
self._unit = None
2016-10-29 02:06:24 +00:00
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit
2016-10-29 02:06:24 +00:00
@property
def device_state_attributes(self):
2016-10-29 02:06:24 +00:00
"""Return the state attributes of the sensor."""
if self._details is not None:
2019-07-31 19:25:30 +00:00
return {ATTR_DEVICE: self._details[0], ATTR_MODEL: self._details[1]}
2016-10-29 02:06:24 +00:00
def update(self):
"""Get the latest data from HDDTemp daemon and updates the state."""
self.hddtemp.update()
if self.hddtemp.data and self.disk in self.hddtemp.data:
2019-07-31 19:25:30 +00:00
self._details = self.hddtemp.data[self.disk].split("|")
self._state = self._details[2]
2019-07-31 19:25:30 +00:00
if self._details is not None and self._details[3] == "F":
self._unit = TEMP_FAHRENHEIT
else:
self._unit = TEMP_CELSIUS
2016-10-29 02:06:24 +00:00
else:
2017-10-31 12:31:12 +00:00
self._state = None
2016-10-29 02:06:24 +00:00
class HddTempData:
2016-10-29 02:06:24 +00:00
"""Get the latest data from HDDTemp and update the states."""
def __init__(self, host, port):
"""Initialize the data object."""
self.host = host
self.port = port
self.data = None
def update(self):
"""Get the latest data from HDDTemp running as daemon."""
2016-10-29 02:06:24 +00:00
try:
2019-07-31 19:25:30 +00:00
connection = Telnet(host=self.host, port=self.port, timeout=DEFAULT_TIMEOUT)
data = (
connection.read_all()
.decode("ascii")
.lstrip("|")
.rstrip("|")
.split("||")
)
self.data = {data[i].split("|")[0]: data[i] for i in range(0, len(data), 1)}
2016-10-29 02:06:24 +00:00
except ConnectionRefusedError:
2019-07-31 19:25:30 +00:00
_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)
2016-10-29 02:06:24 +00:00
self.data = None