Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binary_sensor.workday: fix handling of states vs provinces #7162

Merged
merged 6 commits into from
Apr 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions homeassistant/components/binary_sensor/workday.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,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