Made it possible to define multiple Octoprint printers (#16519)
* Made it possible to define multiple octoprint printers * style fix * Added configuration option for octoprint port * SSL support in octoprint platform configuration * Octoprint component now auto loads sensor and binary_sensor platforms * preliminary support for auto discovery of octoprint servers * Moved sensors and binary sensors configuration into main octoprint configuration * Using base_url as the key for storing api in the octoprint component * made sure to not supersede the platforms' domains * bugfix: continue setting up other printers if one fails * flake8 style correction * Added icons to sensors * Fail platform setup if no printers were successfully added * Simplified custom validator
This commit is contained in:
parent
c6c5d40056
commit
9fa7906aef
4 changed files with 125 additions and 69 deletions
|
@ -7,42 +7,28 @@ https://home-assistant.io/components/sensor.octoprint/
|
|||
import logging
|
||||
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
TEMP_CELSIUS, CONF_NAME, CONF_MONITORED_CONDITIONS)
|
||||
from homeassistant.components.octoprint import (SENSOR_TYPES,
|
||||
DOMAIN as COMPONENT_DOMAIN)
|
||||
from homeassistant.const import (TEMP_CELSIUS)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEPENDENCIES = ['octoprint']
|
||||
DOMAIN = "octoprint"
|
||||
DEFAULT_NAME = 'OctoPrint'
|
||||
NOTIFICATION_ID = 'octoprint_notification'
|
||||
NOTIFICATION_TITLE = 'OctoPrint sensor setup error'
|
||||
|
||||
SENSOR_TYPES = {
|
||||
'Temperatures': ['printer', 'temperature', '*', TEMP_CELSIUS],
|
||||
'Current State': ['printer', 'state', 'text', None],
|
||||
'Job Percentage': ['job', 'progress', 'completion', '%'],
|
||||
'Time Remaining': ['job', 'progress', 'printTimeLeft', 'seconds'],
|
||||
'Time Elapsed': ['job', 'progress', 'printTime', 'seconds'],
|
||||
}
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)):
|
||||
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the available OctoPrint sensors."""
|
||||
octoprint_api = hass.data[DOMAIN]["api"]
|
||||
name = config.get(CONF_NAME)
|
||||
monitored_conditions = config.get(CONF_MONITORED_CONDITIONS)
|
||||
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:
|
||||
|
@ -72,7 +58,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
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])
|
||||
SENSOR_TYPES[octo_type][1], None, SENSOR_TYPES[octo_type][4])
|
||||
devices.append(new_sensor)
|
||||
add_entities(devices, True)
|
||||
|
||||
|
@ -81,7 +67,7 @@ class OctoPrintSensor(Entity):
|
|||
"""Representation of an OctoPrint sensor."""
|
||||
|
||||
def __init__(self, api, condition, sensor_type, sensor_name, unit,
|
||||
endpoint, group, tool=None):
|
||||
endpoint, group, tool=None, icon=None):
|
||||
"""Initialize a new OctoPrint sensor."""
|
||||
self.sensor_name = sensor_name
|
||||
if tool is None:
|
||||
|
@ -96,6 +82,7 @@ class OctoPrintSensor(Entity):
|
|||
self.api_endpoint = endpoint
|
||||
self.api_group = group
|
||||
self.api_tool = tool
|
||||
self._icon = icon
|
||||
_LOGGER.debug("Created OctoPrint sensor %r", self)
|
||||
|
||||
@property
|
||||
|
@ -128,3 +115,8 @@ class OctoPrintSensor(Entity):
|
|||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue