core/homeassistant/components/octoprint/binary_sensor.py

85 lines
2.7 KiB
Python
Raw Normal View History

2016-05-04 01:35:11 +00:00
"""
Support for monitoring OctoPrint binary sensors.
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-05-04 01:35:11 +00:00
import requests
from homeassistant.components.octoprint import (BINARY_SENSOR_TYPES,
DOMAIN as COMPONENT_DOMAIN)
from homeassistant.components.binary_sensor import BinarySensorDevice
_LOGGER = logging.getLogger(__name__)
2016-05-04 01:35:11 +00:00
DEPENDENCIES = ['octoprint']
2016-05-04 01:35:11 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the available OctoPrint binary sensors."""
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:
new_sensor = OctoPrintBinarySensor(
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')
devices.append(new_sensor)
add_entities(devices, True)
2016-05-04 01:35:11 +00:00
class OctoPrintBinarySensor(BinarySensorDevice):
"""Representation an OctoPrint binary sensor."""
2016-05-04 01:35:11 +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:
self._name = '{} {}'.format(sensor_name, condition)
2016-05-04 01:35:11 +00:00
else:
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
_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."""
return bool(self._state)
2016-05-04 01:35:11 +00:00
@property
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:
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