desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant() self.handler_method = None self.hass.bus.listen = mock.Mock()
'Clear data.'
def tearDown(self):
self.hass.stop()
'Test the setup with full configuration.'
def test_setup_config_full(self, mock_client):
config = {'influxdb': {'host': 'host', 'port': 123, 'database': 'db', 'username': 'user', 'password': 'password', 'ssl': 'False', 'verify_ssl': 'False'}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.assertTrue(self.hass.bus.listen.called) self.assertEqual(EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0]) self.assertTrue(mock_client.return_value.query.called)
'Test the setup with default configuration.'
def test_setup_config_defaults(self, mock_client):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass'}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.assertTrue(self.hass.bus.listen.called) self.assertEqual(EVENT_STATE_CHANGED, self.hass.bus.listen.call_args_list[0][0][0])
'Test the setup with minimal configuration.'
def test_setup_minimal_config(self, mock_client):
config = {'influxdb': {}} assert setup_component(self.hass, influxdb.DOMAIN, config)
'Test the setup with existing username and missing password.'
def test_setup_missing_password(self, mock_client):
config = {'influxdb': {'username': 'user'}} assert (not setup_component(self.hass, influxdb.DOMAIN, config))
'Test the setup for query failures.'
def test_setup_query_fail(self, mock_client):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass'}} mock_client.return_value.query.side_effect = influx_client.exceptions.InfluxDBClientError('fake') assert (not setup_component(self.hass, influxdb.DOMAIN, config))
'Setup the client.'
def _setup(self):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass', 'exclude': {'entities': ['fake.blacklisted'], 'domains': ['another_fake']}}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]
'Test the event listener.'
def test_event_listener(self, mock_client):
self._setup() valid = {'1': 1, '1.0': 1.0, STATE_ON: 1, STATE_OFF: 0, 'foo': 'foo'} for (in_, out) in valid.items(): attrs = {'unit_of_measurement': 'foobars', 'longitude': '1.1', 'latitude': '2.2', 'battery_level': '99%', 'temperature': '20c', 'last_seen': 'Last seen 23 minutes ago', 'updated_at': datetime.datetime(2017, 1, 1, 0, 0), 'multi_periods': '0.120.240.2023873'} state = mock.MagicMock(state=in_, domain='fake', entity_id='fake.entity-id', object_id='entity', attributes=attrs) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) if isinstance(out, str): body = [{'measurement': 'foobars', 'tags': {'domain': 'fake', 'entity_id': 'entity'}, 'time': 12345, 'fields': {'state': out, 'longitude': 1.1, 'latitude': 2.2, 'battery_level_str': '99%', 'battery_level': 99.0, 'temperature_str': '20c', 'temperature': 20.0, 'last_seen_str': 'Last seen 23 minutes ago', 'last_seen': 23.0, 'updated_at_str': '2017-01-01 00:00:00', 'updated_at': 20170101000000, 'multi_periods_str': '0.120.240.2023873'}}] else: body = [{'measurement': 'foobars', 'tags': {'domain': 'fake', 'entity_id': 'entity'}, 'time': 12345, 'fields': {'value': out, 'longitude': 1.1, 'latitude': 2.2, 'battery_level_str': '99%', 'battery_level': 99.0, 'temperature_str': '20c', 'temperature': 20.0, 'last_seen_str': 'Last seen 23 minutes ago', 'last_seen': 23.0, 'updated_at_str': '2017-01-01 00:00:00', 'updated_at': 20170101000000, 'multi_periods_str': '0.120.240.2023873'}}] self.handler_method(event) self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) mock_client.return_value.write_points.reset_mock()
'Test the event listener for missing units.'
def test_event_listener_no_units(self, mock_client):
self._setup() for unit in (None, ''): if unit: attrs = {'unit_of_measurement': unit} else: attrs = {} state = mock.MagicMock(state=1, domain='fake', entity_id='fake.entity-id', object_id='entity', attributes=attrs) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': 'fake.entity-id', 'tags': {'domain': 'fake', 'entity_id': 'entity'}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) mock_client.return_value.write_points.reset_mock()
'Test the event listener for write failures.'
def test_event_listener_fail_write(self, mock_client):
self._setup() state = mock.MagicMock(state=1, domain='fake', entity_id='fake.entity-id', object_id='entity', attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) mock_client.return_value.write_points.side_effect = influx_client.exceptions.InfluxDBClientError('foo') self.handler_method(event)
'Test the event listener against ignored states.'
def test_event_listener_states(self, mock_client):
self._setup() for state_state in (1, 'unknown', '', 'unavailable'): state = mock.MagicMock(state=state_state, domain='fake', entity_id='fake.entity-id', object_id='entity', attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': 'fake.entity-id', 'tags': {'domain': 'fake', 'entity_id': 'entity'}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) if (state_state == 1): self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) else: self.assertFalse(mock_client.return_value.write_points.called) mock_client.return_value.write_points.reset_mock()
'Test the event listener against a blacklist.'
def test_event_listener_blacklist(self, mock_client):
self._setup() for entity_id in ('ok', 'blacklisted'): state = mock.MagicMock(state=1, domain='fake', entity_id='fake.{}'.format(entity_id), object_id=entity_id, attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': 'fake.{}'.format(entity_id), 'tags': {'domain': 'fake', 'entity_id': entity_id}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) if (entity_id == 'ok'): self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) else: self.assertFalse(mock_client.return_value.write_points.called) mock_client.return_value.write_points.reset_mock()
'Test the event listener against a blacklist.'
def test_event_listener_blacklist_domain(self, mock_client):
self._setup() for domain in ('ok', 'another_fake'): state = mock.MagicMock(state=1, domain=domain, entity_id='{}.something'.format(domain), object_id='something', attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': '{}.something'.format(domain), 'tags': {'domain': domain, 'entity_id': 'something'}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) if (domain == 'ok'): self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) else: self.assertFalse(mock_client.return_value.write_points.called) mock_client.return_value.write_points.reset_mock()
'Test the event listener against a whitelist.'
def test_event_listener_whitelist(self, mock_client):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass', 'include': {'entities': ['fake.included']}}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.handler_method = self.hass.bus.listen.call_args_list[0][0][1] for entity_id in ('included', 'default'): state = mock.MagicMock(state=1, domain='fake', entity_id='fake.{}'.format(entity_id), object_id=entity_id, attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': 'fake.{}'.format(entity_id), 'tags': {'domain': 'fake', 'entity_id': entity_id}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) if (entity_id == 'included'): self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) else: self.assertFalse(mock_client.return_value.write_points.called) mock_client.return_value.write_points.reset_mock()
'Test the event listener against a whitelist.'
def test_event_listener_whitelist_domain(self, mock_client):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass', 'include': {'domains': ['fake']}}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.handler_method = self.hass.bus.listen.call_args_list[0][0][1] for domain in ('fake', 'another_fake'): state = mock.MagicMock(state=1, domain=domain, entity_id='{}.something'.format(domain), object_id='something', attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': '{}.something'.format(domain), 'tags': {'domain': domain, 'entity_id': 'something'}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) if (domain == 'fake'): self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) else: self.assertFalse(mock_client.return_value.write_points.called) mock_client.return_value.write_points.reset_mock()
'Test the event listener when an attribute has an invalid type.'
def test_event_listener_invalid_type(self, mock_client):
self._setup() valid = {'1': 1, '1.0': 1.0, STATE_ON: 1, STATE_OFF: 0, 'foo': 'foo'} for (in_, out) in valid.items(): attrs = {'unit_of_measurement': 'foobars', 'longitude': '1.1', 'latitude': '2.2', 'invalid_attribute': ['value1', 'value2']} state = mock.MagicMock(state=in_, domain='fake', entity_id='fake.entity-id', object_id='entity', attributes=attrs) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) if isinstance(out, str): body = [{'measurement': 'foobars', 'tags': {'domain': 'fake', 'entity_id': 'entity'}, 'time': 12345, 'fields': {'state': out, 'longitude': 1.1, 'latitude': 2.2, 'invalid_attribute_str': "['value1', 'value2']"}}] else: body = [{'measurement': 'foobars', 'tags': {'domain': 'fake', 'entity_id': 'entity'}, 'time': 12345, 'fields': {'value': float(out), 'longitude': 1.1, 'latitude': 2.2, 'invalid_attribute_str': "['value1', 'value2']"}}] self.handler_method(event) self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) mock_client.return_value.write_points.reset_mock()
'Test the event listener with a default measurement.'
def test_event_listener_default_measurement(self, mock_client):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass', 'default_measurement': 'state', 'exclude': {'entities': ['fake.blacklisted']}}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.handler_method = self.hass.bus.listen.call_args_list[0][0][1] for entity_id in ('ok', 'blacklisted'): state = mock.MagicMock(state=1, domain='fake', entity_id='fake.{}'.format(entity_id), object_id=entity_id, attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': 'state', 'tags': {'domain': 'fake', 'entity_id': entity_id}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) if (entity_id == 'ok'): self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) else: self.assertFalse(mock_client.return_value.write_points.called) mock_client.return_value.write_points.reset_mock()
'Test the event listener when some attributes should be tags.'
def test_event_listener_tags_attributes(self, mock_client):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass', 'tags_attributes': ['friendly_fake']}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.handler_method = self.hass.bus.listen.call_args_list[0][0][1] attrs = {'friendly_fake': 'tag_str', 'field_fake': 'field_str'} state = mock.MagicMock(state=1, domain='fake', entity_id='fake.something', object_id='something', attributes=attrs) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': 'fake.something', 'tags': {'domain': 'fake', 'entity_id': 'something', 'friendly_fake': 'tag_str'}, 'time': 12345, 'fields': {'value': 1, 'field_fake_str': 'field_str'}}] self.handler_method(event) self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) mock_client.return_value.write_points.reset_mock()
'Test the event listener with overrided measurements.'
def test_event_listener_component_override_measurement(self, mock_client):
config = {'influxdb': {'host': 'host', 'username': 'user', 'password': 'pass', 'component_config': {'sensor.fake_humidity': {'override_measurement': 'humidity'}}, 'component_config_glob': {'binary_sensor.*motion': {'override_measurement': 'motion'}}, 'component_config_domain': {'climate': {'override_measurement': 'hvac'}}}} assert setup_component(self.hass, influxdb.DOMAIN, config) self.handler_method = self.hass.bus.listen.call_args_list[0][0][1] test_components = [{'domain': 'sensor', 'id': 'fake_humidity', 'res': 'humidity'}, {'domain': 'binary_sensor', 'id': 'fake_motion', 'res': 'motion'}, {'domain': 'climate', 'id': 'fake_thermostat', 'res': 'hvac'}, {'domain': 'other', 'id': 'just_fake', 'res': 'other.just_fake'}] for comp in test_components: state = mock.MagicMock(state=1, domain=comp['domain'], entity_id=((comp['domain'] + '.') + comp['id']), object_id=comp['id'], attributes={}) event = mock.MagicMock(data={'new_state': state}, time_fired=12345) body = [{'measurement': comp['res'], 'tags': {'domain': comp['domain'], 'entity_id': comp['id']}, 'time': 12345, 'fields': {'value': 1}}] self.handler_method(event) self.assertEqual(mock_client.return_value.write_points.call_count, 1) self.assertEqual(mock_client.return_value.write_points.call_args, mock.call(body)) mock_client.return_value.write_points.reset_mock()
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant()
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Test request config with least amount of data.'
def test_request_least_info(self):
request_id = configurator.request_config(self.hass, 'Test Request', (lambda _: None)) self.assertEqual(1, len(self.hass.services.services.get(configurator.DOMAIN, [])), 'No new service registered') states = self.hass.states.all() self.assertEqual(1, len(states), 'Expected a new state registered') state = states[0] self.assertEqual(configurator.STATE_CONFIGURE, state.state) self.assertEqual(request_id, state.attributes.get(configurator.ATTR_CONFIGURE_ID))
'Test request config with all possible info.'
def test_request_all_info(self):
exp_attr = {ATTR_FRIENDLY_NAME: 'Test Request', configurator.ATTR_DESCRIPTION: 'config description', configurator.ATTR_DESCRIPTION_IMAGE: 'config image url', configurator.ATTR_SUBMIT_CAPTION: 'config submit caption', configurator.ATTR_FIELDS: [], configurator.ATTR_LINK_NAME: 'link name', configurator.ATTR_LINK_URL: 'link url', configurator.ATTR_ENTITY_PICTURE: 'config entity picture', configurator.ATTR_CONFIGURE_ID: configurator.request_config(self.hass, name='Test Request', callback=(lambda _: None), description='config description', description_image='config image url', submit_caption='config submit caption', fields=None, link_name='link name', link_url='link url', entity_picture='config entity picture')} states = self.hass.states.all() self.assertEqual(1, len(states)) state = states[0] self.assertEqual(configurator.STATE_CONFIGURE, state.state) assert (exp_attr == dict(state.attributes))
'Test if our callback gets called when configure service called.'
def test_callback_called_on_configure(self):
calls = [] request_id = configurator.request_config(self.hass, 'Test Request', (lambda _: calls.append(1))) self.hass.services.call(configurator.DOMAIN, configurator.SERVICE_CONFIGURE, {configurator.ATTR_CONFIGURE_ID: request_id}) self.hass.block_till_done() self.assertEqual(1, len(calls), 'Callback not called')
'Test state change on notify errors.'
def test_state_change_on_notify_errors(self):
request_id = configurator.request_config(self.hass, 'Test Request', (lambda _: None)) error = 'Oh no bad bad bad' configurator.notify_errors(request_id, error) state = self.hass.states.all()[0] self.assertEqual(error, state.attributes.get(configurator.ATTR_ERRORS))
'Test if notify errors fails silently with a bad request id.'
def test_notify_errors_fail_silently_on_bad_request_id(self):
configurator.notify_errors(2015, 'Try this error')
'Test if calling request done works.'
def test_request_done_works(self):
request_id = configurator.request_config(self.hass, 'Test Request', (lambda _: None)) configurator.request_done(request_id) self.assertEqual(1, len(self.hass.states.all())) self.hass.bus.fire(EVENT_TIME_CHANGED) self.hass.block_till_done() self.assertEqual(0, len(self.hass.states.all()))
'Test that request_done fails silently with a bad request id.'
def test_request_done_fail_silently_on_bad_request_id(self):
configurator.request_done(2016)
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant()
'Stop down everything that was started.'
def tearDown(self):
self.hass.stop()
'Test introduction setup.'
def test_setup(self):
self.assertTrue(setup_component(self.hass, introduction.DOMAIN, {}))
'Create new Mock Dyson State.'
def __init__(self):
pass
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant()
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Test setup component with no devices.'
def test_setup_component_with_no_devices(self):
self.hass.data[dyson.DYSON_DEVICES] = [] add_devices = mock.MagicMock() dyson.setup_platform(self.hass, None, add_devices) add_devices.assert_called_with([])
'Test setup component with devices.'
def test_setup_component(self):
def _add_device(devices): assert (len(devices) == 1) assert (devices[0].name == 'Device_name') device_fan = _get_device_on() device_non_fan = _get_device_off() self.hass.data[dyson.DYSON_DEVICES] = [device_fan, device_non_fan] dyson.setup_platform(self.hass, None, _add_device)
'Test set fan speed.'
def test_dyson_set_speed(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.should_poll) component.set_speed('1') set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.FAN, fan_speed=FanSpeed.FAN_SPEED_1) component.set_speed('AUTO') set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.AUTO)
'Test turn on fan.'
def test_dyson_turn_on(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.should_poll) component.turn_on() set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.FAN)
'Test turn on fan with night mode.'
def test_dyson_turn_night_mode(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.should_poll) component.night_mode(True) set_config = device.set_configuration set_config.assert_called_with(night_mode=NightMode.NIGHT_MODE_ON) component.night_mode(False) set_config = device.set_configuration set_config.assert_called_with(night_mode=NightMode.NIGHT_MODE_OFF)
'Test night mode.'
def test_is_night_mode(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.is_night_mode) device = _get_device_off() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertTrue(component.is_night_mode)
'Test turn on/off fan with auto mode.'
def test_dyson_turn_auto_mode(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.should_poll) component.auto_mode(True) set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.AUTO) component.auto_mode(False) set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.FAN)
'Test auto mode.'
def test_is_auto_mode(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.is_auto_mode) device = _get_device_auto() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertTrue(component.is_auto_mode)
'Test turn on fan with specified speed.'
def test_dyson_turn_on_speed(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.should_poll) component.turn_on('1') set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.FAN, fan_speed=FanSpeed.FAN_SPEED_1) component.turn_on('AUTO') set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.AUTO)
'Test turn off fan.'
def test_dyson_turn_off(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.should_poll) component.turn_off() set_config = device.set_configuration set_config.assert_called_with(fan_mode=FanMode.OFF)
'Test turn off oscillation.'
def test_dyson_oscillate_off(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) component.oscillate(False) set_config = device.set_configuration set_config.assert_called_with(oscillation=Oscillation.OSCILLATION_OFF)
'Test turn on oscillation.'
def test_dyson_oscillate_on(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) component.oscillate(True) set_config = device.set_configuration set_config.assert_called_with(oscillation=Oscillation.OSCILLATION_ON)
'Test get oscillation value on.'
def test_dyson_oscillate_value_on(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertTrue(component.oscillating)
'Test get oscillation value off.'
def test_dyson_oscillate_value_off(self):
device = _get_device_off() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.oscillating)
'Test device is on.'
def test_dyson_on(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertTrue(component.is_on)
'Test device is off.'
def test_dyson_off(self):
device = _get_device_off() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.is_on) device = _get_device_with_no_state() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertFalse(component.is_on)
'Test get device speed.'
def test_dyson_get_speed(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertEqual(component.speed, 1) device = _get_device_off() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertEqual(component.speed, 4) device = _get_device_with_no_state() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertIsNone(component.speed) device = _get_device_auto() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertEqual(component.speed, 'AUTO')
'Test get device direction.'
def test_dyson_get_direction(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertIsNone(component.current_direction)
'Test get speeds list.'
def test_dyson_get_speed_list(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertEqual(len(component.speed_list), 11)
'Test supported features.'
def test_dyson_supported_features(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) self.assertEqual(component.supported_features, 3)
'Test when message is received.'
def test_on_message(self):
device = _get_device_on() component = dyson.DysonPureCoolLinkDevice(self.hass, device) component.entity_id = 'entity_id' component.schedule_update_ha_state = mock.Mock() component.on_message(MockDysonState()) component.schedule_update_ha_state.assert_called_with()
'Test set night mode service.'
def test_service_set_night_mode(self):
dyson_device = mock.MagicMock() self.hass.data[DYSON_DEVICES] = [] dyson_device.entity_id = 'fan.living_room' self.hass.data[dyson.DYSON_FAN_DEVICES] = [dyson_device] dyson.setup_platform(self.hass, None, mock.MagicMock()) self.hass.services.call(dyson.DOMAIN, dyson.SERVICE_SET_NIGHT_MODE, {'entity_id': 'fan.bed_room', 'night_mode': True}, True) assert (not dyson_device.night_mode.called) self.hass.services.call(dyson.DOMAIN, dyson.SERVICE_SET_NIGHT_MODE, {'entity_id': 'fan.living_room', 'night_mode': True}, True) dyson_device.night_mode.assert_called_with(True)
'Initialize the fan.'
def __init__(self):
pass
'Set up test data.'
def setUp(self):
self.fan = BaseFan()
'Tear down unit test data.'
def tearDown(self):
self.fan = None
'Test fan entity methods.'
def test_fanentity(self):
self.assertIsNone(self.fan.state) self.assertEqual(0, len(self.fan.speed_list)) self.assertEqual(0, self.fan.supported_features) self.assertEqual({}, self.fan.state_attributes) self.fan.set_speed() self.fan.oscillate() with self.assertRaises(NotImplementedError): self.fan.turn_on() with self.assertRaises(NotImplementedError): self.fan.turn_off()
'Helper method to get the fan entity.'
def get_entity(self):
return self.hass.states.get(FAN_ENTITY_ID)
'Initialize unit test data.'
def setUp(self):
self.hass = get_test_home_assistant() self.assertTrue(setup_component(self.hass, fan.DOMAIN, {'fan': {'platform': 'demo'}})) self.hass.block_till_done()
'Tear down unit test data.'
def tearDown(self):
self.hass.stop()
'Test turning on the device.'
def test_turn_on(self):
self.assertEqual(STATE_OFF, self.get_entity().state) fan.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertNotEqual(STATE_OFF, self.get_entity().state) fan.turn_on(self.hass, FAN_ENTITY_ID, fan.SPEED_HIGH) self.hass.block_till_done() self.assertEqual(STATE_ON, self.get_entity().state) self.assertEqual(fan.SPEED_HIGH, self.get_entity().attributes[fan.ATTR_SPEED])
'Test turning off the device.'
def test_turn_off(self):
self.assertEqual(STATE_OFF, self.get_entity().state) fan.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertNotEqual(STATE_OFF, self.get_entity().state) fan.turn_off(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertEqual(STATE_OFF, self.get_entity().state)
'Test turning off all fans.'
def test_turn_off_without_entity_id(self):
self.assertEqual(STATE_OFF, self.get_entity().state) fan.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertNotEqual(STATE_OFF, self.get_entity().state) fan.turn_off(self.hass) self.hass.block_till_done() self.assertEqual(STATE_OFF, self.get_entity().state)
'Test setting the direction of the device.'
def test_set_direction(self):
self.assertEqual(STATE_OFF, self.get_entity().state) fan.set_direction(self.hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE) self.hass.block_till_done() self.assertEqual(fan.DIRECTION_REVERSE, self.get_entity().attributes.get('direction'))
'Test setting the speed of the device.'
def test_set_speed(self):
self.assertEqual(STATE_OFF, self.get_entity().state) fan.set_speed(self.hass, FAN_ENTITY_ID, fan.SPEED_LOW) self.hass.block_till_done() self.assertEqual(fan.SPEED_LOW, self.get_entity().attributes.get('speed'))
'Test oscillating the fan.'
def test_oscillate(self):
self.assertFalse(self.get_entity().attributes.get('oscillating')) fan.oscillate(self.hass, FAN_ENTITY_ID, True) self.hass.block_till_done() self.assertTrue(self.get_entity().attributes.get('oscillating')) fan.oscillate(self.hass, FAN_ENTITY_ID, False) self.hass.block_till_done() self.assertFalse(self.get_entity().attributes.get('oscillating'))
'Test is on service call.'
def test_is_on(self):
self.assertFalse(fan.is_on(self.hass, FAN_ENTITY_ID)) fan.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertTrue(fan.is_on(self.hass, FAN_ENTITY_ID))
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant()
'Stop everything that was started.'
def tearDown(self):
self.hass.stop()
'Test if Dyson connection failed.'
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=False) def test_dyson_login_failed(self, mocked_login):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR'}}) self.assertEqual(mocked_login.call_count, 1)
'Test valid connection to dyson web service.'
@mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[]) @mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True) def test_dyson_login(self, mocked_login, mocked_devices):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR'}}) self.assertEqual(mocked_login.call_count, 1) self.assertEqual(mocked_devices.call_count, 1) self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
'Test device connection using custom configuration.'
@mock.patch('homeassistant.helpers.discovery.load_platform') @mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[_get_dyson_account_device_available()]) @mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True) def test_dyson_custom_conf(self, mocked_login, mocked_devices, mocked_discovery):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR', dyson.CONF_DEVICES: [{'device_id': 'XX-XXXXX-XX', 'device_ip': '192.168.0.1'}]}}) self.assertEqual(mocked_login.call_count, 1) self.assertEqual(mocked_devices.call_count, 1) self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 1) self.assertEqual(mocked_discovery.call_count, 3)
'Test device connection with an invalid device.'
@mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[_get_dyson_account_device_not_available()]) @mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True) def test_dyson_custom_conf_device_not_available(self, mocked_login, mocked_devices):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR', dyson.CONF_DEVICES: [{'device_id': 'XX-XXXXX-XX', 'device_ip': '192.168.0.1'}]}}) self.assertEqual(mocked_login.call_count, 1) self.assertEqual(mocked_devices.call_count, 1) self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
'Test device connection with device raising an exception.'
@mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[_get_dyson_account_device_error()]) @mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True) def test_dyson_custom_conf_device_error(self, mocked_login, mocked_devices):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR', dyson.CONF_DEVICES: [{'device_id': 'XX-XXXXX-XX', 'device_ip': '192.168.0.1'}]}}) self.assertEqual(mocked_login.call_count, 1) self.assertEqual(mocked_devices.call_count, 1) self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
'Test device connection with custom conf and unknown device.'
@mock.patch('homeassistant.helpers.discovery.load_platform') @mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[_get_dyson_account_device_available()]) @mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True) def test_dyson_custom_conf_with_unknown_device(self, mocked_login, mocked_devices, mocked_discovery):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR', dyson.CONF_DEVICES: [{'device_id': 'XX-XXXXX-XY', 'device_ip': '192.168.0.1'}]}}) self.assertEqual(mocked_login.call_count, 1) self.assertEqual(mocked_devices.call_count, 1) self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0) self.assertEqual(mocked_discovery.call_count, 0)
'Test device connection using discovery.'
@mock.patch('homeassistant.helpers.discovery.load_platform') @mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[_get_dyson_account_device_available()]) @mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True) def test_dyson_discovery(self, mocked_login, mocked_devices, mocked_discovery):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR', dyson.CONF_TIMEOUT: 5, dyson.CONF_RETRY: 2}}) self.assertEqual(mocked_login.call_count, 1) self.assertEqual(mocked_devices.call_count, 1) self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 1) self.assertEqual(mocked_discovery.call_count, 3)
'Test device connection with discovery and invalid device.'
@mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[_get_dyson_account_device_not_available()]) @mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True) def test_dyson_discovery_device_not_available(self, mocked_login, mocked_devices):
dyson.setup(self.hass, {dyson.DOMAIN: {dyson.CONF_USERNAME: 'email', dyson.CONF_PASSWORD: 'password', dyson.CONF_LANGUAGE: 'FR', dyson.CONF_TIMEOUT: 5, dyson.CONF_RETRY: 2}}) self.assertEqual(mocked_login.call_count, 1) self.assertEqual(mocked_devices.call_count, 1) self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
'Setup things to be run when tests are started.'
def setUp(self):
self.hass = get_test_home_assistant() self.mock_publish = mock_mqtt_component(self.hass)
'Stop down everything that was started.'
def tearDown(self):
self.hass.stop()
'Test the controlling state via topic.'
def test_state_via_state_topic(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP'}})) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_CLOSED, state.state) fire_mqtt_message(self.hass, 'state-topic', '50') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '100') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', STATE_CLOSED) self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_CLOSED, state.state) fire_mqtt_message(self.hass, 'state-topic', STATE_OPEN) self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state)
'Test the controlling state via topic.'
def test_state_via_template(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'value_template': '{{ (value | multiply(0.01)) | int }}'}})) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) fire_mqtt_message(self.hass, 'state-topic', '10000') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '99') self.hass.block_till_done() state = self.hass.states.get('cover.test') self.assertEqual(STATE_CLOSED, state.state)
'Test changing state optimistically.'
def test_optimistic_state_change(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'qos': 0}})) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) cover.open_cover(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('command-topic', 'OPEN', 0, False), self.mock_publish.mock_calls[(-2)][1]) state = self.hass.states.get('cover.test') self.assertEqual(STATE_OPEN, state.state) cover.close_cover(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('command-topic', 'CLOSE', 0, False), self.mock_publish.mock_calls[(-2)][1]) state = self.hass.states.get('cover.test') self.assertEqual(STATE_CLOSED, state.state)
'Test the sending of open_cover.'
def test_send_open_cover_command(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 2}})) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) cover.open_cover(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('command-topic', 'OPEN', 2, False), self.mock_publish.mock_calls[(-2)][1]) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state)
'Test the sending of close_cover.'
def test_send_close_cover_command(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 2}})) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) cover.close_cover(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('command-topic', 'CLOSE', 2, False), self.mock_publish.mock_calls[(-2)][1]) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state)
'Test the sending of stop_cover.'
def test_send_stop__cover_command(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 2}})) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state) cover.stop_cover(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('command-topic', 'STOP', 2, False), self.mock_publish.mock_calls[(-2)][1]) state = self.hass.states.get('cover.test') self.assertEqual(STATE_UNKNOWN, state.state)
'Test the current cover position.'
def test_current_cover_position(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP'}})) state_attributes_dict = self.hass.states.get('cover.test').attributes self.assertFalse(('current_position' in state_attributes_dict)) self.assertFalse(('current_tilt_position' in state_attributes_dict)) self.assertFalse(((4 & self.hass.states.get('cover.test').attributes['supported_features']) == 4)) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.block_till_done() current_cover_position = self.hass.states.get('cover.test').attributes['current_position'] self.assertEqual(0, current_cover_position) fire_mqtt_message(self.hass, 'state-topic', '50') self.hass.block_till_done() current_cover_position = self.hass.states.get('cover.test').attributes['current_position'] self.assertEqual(50, current_cover_position) fire_mqtt_message(self.hass, 'state-topic', '101') self.hass.block_till_done() current_cover_position = self.hass.states.get('cover.test').attributes['current_position'] self.assertEqual(50, current_cover_position) fire_mqtt_message(self.hass, 'state-topic', 'non-numeric') self.hass.block_till_done() current_cover_position = self.hass.states.get('cover.test').attributes['current_position'] self.assertEqual(50, current_cover_position)
'Test setting cover position.'
def test_set_cover_position(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP'}})) state_attributes_dict = self.hass.states.get('cover.test').attributes self.assertFalse(('current_position' in state_attributes_dict)) self.assertFalse(('current_tilt_position' in state_attributes_dict)) self.assertTrue(((4 & self.hass.states.get('cover.test').attributes['supported_features']) == 4)) fire_mqtt_message(self.hass, 'state-topic', '22') self.hass.block_till_done() state_attributes_dict = self.hass.states.get('cover.test').attributes self.assertTrue(('current_position' in state_attributes_dict)) self.assertFalse(('current_tilt_position' in state_attributes_dict)) current_cover_position = self.hass.states.get('cover.test').attributes['current_position'] self.assertEqual(22, current_cover_position)
'Test setting cover position via template.'
def test_set_position_templated(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'set_position_template': '{{100-62}}', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP'}})) cover.set_cover_position(self.hass, 100, 'cover.test') self.hass.block_till_done() self.assertEqual(('position-topic', '38', 0, False), self.mock_publish.mock_calls[(-2)][1])
'Test setting cover position via template.'
def test_set_position_untemplated(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'set_position_topic': 'position-topic', 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP'}})) cover.set_cover_position(self.hass, 62, 'cover.test') self.hass.block_till_done() self.assertEqual(('position-topic', 62, 0, False), self.mock_publish.mock_calls[(-2)][1])
'Test with no command topic.'
def test_no_command_topic(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command', 'tilt_status_topic': 'tilt-status'}})) self.assertEqual(240, self.hass.states.get('cover.test').attributes['supported_features'])
'Test with command topic and tilt config.'
def test_with_command_topic_and_tilt(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'command_topic': 'test', 'platform': 'mqtt', 'name': 'test', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command', 'tilt_status_topic': 'tilt-status'}})) self.assertEqual(251, self.hass.states.get('cover.test').attributes['supported_features'])
'Test the defaults.'
def test_tilt_defaults(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command', 'tilt_status_topic': 'tilt-status'}})) state_attributes_dict = self.hass.states.get('cover.test').attributes self.assertTrue(('current_tilt_position' in state_attributes_dict)) current_cover_position = self.hass.states.get('cover.test').attributes['current_tilt_position'] self.assertEqual(STATE_UNKNOWN, current_cover_position)
'Test tilt defaults on close/open.'
def test_tilt_via_invocation_defaults(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command-topic', 'tilt_status_topic': 'tilt-status-topic'}})) cover.open_cover_tilt(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('tilt-command-topic', 100, 0, False), self.mock_publish.mock_calls[(-2)][1]) cover.close_cover_tilt(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('tilt-command-topic', 0, 0, False), self.mock_publish.mock_calls[(-2)][1])
'Test tilting to a given value.'
def test_tilt_given_value(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command-topic', 'tilt_status_topic': 'tilt-status-topic', 'tilt_opened_value': 400, 'tilt_closed_value': 125}})) cover.open_cover_tilt(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('tilt-command-topic', 400, 0, False), self.mock_publish.mock_calls[(-2)][1]) cover.close_cover_tilt(self.hass, 'cover.test') self.hass.block_till_done() self.assertEqual(('tilt-command-topic', 125, 0, False), self.mock_publish.mock_calls[(-2)][1])
'Test tilt by updating status via MQTT.'
def test_tilt_via_topic(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command-topic', 'tilt_status_topic': 'tilt-status-topic', 'tilt_opened_value': 400, 'tilt_closed_value': 125}})) fire_mqtt_message(self.hass, 'tilt-status-topic', '0') self.hass.block_till_done() current_cover_tilt_position = self.hass.states.get('cover.test').attributes['current_tilt_position'] self.assertEqual(0, current_cover_tilt_position) fire_mqtt_message(self.hass, 'tilt-status-topic', '50') self.hass.block_till_done() current_cover_tilt_position = self.hass.states.get('cover.test').attributes['current_tilt_position'] self.assertEqual(50, current_cover_tilt_position)
'Test tilt status via MQTT with altered tilt range.'
def test_tilt_via_topic_altered_range(self):
self.assertTrue(setup_component(self.hass, cover.DOMAIN, {cover.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_open': 'OPEN', 'payload_close': 'CLOSE', 'payload_stop': 'STOP', 'tilt_command_topic': 'tilt-command-topic', 'tilt_status_topic': 'tilt-status-topic', 'tilt_opened_value': 400, 'tilt_closed_value': 125, 'tilt_min': 0, 'tilt_max': 50}})) fire_mqtt_message(self.hass, 'tilt-status-topic', '0') self.hass.block_till_done() current_cover_tilt_position = self.hass.states.get('cover.test').attributes['current_tilt_position'] self.assertEqual(0, current_cover_tilt_position) fire_mqtt_message(self.hass, 'tilt-status-topic', '50') self.hass.block_till_done() current_cover_tilt_position = self.hass.states.get('cover.test').attributes['current_tilt_position'] self.assertEqual(100, current_cover_tilt_position) fire_mqtt_message(self.hass, 'tilt-status-topic', '25') self.hass.block_till_done() current_cover_tilt_position = self.hass.states.get('cover.test').attributes['current_tilt_position'] self.assertEqual(50, current_cover_tilt_position)