forked from cyberjunky/home-assistant-custom-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remarks.py
152 lines (128 loc) · 5.65 KB
/
remarks.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
145
146
147
148
149
150
151
152
"""
Support for publishing random remarks on eventbus to be used in automation.
file: 1100tags.txt
hour: 21
minute: 0
outside_temp_sensor: sensor.pws_temp_c
cold_threshold: 5
freeze_threshold: -5
temp_hour: 6
temp_minute: 30
"""
import random
from logging import getLogger
import voluptuous as vol
from homeassistant.helpers.event import track_time_change
import homeassistant.helpers.config_validation as cv
_LOGGER = getLogger(__name__)
CONF_FILE = 'file'
CONF_OUTSIDE_T_SENSOR = 'outside_temp_sensor'
CONF_COLD_THRESHOLD = 'cold_threshold'
CONF_FREEZE_THRESHOLD = 'freeze_threshold'
CONF_HOUR = 'hour'
CONF_MINUTE = 'minute'
CONF_TEMP_HOUR = 'temp_hour'
CONF_TEMP_MINUTE = 'temp_minute'
DEFAULT_INTERVAL = 6
DEFAULT_OUTSIDE_T_SENSOR = 'sensor.pws_feelslike_c'
DEFAULT_COLD_THRESHOLD = 15
DEFAULT_FREEZE_THRESHOLD = 4
DEFAULT_FILE = '1100tags.txt'
DEFAULT_HOUR = 9
DEFAULT_MINUTE = 0
DEFAULT_TEMP_HOUR = 6
DEFAULT_TEMP_MINUTE = 30
ATTR_TEXT = 'text'
DOMAIN = 'remarks'
EVENT_REMARKS = 'remarks'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(CONF_FILE, default=DEFAULT_FILE): cv.string,
vol.Optional(CONF_HOUR, default=DEFAULT_HOUR): vol.Any(
vol.Coerce(int),
vol.Coerce(str)),
vol.Optional(CONF_MINUTE, default=DEFAULT_MINUTE): vol.Any(
vol.Coerce(int),
vol.Coerce(str)),
vol.Optional(CONF_OUTSIDE_T_SENSOR,
default=DEFAULT_OUTSIDE_T_SENSOR): cv.entity_id,
vol.Optional(CONF_COLD_THRESHOLD,
default=DEFAULT_COLD_THRESHOLD): vol.Coerce(float),
vol.Optional(CONF_FREEZE_THRESHOLD,
default=DEFAULT_FREEZE_THRESHOLD): vol.Coerce(float),
vol.Optional(CONF_TEMP_HOUR, default=DEFAULT_TEMP_HOUR): vol.Any(
vol.Coerce(int),
vol.Coerce(str)),
vol.Optional(CONF_TEMP_MINUTE, default=DEFAULT_TEMP_MINUTE): vol.Any(
vol.Coerce(int),
vol.Coerce(str)),
}),
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
"""Set up the remarks component."""
remarksfile = config.get(DOMAIN)[CONF_FILE]
outside_temp_sensor = config.get(DOMAIN)[CONF_OUTSIDE_T_SENSOR]
cold_threshold = config.get(DOMAIN)[CONF_COLD_THRESHOLD]
freeze_threshold = config.get(DOMAIN)[CONF_FREEZE_THRESHOLD]
hour = config.get(DOMAIN)[CONF_HOUR]
minute = config.get(DOMAIN)[CONF_MINUTE]
temp_hour = config.get(DOMAIN)[CONF_TEMP_HOUR]
temp_minute = config.get(DOMAIN)[CONF_TEMP_MINUTE]
RemarksManager(remarksfile, outside_temp_sensor, cold_threshold,
freeze_threshold, hour, minute, temp_hour,
temp_minute, hass)
return True
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-few-public-methods
class RemarksManager(object):
"""Get data from remarks files."""
# pylint: disable=too-many-arguments
def __init__(self, remarksfile, temp_sensor,
cold_threshold, freeze_threshold, hour, minute,
temp_hour, temp_minute, hass):
"""Initialize the data object."""
self._hass = hass
self._cfgdir = self._hass.config.config_dir
self._remarksfile = self._cfgdir+'/remarks/'+remarksfile
self._temp_sensor = temp_sensor
self._cold_threshold = cold_threshold
self._cold_temp_file = self._cfgdir+'/remarks/list_temp_below_20.txt'
self._freeze_threshold = freeze_threshold
self._freeze_temp_file = self._cfgdir+'/remarks/list_temp_below_0.txt'
self._remark = None
self._hour = hour
self._minute = minute
self._temp_hour = temp_hour
self._temp_minute = temp_minute
track_time_change(hass, lambda now: self._get_remark(),
hour=self._hour, minute=self._minute, second=0)
track_time_change(hass, lambda now: self._get_temp_remark(),
hour=self._temp_hour, minute=self._temp_minute,
second=0)
def _get_remark(self):
"""Grab random remark from file and publish it to the event bus."""
_LOGGER.debug('Fetching remark from file "%s"', self._remarksfile)
lines = open(str(self._remarksfile)).read().splitlines()
self._remark = random.choice(lines)
_LOGGER.debug('Fire event for new remark')
self._hass.bus.fire(EVENT_REMARKS, {ATTR_TEXT: self._remark})
def _get_temp_remark(self):
"""Grab random tempremark from file and publish it on event bus."""
sensor = self._hass.states.get(self._temp_sensor)
unit = sensor.attributes.get('unit_of_measurement')
if float(sensor.state) < float(self._freeze_threshold):
_LOGGER.debug('Fetching remark from "%s"', self._freeze_temp_file)
lines = open(str(self._freeze_temp_file)).read().splitlines()
self._remark = 'It is currently ' + sensor.state + unit +\
' outside. ' + random.choice(lines)
_LOGGER.debug('Event for freezetemp remark, temp is %s%s',
sensor.state, unit)
self._hass.bus.fire(EVENT_REMARKS, {ATTR_TEXT: self._remark})
elif float(sensor.state) < float(self._cold_threshold):
_LOGGER.debug('Fetching remark from "%s"', self._cold_temp_file)
lines = open(str(self._cold_temp_file)).read().splitlines()
self._remark = 'It is currently ' + sensor.state + unit +\
' outside. '+random.choice(lines)
_LOGGER.debug('Event for coldtemp remark, temp is%s%s',
sensor.state, unit)
self._hass.bus.fire(EVENT_REMARKS, {ATTR_TEXT: self._remark})