forked from richibrics/HassMonitorMqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
129 lines (103 loc) · 5.23 KB
/
__init__.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
import logging
import re
import sys
from subprocess import check_output, CalledProcessError
from copy import deepcopy
import voluptuous as vol
import homeassistant.loader as loader
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
import homeassistant.util.dt as dt_util
import homeassistant.helpers.config_validation as cv
from homeassistant.components import recorder
from homeassistant.helpers.entity import Entity
from homeassistant.core import callback
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.components.mqtt import (
ATTR_DISCOVERY_HASH,
CONF_QOS,
CONF_UNIQUE_ID,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
MqttEntityDeviceInfo,
subscription,
)
DEPENDENCIES = ["mqtt"]
# The domain of your component. Should be equal to the name of your component.
DOMAIN = "monitor_mqtt"
CONF_LIST_KEY = 'monitor_list'
CONF_CLIENT_NAME = "client_name"
MONITOR_MQTT_SCHEMA = vol.Schema(
{
vol.Required(CONF_CLIENT_NAME): cv.string,
}, extra=vol.ALLOW_EXTRA
)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: {
CONF_LIST_KEY:
vol.All(cv.ensure_list, [MONITOR_MQTT_SCHEMA])
}
}, extra=vol.ALLOW_EXTRA)
inbox_information = [{'id': 'ram', 'name': 'ram_used_percentage', 'sensor_label': 'Ram used percentage', 'unity': '%', 'icon': 'mdi:memory', 'device_class': None, 'value': None},
{'id': 'cpu', 'name': 'cpu_used_percentage',
'sensor_label': 'CPU used percentage', 'unity': '%', 'icon': 'mdi:calculator-variant', 'device_class': None, 'value': None},
{'id': 'disk', 'name': 'disk_used_percentage',
'sensor_label': 'Disk used percentage', 'unity': '%', 'icon': 'mdi:harddisk', 'device_class': None, 'value': None},
{'id': 'os', 'name': 'operating_system',
'sensor_label': 'Operating system', 'unity': '', 'icon': 'mdi:$OPERATING_SYSTEM', 'device_class': None, 'value': None},
{'id': 'battery_level', 'name': 'battery_level_percentage',
'sensor_label': 'Battery percentage', 'unity': '%', 'icon': 'mdi:battery', 'device_class': 'battery', 'value': None},
{'id': 'battery_charging', 'name': 'battery_charging',
'sensor_label': 'Battery charging', 'unity': '', 'icon': 'mdi:$PLUGGED', 'device_class': None, 'value': None},
{'id': 'cpu_temperature', 'name': 'cpu_temperature',
'sensor_label': 'CPU temperature', 'unity': '°C', 'icon': 'mdi:coolant-temperature', 'device_class': 'temperature', 'value': None},
{'id': 'time', 'name': 'message_time', 'sensor_label': 'Last update time', 'unity': '', 'icon': 'mdi:clock-outline', 'device_class': None, 'value': None}]
outbox_information = [{'id': 'shutdown', 'name': 'shutdown_command', 'sensor_label': 'Shutdown', 'icon': 'mdi:power'},
{'id': 'reboot', 'name': 'reboot_command',
'sensor_label': 'Reboot', 'icon': 'mdi:restart'},
{'id': 'lock', 'name': 'lock_command',
'sensor_label': 'Lock', 'icon': 'mdi:lock'}
]
camera_information = {'id': 'screen', 'name': 'screenshot',
'camera_label': 'Screen', 'icon': 'mdi:monitor-clean'}
async def async_setup(hass, config):
hass.data[DOMAIN] = {'data': []}
# index is the number of client info to find them in the hass.data list
for index, client in enumerate(config[DOMAIN][CONF_LIST_KEY]):
client_name = client[CONF_CLIENT_NAME]
topic = 'monitor/' + client_name + '/'
# These hass.data will be passed to the sensors
hass.data[DOMAIN]['data'].append({'client_name': client_name, 'topic': topic, 'inbox_information': deepcopy(inbox_information),
'outbox_information': outbox_information, 'camera_information': camera_information, 'last_message_time': None})
# Load the sensors - that receive and manage clients messages
hass.async_create_task(
async_load_platform(
hass, SENSOR_DOMAIN, DOMAIN, index, config
)
)
# Load the scripts - that send tommands to the client
hass.async_create_task(
async_load_platform(
hass, SWITCH_DOMAIN, DOMAIN, index, config
)
)
hass.async_create_task(
async_load_platform(
hass, BINARY_SENSOR_DOMAIN, DOMAIN, index, config
)
)
hass.async_create_task(
async_load_platform(
hass, CAMERA_DOMAIN, DOMAIN, index, config
)
)
def update(call=None):
"""Mqtt updates automatically when messages arrive"""
pass
hass.services.async_register(DOMAIN, "monitor", update)
return True