2016-05-04 01:35:11 +00:00
|
|
|
"""
|
|
|
|
Support for monitoring OctoPrint binary sensors.
|
|
|
|
|
2016-05-07 01:03:18 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/binary_sensor.octoprint/
|
2016-05-04 01:35:11 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2016-09-02 10:26:23 +00:00
|
|
|
|
2016-05-04 01:35:11 +00:00
|
|
|
import requests
|
2016-09-02 10:26:23 +00:00
|
|
|
import voluptuous as vol
|
2016-05-04 01:35:11 +00:00
|
|
|
|
2017-04-30 03:36:50 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_MONITORED_CONDITIONS
|
2016-09-02 10:26:23 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDevice, PLATFORM_SCHEMA)
|
2016-05-04 01:35:11 +00:00
|
|
|
from homeassistant.loader import get_component
|
2016-09-02 10:26:23 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-05-04 01:35:11 +00:00
|
|
|
|
2016-09-02 10:26:23 +00:00
|
|
|
DEPENDENCIES = ['octoprint']
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'OctoPrint'
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
|
|
|
# API Endpoint, Group, Key, unit
|
2016-09-02 10:26:23 +00:00
|
|
|
'Printing': ['printer', 'state', 'printing', None],
|
|
|
|
'Printing Error': ['printer', 'state', 'error', None]
|
2016-05-04 01:35:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 10:26:23 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_TYPES):
|
|
|
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the available OctoPrint binary sensors."""
|
2016-05-04 01:35:11 +00:00
|
|
|
octoprint = get_component('octoprint')
|
2016-09-02 10:26:23 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2017-04-30 05:04:49 +00:00
|
|
|
monitored_conditions = config.get(
|
|
|
|
CONF_MONITORED_CONDITIONS, SENSOR_TYPES.keys())
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
devices = []
|
|
|
|
for octo_type in monitored_conditions:
|
2017-04-30 05:04:49 +00:00
|
|
|
new_sensor = OctoPrintBinarySensor(
|
|
|
|
octoprint.OCTOPRINT, octo_type, SENSOR_TYPES[octo_type][2],
|
|
|
|
name, SENSOR_TYPES[octo_type][3], SENSOR_TYPES[octo_type][0],
|
|
|
|
SENSOR_TYPES[octo_type][1], 'flags')
|
2016-09-02 10:26:23 +00:00
|
|
|
devices.append(new_sensor)
|
2016-05-04 01:35:11 +00:00
|
|
|
add_devices(devices)
|
|
|
|
|
|
|
|
|
|
|
|
class OctoPrintBinarySensor(BinarySensorDevice):
|
2016-05-07 01:03:18 +00:00
|
|
|
"""Representation an OctoPrint binary sensor."""
|
2016-05-04 01:35:11 +00:00
|
|
|
|
2016-09-02 10:26:23 +00:00
|
|
|
def __init__(self, api, condition, sensor_type, sensor_name, unit,
|
|
|
|
endpoint, group, tool=None):
|
2016-05-04 01:35:11 +00:00
|
|
|
"""Initialize a new OctoPrint sensor."""
|
|
|
|
self.sensor_name = sensor_name
|
|
|
|
if tool is None:
|
2016-09-02 10:26:23 +00:00
|
|
|
self._name = '{} {}'.format(sensor_name, condition)
|
2016-05-04 01:35:11 +00:00
|
|
|
else:
|
2016-09-02 10:26:23 +00:00
|
|
|
self._name = '{} {}'.format(sensor_name, condition)
|
2016-05-04 01:35:11 +00:00
|
|
|
self.sensor_type = sensor_type
|
|
|
|
self.api = api
|
|
|
|
self._state = False
|
|
|
|
self._unit_of_measurement = unit
|
|
|
|
self.api_endpoint = endpoint
|
|
|
|
self.api_group = group
|
|
|
|
self.api_tool = tool
|
|
|
|
# Set initial state
|
|
|
|
self.update()
|
2016-05-07 01:03:18 +00:00
|
|
|
_LOGGER.debug("Created OctoPrint binary sensor %r", self)
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if binary sensor is on."""
|
2017-04-30 03:36:50 +00:00
|
|
|
return bool(self._state)
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor, from DEVICE_CLASSES."""
|
2016-05-04 01:35:11 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update state of sensor."""
|
|
|
|
try:
|
2017-04-30 05:04:49 +00:00
|
|
|
self._state = self.api.update(
|
|
|
|
self.sensor_type, self.api_endpoint, self.api_group,
|
|
|
|
self.api_tool)
|
2016-05-04 01:35:11 +00:00
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
# Error calling the api, already logged in api.update()
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._state is None:
|
2016-05-07 01:03:18 +00:00
|
|
|
_LOGGER.warning("Unable to locate value for %s", self.sensor_type)
|