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,45 +7,33 @@ https://home-assistant.io/components/binary_sensor.octoprint/
|
|||
import logging
|
||||
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_NAME, CONF_MONITORED_CONDITIONS
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDevice, PLATFORM_SCHEMA)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.octoprint import (BINARY_SENSOR_TYPES,
|
||||
DOMAIN as COMPONENT_DOMAIN)
|
||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEPENDENCIES = ['octoprint']
|
||||
DOMAIN = "octoprint"
|
||||
DEFAULT_NAME = 'OctoPrint'
|
||||
|
||||
SENSOR_TYPES = {
|
||||
# API Endpoint, Group, Key, unit
|
||||
'Printing': ['printer', 'state', 'printing', None],
|
||||
'Printing Error': ['printer', 'state', 'error', None]
|
||||
}
|
||||
|
||||
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 binary sensors."""
|
||||
octoprint_api = hass.data[DOMAIN]["api"]
|
||||
name = config.get(CONF_NAME)
|
||||
monitored_conditions = config.get(
|
||||
CONF_MONITORED_CONDITIONS, SENSOR_TYPES.keys())
|
||||
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]
|
||||
|
||||
devices = []
|
||||
for octo_type in monitored_conditions:
|
||||
new_sensor = OctoPrintBinarySensor(
|
||||
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], 'flags')
|
||||
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)
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@ SERVICE_DAIKIN = 'daikin'
|
|||
SERVICE_SABNZBD = 'sabnzbd'
|
||||
SERVICE_SAMSUNG_PRINTER = 'samsung_printer'
|
||||
SERVICE_HOMEKIT = 'homekit'
|
||||
SERVICE_OCTOPRINT = 'octoprint'
|
||||
|
||||
CONFIG_ENTRY_HANDLERS = {
|
||||
SERVICE_DECONZ: 'deconz',
|
||||
|
@ -67,6 +68,7 @@ SERVICE_HANDLERS = {
|
|||
SERVICE_SABNZBD: ('sabnzbd', None),
|
||||
SERVICE_SAMSUNG_PRINTER: ('sensor', 'syncthru'),
|
||||
SERVICE_KONNECTED: ('konnected', None),
|
||||
SERVICE_OCTOPRINT: ('octoprint', None),
|
||||
'panasonic_viera': ('media_player', 'panasonic_viera'),
|
||||
'plex_mediaserver': ('media_player', 'plex'),
|
||||
'roku': ('media_player', 'roku'),
|
||||
|
|
|
@ -11,43 +11,117 @@ import requests
|
|||
import voluptuous as vol
|
||||
from aiohttp.hdrs import CONTENT_TYPE
|
||||
|
||||
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONTENT_TYPE_JSON
|
||||
from homeassistant.components.discovery import SERVICE_OCTOPRINT
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY, CONF_HOST, CONTENT_TYPE_JSON, CONF_NAME, CONF_PORT,
|
||||
CONF_SSL, TEMP_CELSIUS, CONF_MONITORED_CONDITIONS, CONF_SENSORS,
|
||||
CONF_BINARY_SENSORS)
|
||||
from homeassistant.helpers import discovery
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.discovery import load_platform
|
||||
from homeassistant.util import slugify as util_slugify
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = 'octoprint'
|
||||
CONF_NUMBER_OF_TOOLS = 'number_of_tools'
|
||||
CONF_BED = 'bed'
|
||||
DEFAULT_NAME = 'OctoPrint'
|
||||
|
||||
|
||||
def has_all_unique_names(value):
|
||||
"""Validate that printers have an unique name."""
|
||||
names = [util_slugify(printer['name']) for printer in value]
|
||||
vol.Schema(vol.Unique())(names)
|
||||
return value
|
||||
|
||||
|
||||
BINARY_SENSOR_TYPES = {
|
||||
# API Endpoint, Group, Key, unit
|
||||
'Printing': ['printer', 'state', 'printing', None],
|
||||
'Printing Error': ['printer', 'state', 'error', None]
|
||||
}
|
||||
|
||||
BINARY_SENSOR_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(BINARY_SENSOR_TYPES)):
|
||||
vol.All(cv.ensure_list, [vol.In(BINARY_SENSOR_TYPES)]),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
})
|
||||
|
||||
SENSOR_TYPES = {
|
||||
# API Endpoint, Group, Key, unit, icon
|
||||
'Temperatures': ['printer', 'temperature', '*', TEMP_CELSIUS],
|
||||
'Current State': ['printer', 'state', 'text', None, 'mdi:printer-3d'],
|
||||
'Job Percentage': ['job', 'progress', 'completion', '%',
|
||||
'mdi:file-percent'],
|
||||
'Time Remaining': ['job', 'progress', 'printTimeLeft', 'seconds',
|
||||
'mdi:clock-end'],
|
||||
'Time Elapsed': ['job', 'progress', 'printTime', 'seconds',
|
||||
'mdi:clock-start'],
|
||||
}
|
||||
|
||||
SENSOR_SCHEMA = vol.Schema({
|
||||
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,
|
||||
})
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
DOMAIN: vol.All(cv.ensure_list, [vol.Schema({
|
||||
vol.Required(CONF_API_KEY): cv.string,
|
||||
vol.Required(CONF_HOST): cv.string,
|
||||
vol.Optional(CONF_SSL, default=False): cv.boolean,
|
||||
vol.Optional(CONF_PORT, default=80): cv.port,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_NUMBER_OF_TOOLS, default=0): cv.positive_int,
|
||||
vol.Optional(CONF_BED, default=False): cv.boolean
|
||||
}),
|
||||
vol.Optional(CONF_BED, default=False): cv.boolean,
|
||||
vol.Optional(CONF_SENSORS, default={}): SENSOR_SCHEMA,
|
||||
vol.Optional(CONF_BINARY_SENSORS, default={}): BINARY_SENSOR_SCHEMA
|
||||
})], has_all_unique_names),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the OctoPrint component."""
|
||||
base_url = 'http://{}/api/'.format(config[DOMAIN][CONF_HOST])
|
||||
api_key = config[DOMAIN][CONF_API_KEY]
|
||||
number_of_tools = config[DOMAIN][CONF_NUMBER_OF_TOOLS]
|
||||
bed = config[DOMAIN][CONF_BED]
|
||||
printers = hass.data[DOMAIN] = {}
|
||||
success = False
|
||||
|
||||
hass.data[DOMAIN] = {"api": None}
|
||||
def device_discovered(service, info):
|
||||
"""Get called when an Octoprint server has been discovered."""
|
||||
_LOGGER.debug('Found an Octoprint server: %s', info)
|
||||
|
||||
try:
|
||||
octoprint_api = OctoPrintAPI(base_url, api_key, bed, number_of_tools)
|
||||
hass.data[DOMAIN]["api"] = octoprint_api
|
||||
octoprint_api.get('printer')
|
||||
octoprint_api.get('job')
|
||||
except requests.exceptions.RequestException as conn_err:
|
||||
_LOGGER.error("Error setting up OctoPrint API: %r", conn_err)
|
||||
discovery.listen(hass, SERVICE_OCTOPRINT, device_discovered)
|
||||
|
||||
return True
|
||||
for printer in config[DOMAIN]:
|
||||
name = printer[CONF_NAME]
|
||||
ssl = 's' if printer[CONF_SSL] else ''
|
||||
base_url = 'http{}://{}:{}/api/'.format(ssl,
|
||||
printer[CONF_HOST],
|
||||
printer[CONF_PORT])
|
||||
api_key = printer[CONF_API_KEY]
|
||||
number_of_tools = printer[CONF_NUMBER_OF_TOOLS]
|
||||
bed = printer[CONF_BED]
|
||||
try:
|
||||
octoprint_api = OctoPrintAPI(base_url, api_key, bed,
|
||||
number_of_tools)
|
||||
printers[base_url] = octoprint_api
|
||||
octoprint_api.get('printer')
|
||||
octoprint_api.get('job')
|
||||
except requests.exceptions.RequestException as conn_err:
|
||||
_LOGGER.error("Error setting up OctoPrint API: %r", conn_err)
|
||||
continue
|
||||
|
||||
sensors = printer[CONF_SENSORS][CONF_MONITORED_CONDITIONS]
|
||||
load_platform(hass, 'sensor', DOMAIN, {'name': name,
|
||||
'base_url': base_url,
|
||||
'sensors': sensors})
|
||||
b_sensors = printer[CONF_BINARY_SENSORS][CONF_MONITORED_CONDITIONS]
|
||||
load_platform(hass, 'binary_sensor', DOMAIN, {'name': name,
|
||||
'base_url': base_url,
|
||||
'sensors': b_sensors})
|
||||
success = True
|
||||
|
||||
return success
|
||||
|
||||
|
||||
class OctoPrintAPI:
|
||||
|
|
|
@ -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