Skip to content

Commit

Permalink
binary_sensor.workday: fix handling of states vs provinces (#7162)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
drkp authored and balloob committed Apr 30, 2017
1 parent 8df5de2 commit 7ff1ded
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
17 changes: 11 additions & 6 deletions homeassistant/components/binary_sensor/workday.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand Down
45 changes: 44 additions & 1 deletion tests/components/binary_sensor/test_workday.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,27 @@ def setup_method(self):
},
}

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']
},
}
Expand Down Expand Up @@ -122,6 +137,34 @@ def test_public_holiday_noprovince(self):
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'):
Expand Down

0 comments on commit 7ff1ded

Please sign in to comment.