-
Notifications
You must be signed in to change notification settings - Fork 14
/
sensor.py
144 lines (120 loc) · 5.08 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Support for monitoring Duet3D sensors."""
#TODO: add tool and bed status, need moar sensors!
import logging
import requests
from homeassistant.const import TEMP_CELSIUS
from homeassistant.helpers.entity import Entity
from . import DOMAIN as COMPONENT_DOMAIN, SENSOR_TYPES
_LOGGER = logging.getLogger(__name__)
NOTIFICATION_ID = 'duet3d_notification'
NOTIFICATION_TITLE = 'Duet3d sensor setup error'
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']
duet3d_api = hass.data[COMPONENT_DOMAIN][base_url]
tools = duet3d_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_of_tools to your config <br />'
'and restart.',
title=NOTIFICATION_TITLE,
notification_id=NOTIFICATION_ID)
devices = []
types = ["current", "active", "standby"]
bed_types = ["current", "active"]
for octo_type in monitored_conditions:
if octo_type == "Temperatures":
for tool in tools:
if tool == 'bed':
for temp_type in bed_types:
new_sensor = Duet3DSensor(
duet3d_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:
for temp_type in types:
new_sensor = Duet3DSensor(
duet3d_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 = Duet3DSensor(
duet3d_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)
class Duet3DSensor(Entity):
"""Representation of an OctoPrint sensor."""
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)
else:
self._name = '{} {} tool{} {}'.format(
sensor_name, condition, tool, 'temp')
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
self._available = False
_LOGGER.debug("Created OctoPrint sensor %r", self)
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
print_status_dict = { 'S':'Stopped', 'M':'Simulating',
'P': 'Printing', 'I':'Idle', 'C':'Configuring',
'B':'Busy', 'D':'Decelerating', 'R':'Resuming',
'H':'Halted', 'F':'Flashing Firmware', 'T':'Changin Tool'}
sensor_unit = self.unit_of_measurement
if self._state in print_status_dict:
self._state = print_status_dict[self._state]
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
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
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)
self._available = True
except requests.exceptions.ConnectionError:
# Error calling the api, already logged in api.update()
self._available = False
return
@property
def icon(self):
"""Icon to use in the frontend."""
return self._icon
@property
def available(self):
return self._available