diff --git a/homeassistant/components/binary_sensor/workday.py b/homeassistant/components/binary_sensor/workday.py index 57c4533fb5d..81cc8fd8798 100644 --- a/homeassistant/components/binary_sensor/workday.py +++ b/homeassistant/components/binary_sensor/workday.py @@ -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()): diff --git a/tests/components/binary_sensor/test_workday.py b/tests/components/binary_sensor/test_workday.py index 814606613f5..c828a3b0cd6 100644 --- a/tests/components/binary_sensor/test_workday.py +++ b/tests/components/binary_sensor/test_workday.py @@ -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'):