This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -10,8 +10,8 @@ from . import DOMAIN as COMPONENT_DOMAIN, SENSOR_TYPES
_LOGGER = logging.getLogger(__name__)
NOTIFICATION_ID = 'octoprint_notification'
NOTIFICATION_TITLE = 'OctoPrint sensor setup error'
NOTIFICATION_ID = "octoprint_notification"
NOTIFICATION_TITLE = "OctoPrint sensor setup error"
def setup_platform(hass, config, add_entities, discovery_info=None):
@ -19,23 +19,24 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if discovery_info is None:
return
name = discovery_info['name']
base_url = discovery_info['base_url']
monitored_conditions = discovery_info['sensors']
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.',
"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)
notification_id=NOTIFICATION_ID,
)
devices = []
types = ["actual", "target"]
@ -44,15 +45,28 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
for tool in tools:
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)
octoprint_api,
temp_type,
temp_type,
name,
SENSOR_TYPES[octo_type][3],
SENSOR_TYPES[octo_type][0],
SENSOR_TYPES[octo_type][1],
tool,
)
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])
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)
@ -60,15 +74,24 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class OctoPrintSensor(Entity):
"""Representation of an OctoPrint sensor."""
def __init__(self, api, condition, sensor_type, sensor_name, unit,
endpoint, group, tool=None, icon=None):
def __init__(
self,
api,
condition,
sensor_type,
sensor_name,
unit,
endpoint,
group,
tool=None,
icon=None,
):
"""Initialize a new OctoPrint sensor."""
self.sensor_name = sensor_name
if tool is None:
self._name = '{} {}'.format(sensor_name, condition)
self._name = "{} {}".format(sensor_name, condition)
else:
self._name = '{} {} {} {}'.format(
sensor_name, condition, tool, 'temp')
self._name = "{} {} {} {}".format(sensor_name, condition, tool, "temp")
self.sensor_type = sensor_type
self.api = api
self._state = None
@ -104,8 +127,8 @@ class OctoPrintSensor(Entity):
"""Update state of sensor."""
try:
self._state = self.api.update(
self.sensor_type, self.api_endpoint, self.api_group,
self.api_tool)
self.sensor_type, self.api_endpoint, self.api_group, self.api_tool
)
except requests.exceptions.ConnectionError:
# Error calling the api, already logged in api.update()
return