core/homeassistant/components/sensor/octoprint.py

123 lines
4.2 KiB
Python
Raw Normal View History

2016-05-04 01:35:11 +00:00
"""
Support for monitoring OctoPrint sensors.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/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 (SENSOR_TYPES,
DOMAIN as COMPONENT_DOMAIN)
from homeassistant.const import (TEMP_CELSIUS)
2016-05-04 01:35:11 +00:00
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
2016-05-04 01:35:11 +00:00
DEPENDENCIES = ['octoprint']
NOTIFICATION_ID = 'octoprint_notification'
NOTIFICATION_TITLE = 'OctoPrint sensor setup error'
2016-05-04 01:35:11 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the available OctoPrint 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]
tools = octoprint_api.get_tools()
if "Temperatures" in monitored_conditions:
if not tools:
hass.components.persistent_notification.create(
'Your printer appears to be offline.<br />'
'If you do not want to have your printer on <br />'
' at all times, and you would like to monitor <br /> '
'temperatures, please add <br />'
'bed and/or number&#95of&#95tools to your config <br />'
'and restart.',
title=NOTIFICATION_TITLE,
notification_id=NOTIFICATION_ID)
2016-05-04 01:35:11 +00:00
devices = []
types = ["actual", "target"]
for octo_type in monitored_conditions:
if octo_type == "Temperatures":
for tool in tools:
2016-05-04 01:35:11 +00:00
for temp_type in types:
new_sensor = OctoPrintSensor(
octoprint_api, temp_type, temp_type, name,
SENSOR_TYPES[octo_type][3], SENSOR_TYPES[octo_type][0],
SENSOR_TYPES[octo_type][1], tool)
2016-05-04 01:35:11 +00:00
devices.append(new_sensor)
else:
new_sensor = OctoPrintSensor(
octoprint_api, octo_type, SENSOR_TYPES[octo_type][2],
name, SENSOR_TYPES[octo_type][3], SENSOR_TYPES[octo_type][0],
SENSOR_TYPES[octo_type][1], None, SENSOR_TYPES[octo_type][4])
devices.append(new_sensor)
add_entities(devices, True)
2016-05-04 01:35:11 +00:00
class OctoPrintSensor(Entity):
"""Representation of an OctoPrint sensor."""
2016-05-04 01:35:11 +00:00
def __init__(self, api, condition, sensor_type, sensor_name, unit,
endpoint, group, tool=None, icon=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, tool, 'temp')
2016-05-04 01:35:11 +00:00
self.sensor_type = sensor_type
self.api = api
self._state = None
self._unit_of_measurement = unit
self.api_endpoint = endpoint
self.api_group = group
self.api_tool = tool
self._icon = icon
_LOGGER.debug("Created OctoPrint 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 state(self):
"""Return the state of the sensor."""
sensor_unit = self.unit_of_measurement
if sensor_unit in (TEMP_CELSIUS, "%"):
# API sometimes returns null and not 0
if self._state is None:
self._state = 0
return round(self._state, 2)
return self._state
2016-05-04 01:35:11 +00:00
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
2016-05-04 01:35:11 +00:00
return self._unit_of_measurement
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
@property
def icon(self):
"""Icon to use in the frontend."""
return self._icon