mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
binary_sensor.workday: fix handling of states vs provinces (#7162)
* binary_sensor.workday: fix handling of states vs provinces * Add test cases for workday sensor with states * remove redundant assignment * Repair unit test to improve coverage Patch from Wolf-Bastian Pöttner * Fix handling of invalid states/provinces * fix indentation to satisfy pylint
This commit is contained in:
parent
8df5de2bb8
commit
7ff1ded0b5
@ -65,15 +65,20 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
obj_holidays = getattr(holidays, country)(years=year)
|
obj_holidays = getattr(holidays, country)(years=year)
|
||||||
|
|
||||||
if province:
|
if province:
|
||||||
if province not in obj_holidays.PROVINCES and \
|
# 'state' and 'prov' are not interchangeable, so need to make
|
||||||
province not in obj_holidays.STATES:
|
# 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",
|
_LOGGER.error("There is no province/state %s in country %s",
|
||||||
province, country)
|
province, country)
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
year = datetime.datetime.now().year
|
|
||||||
obj_holidays = getattr(holidays, country)(prov=province,
|
|
||||||
years=year)
|
|
||||||
|
|
||||||
_LOGGER.debug("Found the following holidays for your configuration:")
|
_LOGGER.debug("Found the following holidays for your configuration:")
|
||||||
for date, name in sorted(obj_holidays.items()):
|
for date, name in sorted(obj_holidays.items()):
|
||||||
|
@ -38,12 +38,27 @@ class TestWorkdaySetup(object):
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.config_state = {
|
||||||
|
'binary_sensor': {
|
||||||
|
'platform': 'workday',
|
||||||
|
'country': 'US',
|
||||||
|
'province': 'CA'
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
self.config_nostate = {
|
||||||
|
'binary_sensor': {
|
||||||
|
'platform': 'workday',
|
||||||
|
'country': 'US',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
self.config_includeholiday = {
|
self.config_includeholiday = {
|
||||||
'binary_sensor': {
|
'binary_sensor': {
|
||||||
'platform': 'workday',
|
'platform': 'workday',
|
||||||
'country': 'DE',
|
'country': 'DE',
|
||||||
'province': 'BW',
|
'province': 'BW',
|
||||||
'workdays': ['holiday', 'mon', 'tue', 'wed', 'thu', 'fri'],
|
'workdays': ['holiday'],
|
||||||
'excludes': ['sat', 'sun']
|
'excludes': ['sat', 'sun']
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -122,6 +137,34 @@ class TestWorkdaySetup(object):
|
|||||||
entity = self.hass.states.get('binary_sensor.workday_sensor')
|
entity = self.hass.states.get('binary_sensor.workday_sensor')
|
||||||
assert entity.state == 'on'
|
assert entity.state == 'on'
|
||||||
|
|
||||||
|
# Freeze time to a public holiday in state CA
|
||||||
|
@freeze_time("Mar 31st, 2017")
|
||||||
|
def test_public_holiday_state(self):
|
||||||
|
"""Test if public holidays are reported correctly."""
|
||||||
|
with assert_setup_component(1, 'binary_sensor'):
|
||||||
|
setup_component(self.hass, 'binary_sensor', self.config_state)
|
||||||
|
|
||||||
|
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
|
||||||
|
|
||||||
|
self.hass.start()
|
||||||
|
|
||||||
|
entity = self.hass.states.get('binary_sensor.workday_sensor')
|
||||||
|
assert entity.state == 'off'
|
||||||
|
|
||||||
|
# Freeze time to a public holiday in state CA
|
||||||
|
@freeze_time("Mar 31st, 2017")
|
||||||
|
def test_public_holiday_nostate(self):
|
||||||
|
"""Test if public holidays are reported correctly."""
|
||||||
|
with assert_setup_component(1, 'binary_sensor'):
|
||||||
|
setup_component(self.hass, 'binary_sensor', self.config_nostate)
|
||||||
|
|
||||||
|
assert self.hass.states.get('binary_sensor.workday_sensor') is not None
|
||||||
|
|
||||||
|
self.hass.start()
|
||||||
|
|
||||||
|
entity = self.hass.states.get('binary_sensor.workday_sensor')
|
||||||
|
assert entity.state == 'on'
|
||||||
|
|
||||||
def test_setup_component_invalidprovince(self):
|
def test_setup_component_invalidprovince(self):
|
||||||
"""Setup workday component."""
|
"""Setup workday component."""
|
||||||
with assert_setup_component(1, 'binary_sensor'):
|
with assert_setup_component(1, 'binary_sensor'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user