2019-02-14 04:35:12 +00:00
|
|
|
"""Support for monitoring OctoPrint binary sensors."""
|
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
|
|
|
|
|
2018-10-11 07:52:13 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
2016-09-02 10:26:23 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import BINARY_SENSOR_TYPES, DOMAIN as COMPONENT_DOMAIN
|
|
|
|
|
2016-09-02 10:26:23 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the available OctoPrint binary sensors."""
|
2018-10-11 07:52:13 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
name = discovery_info['name']
|
|
|
|
base_url = discovery_info['base_url']
|
|
|
|
monitored_conditions = discovery_info['sensors']
|
|
|
|
octoprint_api = hass.data[COMPONENT_DOMAIN][base_url]
|
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(
|
2018-10-11 07:52:13 +00:00
|
|
|
octoprint_api, octo_type, BINARY_SENSOR_TYPES[octo_type][2],
|
|
|
|
name, BINARY_SENSOR_TYPES[octo_type][3],
|
|
|
|
BINARY_SENSOR_TYPES[octo_type][0],
|
|
|
|
BINARY_SENSOR_TYPES[octo_type][1], 'flags')
|
2016-09-02 10:26:23 +00:00
|
|
|
devices.append(new_sensor)
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devices, True)
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
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
|