def test_preheat_event_with_sensor_reschedules_on_sensor_change(): hass.time = time_of_day(hour=4) hass.args['sensors'] = [{'entity_id': 'sensor.test'}] hass.states['climate.test'] = {'state': 'Manual', 'attributes': {'temperature':18.0, 'current_temperature':20.5}} hass.states['sensor.test'] = {'state': 10} datapoints = [{'start_temp':18.0, 'target_temp':19.0, 'duration_s':3060.0, 'sensor_readings':[('sensor.test', 12.0)]}, {'start_temp':19.0, 'target_temp':20.0, 'duration_s':3060.0, 'sensor_readings':[('sensor.test', 13.0)]}, {'start_temp':18.0, 'target_temp':20.0, 'duration_s':5100.0, 'sensor_readings':[('sensor.test', 8.0)]}, {'start_temp':20.0, 'target_temp':21.0, 'duration_s':2940.0, 'sensor_readings':[('sensor.test', 16.0)]}] store.data['test'] = {'datapoints': datapoints} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', {'zone': 'test', 'name': 'test', 'type': 'event', 'target_temp': '21', 'target_time': '07:00'}) old_state = hass.states['sensor.test'] new_state = {'state': 10.5} hass.states['sensor.test'] = new_state hass.trigger_state_callback('sensor.test', None, old_state, new_state) hass.advance_time(time_of_day(6, 19, 59)) assert events() == [] hass.advance_time(time_of_day(6, 20)) assert events() == [{'event': 'smartclimate.start_preheat', 'data':{'name': 'test', 'target_temp': 21}}]
def test_ignores_intermediate_states(): '''Test a completed temperature shift gets recorded to the store''' ZoneImpl(hass, store) old_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 18., 'current_temperature': 18. } } new_state = { 'state': 'Manual', 'attributes': { 'temperature': 20., 'current_temperature': 18. } } hass.time = relative_time(0) hass.trigger_state_callback('climate.test', None, old_state, new_state) old_state = new_state new_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 20., 'current_temperature': 19. } } hass.time = relative_time(5100) hass.trigger_state_callback('climate.test', None, old_state, new_state) old_state = new_state new_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 20., 'current_temperature': 20. } } hass.time = relative_time(5100) hass.trigger_state_callback('climate.test', None, old_state, new_state) assert store.saved assert store.data == { '_version': 1, 'test': { 'datapoints': [{ 'start_temp': 18., 'target_temp': 20., 'sensor_readings': [], 'duration_s': 5100.0 }] } }
def test_conitinues_tracking_if_target_temp_raised(): '''nothing is recorded if the target temp is changed while tracking''' ZoneImpl(hass, store) old_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 18., 'current_temperature': 18. } } new_state = { 'state': 'Manual', 'attributes': { 'temperature': 20., 'current_temperature': 18. } } hass.trigger_state_callback('climate.test', None, old_state, new_state) old_state = new_state new_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 21., 'current_temperature': 18.5 } } hass.time = relative_time(1800) hass.trigger_state_callback('climate.test', None, old_state, new_state) old_state = new_state new_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 21., 'current_temperature': 21. } } hass.time = relative_time(6300) hass.trigger_state_callback('climate.test', None, old_state, new_state) assert store.saved assert store.data == { '_version': 1, 'test': { 'datapoints': [{ 'start_temp': 18., 'target_temp': 21., 'sensor_readings': [], 'duration_s': 6300.0 }] } }
def test_preheat_event_default_duration(): hass.time = time_of_day(hour=4) hass.states['climate.test'] = {'state': 'Manual', 'attributes': {'temperature':18.0, 'current_temperature':20.5}} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', {'zone': 'test', 'name': 'test', 'type': 'event', 'target_temp': '21', 'target_time': '07:00'}) hass.advance_time(time_of_day(5, 59, 59)) assert events() == [] hass.advance_time(time_of_day(6, 00)) assert events() == [{'event': 'smartclimate.start_preheat', 'data':{'name': 'test', 'target_temp': 21}}]
def test_preheat_event_can_immediately_trigger(): hass.time = time_of_day(6, 40) hass.states['climate.test'] = {'state': 'Manual', 'attributes': {'temperature':18.0, 'current_temperature':20.5}} datapoints = [{'start_temp':18.0, 'target_temp':19.0, 'duration_s':2700.0, 'sensor_readings':[]}, {'start_temp':19.0, 'target_temp':20.0, 'duration_s':2700.0, 'sensor_readings':[]}, {'start_temp':18.0, 'target_temp':20.0, 'duration_s':4500.0, 'sensor_readings':[]}] store.data['test'] = {'datapoints': datapoints} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', {'zone': 'test', 'name': 'test', 'type': 'event', 'target_temp': 21, 'target_time': '07:00'}) assert events() == [{'event': 'smartclimate.start_preheat', 'data':{'name': 'test', 'target_temp': 21}}]
def test_preheat_sensor_with_sensor_updates_on_sensor_change(): hass.time = time_of_day(hour=4) hass.args['sensors'] = [{'entity_id': 'sensor.test'}] hass.states['climate.test'] = { 'state': 'Manual', 'attributes': { 'temperature': 18.0, 'current_temperature': 20.5 } } hass.states['sensor.test'] = {'state': 10} datapoints = [{ 'start_temp': 18.0, 'target_temp': 19.0, 'duration_s': 3060.0, 'sensor_readings': [('sensor.test', 12.0)] }, { 'start_temp': 19.0, 'target_temp': 20.0, 'duration_s': 3060.0, 'sensor_readings': [('sensor.test', 13.0)] }, { 'start_temp': 18.0, 'target_temp': 20.0, 'duration_s': 5100.0, 'sensor_readings': [('sensor.test', 8.0)] }, { 'start_temp': 20.0, 'target_temp': 21.0, 'duration_s': 2940.0, 'sensor_readings': [('sensor.test', 16.0)] }] store.data['test'] = {'datapoints': datapoints} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', { 'zone': 'test', 'name': 'prediction', 'type': 'sensor', 'target_temp': 21 }) old_state = hass.states['sensor.test'] new_state = {'state': 10.5} hass.states['sensor.test'] = new_state hass.trigger_state_callback('sensor.test', None, old_state, new_state) assert hass.set_states['sensor.prediction'] == { 'state': 2400, 'attributes': { 'target_temp': 21.0 } }
def test_preheat_sensor_updates_trigger_time_on_temp_change(): hass.time = time_of_day(hour=4) hass.states['climate.test'] = { 'state': 'Manual', 'attributes': { 'temperature': 18.0, 'current_temperature': 20.0 } } datapoints = [{ 'start_temp': 18.0, 'target_temp': 19.0, 'duration_s': 2700.0, 'sensor_readings': [] }, { 'start_temp': 19.0, 'target_temp': 20.0, 'duration_s': 2700.0, 'sensor_readings': [] }, { 'start_temp': 18.0, 'target_temp': 20.0, 'duration_s': 4500.0, 'sensor_readings': [] }] store.data['test'] = {'datapoints': datapoints} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', { 'zone': 'test', 'name': 'prediction', 'type': 'sensor', 'target_temp': 21 }) old_state = hass.states['climate.test'] new_state = { 'state': 'Manual', 'attributes': { 'temperature': 18.0, 'current_temperature': 20.5 } } hass.states['climate.test'] = new_state hass.trigger_state_callback('climate.test', None, old_state, new_state) assert hass.set_states['sensor.prediction'] == { 'state': 1800, 'attributes': { 'target_temp': 21.0 } }
def test_clear_preheat_event(): hass.time = time_of_day(hour=4) hass.states['climate.test'] = {'state': 'Manual', 'attributes': {'temperature':18.0, 'current_temperature':20.5}} datapoints = [{'start_temp':18.0, 'target_temp':19.0, 'duration_s':2700.0, 'sensor_readings':[]}, {'start_temp':19.0, 'target_temp':20.0, 'duration_s':2700.0, 'sensor_readings':[]}, {'start_temp':18.0, 'target_temp':20.0, 'duration_s':4500.0, 'sensor_readings':[]}] store.data['test'] = {'datapoints': datapoints} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', {'zone': 'test', 'name': 'test', 'type': 'event', 'target_temp': '21', 'target_time': '07:00'}) hass.trigger_event_callback('smartclimate.clear_preheat', {'zone': 'test', 'name': 'test'}) hass.advance_time(time_of_day(6, 30)) assert events() == []
def test_doesnt_record_missing_sensor_reading(): '''Test a completed temperature shift gets recorded to the store''' store.data[hass.name] = { 'datapoints': [{ 'start_temp': 18., 'target_temp': 20., 'sensor_readings': [('sensor.sensor1', 8.0)], 'duration_s': 5100.0 }] } initial_data = deepcopy(store.data) hass.args['sensors'] = [{'entity_id': 'sensor.sensor1'}] ZoneImpl(hass, store) #hass.states.set('sensor.sensor1', '8.0') old_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 18., 'current_temperature': 18. } } new_state = { 'state': 'Manual', 'attributes': { 'temperature': 20., 'current_temperature': 18. } } hass.time = relative_time(0) hass.trigger_state_callback('climate.test', None, old_state, new_state) old_state = new_state new_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 20., 'current_temperature': 20. } } hass.time = relative_time(5100) hass.trigger_state_callback('climate.test', None, old_state, new_state) assert store.saved is False assert store.data == initial_data
def test_preheat_event_updates_trigger_time_on_temp_change(): hass.time = time_of_day(hour=4) hass.states['climate.test'] = {'state': 'Manual', 'attributes': {'temperature':18.0, 'current_temperature':20.0}} datapoints = [{'start_temp':18.0, 'target_temp':19.0, 'duration_s':2700.0, 'sensor_readings':[]}, {'start_temp':19.0, 'target_temp':20.0, 'duration_s':2700.0, 'sensor_readings':[]}, {'start_temp':18.0, 'target_temp':20.0, 'duration_s':4500.0, 'sensor_readings':[]}] store.data['test'] = {'datapoints': datapoints} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', {'zone': 'test', 'name': 'test', 'type': 'event', 'target_temp': '21', 'target_time': '07:00'}) old_state = hass.states['climate.test'] new_state = {'state': 'Manual', 'attributes': {'temperature':18.0, 'current_temperature':20.5}} hass.states['climate.test'] = new_state hass.trigger_state_callback('climate.test', None, old_state, new_state) hass.advance_time(time_of_day(6, 29, 59)) assert events() == [] hass.advance_time(time_of_day(6, 30)) assert events() == [{'event': 'smartclimate.start_preheat', 'data':{'name': 'test', 'target_temp': 21}}]
def test_preheat_sensor_default_duration(): hass.time = time_of_day(hour=4) hass.states['climate.test'] = { 'state': 'Manual', 'attributes': { 'temperature': 18.0, 'current_temperature': 20.5 } } ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', { 'zone': 'test', 'name': 'prediction', 'type': 'sensor', 'target_temp': 21 }) assert hass.set_states['sensor.prediction'] == { 'state': 3600, 'attributes': { 'target_temp': 21.0 } }
def test_clear_preheat_sensor(): hass.time = time_of_day(hour=4) hass.states['climate.test'] = { 'state': 'Manual', 'attributes': { 'temperature': 18.0, 'current_temperature': 20.5 } } datapoints = [{ 'start_temp': 18.0, 'target_temp': 19.0, 'duration_s': 2700.0, 'sensor_readings': [] }, { 'start_temp': 19.0, 'target_temp': 20.0, 'duration_s': 2700.0, 'sensor_readings': [] }, { 'start_temp': 18.0, 'target_temp': 20.0, 'duration_s': 4500.0, 'sensor_readings': [] }] store.data['test'] = {'datapoints': datapoints} ZoneImpl(hass, store) hass.trigger_event_callback('smartclimate.set_preheat', { 'zone': 'test', 'name': 'prediction', 'type': 'sensor', 'target_temp': 21 }) hass.trigger_event_callback('smartclimate.clear_preheat', { 'zone': 'test', 'name': 'prediction' }) assert hass.set_states['sensor.prediction'] == {'state': 'unknown'}
def test_up_event(): '''Test up event is fired''' ZoneImpl(hass, store) assert hass.fired_events == [{'event': 'smartclimate.up', 'data':{'zone': 'test'}}]
def test_records_additional_sensors(): '''Test a completed temperature shift gets recorded to the store''' hass.args['sensors'] = [{ 'entity_id': 'sensor.sensor1' }, { 'entity_id': 'sensor.sensor2', 'attribute': 'attr' }, { 'name': 'test_sensor', 'entity_id': 'sensor.sensor3' }] ZoneImpl(hass, store) hass.states['sensor.sensor1'] = {'state': 8.0} hass.states['sensor.sensor2'] = { 'state': 'on', 'attributes': { 'attr': '16.0' } } hass.states['sensor.sensor3'] = {'state': '12.0'} old_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 18., 'current_temperature': 18. } } new_state = { 'state': 'Manual', 'attributes': { 'temperature': 20., 'current_temperature': 18. } } hass.time = relative_time(0) hass.trigger_state_callback('climate.test', None, old_state, new_state) hass.states['sensor.sensor1'] = {'state': '9.0'} hass.states['sensor.sensor2'] = { 'state': 'on', 'attributes': { 'attr': '17.0' } } hass.states['sensor.sensor3'] = {'state': '13.0'} old_state = new_state new_state = { 'state': 'Smart Schedule', 'attributes': { 'temperature': 20., 'current_temperature': 20. } } hass.time = relative_time(5100) hass.trigger_state_callback('climate.test', None, old_state, new_state) assert store.saved assert store.data == { '_version': 1, 'test': { 'datapoints': [{ 'start_temp': 18., 'target_temp': 20., 'sensor_readings': [('sensor.sensor1', 8.), ('sensor.sensor2.attr', 16.), ('test_sensor', 12.)], 'duration_s': 5100.0 }] } }