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:
Dan Ports 2017-04-30 00:31:46 -07:00 committed by Paulus Schoutsen
parent 8df5de2bb8
commit 7ff1ded0b5
2 changed files with 55 additions and 7 deletions

View File

@ -65,15 +65,20 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
obj_holidays = getattr(holidays, country)(years=year)
if province:
if province not in obj_holidays.PROVINCES and \
province not in obj_holidays.STATES:
# '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 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:")
for date, name in sorted(obj_holidays.items()):

View File

@ -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 = {
'binary_sensor': {
'platform': 'workday',
'country': 'DE',
'province': 'BW',
'workdays': ['holiday', 'mon', 'tue', 'wed', 'thu', 'fri'],
'workdays': ['holiday'],
'excludes': ['sat', 'sun']
},
}
@ -122,6 +137,34 @@ class TestWorkdaySetup(object):
entity = self.hass.states.get('binary_sensor.workday_sensor')
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):
"""Setup workday component."""
with assert_setup_component(1, 'binary_sensor'):