forked from home-assistant/core
/
binary_sensor.py
269 lines (236 loc) · 6.71 KB
/
binary_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Sensor to indicate whether the current day is a workday."""
import logging
from datetime import datetime, timedelta
import holidays
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, WEEKDAYS
from homeassistant.components.binary_sensor import BinarySensorDevice
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
# List of all countries currently supported by holidays
# Source: https://github.com/dr-prodigy/python-holidays#available-countries
# There seems to be no way to get the list out at runtime
ALL_COUNTRIES = [
"Argentina",
"AR",
"Aruba",
"AW",
"Australia",
"AU",
"Austria",
"AT",
"Brazil",
"BR",
"Belarus",
"BY",
"Belgium",
"BE",
"Bulgaria",
"BG",
"Canada",
"CA",
"Colombia",
"CO",
"Croatia",
"HR",
"Czech",
"CZ",
"Denmark",
"DK",
"England",
"Estonia",
"EE",
"EuropeanCentralBank",
"ECB",
"TAR",
"Finland",
"FI",
"France",
"FRA",
"Germany",
"DE",
"Hungary",
"HU",
"Iceland",
"IS",
"India",
"IND",
"Ireland",
"IE",
"Isle of Man",
"Italy",
"IT",
"Japan",
"JP",
"Kenya",
"KE",
"Lithuania",
"LT",
"Luxembourg",
"LU",
"Mexico",
"MX",
"Netherlands",
"NL",
"NewZealand",
"NZ",
"Northern Ireland",
"Norway",
"NO",
"Peru",
"PE",
"Poland",
"PL",
"Portugal",
"PT",
"PortugalExt",
"PTE",
"Russia",
"RU",
"Scotland",
"Slovenia",
"SI",
"Slovakia",
"SK",
"South Africa",
"ZA",
"Spain",
"ES",
"Sweden",
"SE",
"Switzerland",
"CH",
"Ukraine",
"UA",
"UnitedKingdom",
"UK",
"UnitedStates",
"US",
"Wales",
]
ALLOWED_DAYS = WEEKDAYS + ["holiday"]
CONF_COUNTRY = "country"
CONF_PROVINCE = "province"
CONF_WORKDAYS = "workdays"
CONF_EXCLUDES = "excludes"
CONF_OFFSET = "days_offset"
CONF_ADD_HOLIDAYS = "add_holidays"
# By default, Monday - Friday are workdays
DEFAULT_WORKDAYS = ["mon", "tue", "wed", "thu", "fri"]
# By default, public holidays, Saturdays and Sundays are excluded from workdays
DEFAULT_EXCLUDES = ["sat", "sun", "holiday"]
DEFAULT_NAME = "Workday Sensor"
DEFAULT_OFFSET = 0
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_COUNTRY): vol.In(ALL_COUNTRIES),
vol.Optional(CONF_EXCLUDES, default=DEFAULT_EXCLUDES): vol.All(
cv.ensure_list, [vol.In(ALLOWED_DAYS)]
),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_OFFSET, default=DEFAULT_OFFSET): vol.Coerce(int),
vol.Optional(CONF_PROVINCE): cv.string,
vol.Optional(CONF_WORKDAYS, default=DEFAULT_WORKDAYS): vol.All(
cv.ensure_list, [vol.In(ALLOWED_DAYS)]
),
vol.Optional(CONF_ADD_HOLIDAYS): vol.All(cv.ensure_list, [cv.string]),
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Workday sensor."""
sensor_name = config.get(CONF_NAME)
country = config.get(CONF_COUNTRY)
province = config.get(CONF_PROVINCE)
workdays = config.get(CONF_WORKDAYS)
excludes = config.get(CONF_EXCLUDES)
days_offset = config.get(CONF_OFFSET)
add_holidays = config.get(CONF_ADD_HOLIDAYS)
year = (get_date(datetime.today()) + timedelta(days=days_offset)).year
obj_holidays = getattr(holidays, country)(years=year)
if province:
# 'state' and 'prov' are not interchangeable, so need to make
# sure we use the right one
if hasattr(obj_holidays, "PROVINCES") and province in obj_holidays.PROVINCES:
obj_holidays = getattr(holidays, country)(prov=province, years=year)
elif hasattr(obj_holidays, "STATES") and province in obj_holidays.STATES:
obj_holidays = getattr(holidays, country)(state=province, years=year)
else:
_LOGGER.error(
"There is no province/state %s in country %s", province, country
)
return
# Add custom holidays
try:
obj_holidays.append(add_holidays)
except TypeError:
_LOGGER.debug("No custom holidays or invalid holidays")
_LOGGER.debug("Found the following holidays for your configuration:")
for date, name in sorted(obj_holidays.items()):
_LOGGER.debug("%s %s", date, name)
add_entities(
[IsWorkdaySensor(obj_holidays, workdays, excludes, days_offset, sensor_name)],
True,
)
def day_to_string(day):
"""Convert day index 0 - 7 to string."""
try:
return ALLOWED_DAYS[day]
except IndexError:
return None
def get_date(date):
"""Return date. Needed for testing."""
return date
class IsWorkdaySensor(BinarySensorDevice):
"""Implementation of a Workday sensor."""
def __init__(self, obj_holidays, workdays, excludes, days_offset, name):
"""Initialize the Workday sensor."""
self._name = name
self._obj_holidays = obj_holidays
self._workdays = workdays
self._excludes = excludes
self._days_offset = days_offset
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def is_on(self):
"""Return the state of the device."""
return self._state
def is_include(self, day, now):
"""Check if given day is in the includes list."""
if day in self._workdays:
return True
if "holiday" in self._workdays and now in self._obj_holidays:
return True
return False
def is_exclude(self, day, now):
"""Check if given day is in the excludes list."""
if day in self._excludes:
return True
if "holiday" in self._excludes and now in self._obj_holidays:
return True
return False
@property
def state_attributes(self):
"""Return the attributes of the entity."""
# return self._attributes
return {
CONF_WORKDAYS: self._workdays,
CONF_EXCLUDES: self._excludes,
CONF_OFFSET: self._days_offset,
}
async def async_update(self):
"""Get date and look whether it is a holiday."""
# Default is no workday
self._state = False
# Get iso day of the week (1 = Monday, 7 = Sunday)
date = get_date(datetime.today()) + timedelta(days=self._days_offset)
day = date.isoweekday() - 1
day_of_week = day_to_string(day)
if self.is_include(day_of_week, date):
self._state = True
if self.is_exclude(day_of_week, date):
self._state = False