Skip to content

Commit

Permalink
Made it possible to define multiple Octoprint printers (#16519)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
reefab authored and MartinHjelmare committed Oct 11, 2018
1 parent c6c5d40 commit 9fa7906
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 72 deletions.
40 changes: 14 additions & 26 deletions homeassistant/components/binary_sensor/octoprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,33 @@
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)

Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
SERVICE_SABNZBD = 'sabnzbd'
SERVICE_SAMSUNG_PRINTER = 'samsung_printer'
SERVICE_HOMEKIT = 'homekit'
SERVICE_OCTOPRINT = 'octoprint'

CONFIG_ENTRY_HANDLERS = {
SERVICE_DECONZ: 'deconz',
Expand All @@ -67,6 +68,7 @@
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'),
Expand Down
114 changes: 94 additions & 20 deletions homeassistant/components/octoprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,117 @@
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]

hass.data[DOMAIN] = {"api": None}

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)

return True
printers = hass.data[DOMAIN] = {}
success = False

def device_discovered(service, info):
"""Get called when an Octoprint server has been discovered."""
_LOGGER.debug('Found an Octoprint server: %s', info)

discovery.listen(hass, SERVICE_OCTOPRINT, device_discovered)

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:
Expand Down
44 changes: 18 additions & 26 deletions homeassistant/components/sensor/octoprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,28 @@
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:
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -96,6 +82,7 @@ def __init__(self, api, condition, sensor_type, sensor_name, unit,
self.api_endpoint = endpoint
self.api_group = group
self.api_tool = tool
self._icon = icon
_LOGGER.debug("Created OctoPrint sensor %r", self)

@property
Expand Down Expand Up @@ -128,3 +115,8 @@ def update(self):
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

0 comments on commit 9fa7906

Please sign in to comment.