def test_shared_topic(self): mqtt = MockMqtt(self) state = MqttSharedTopic(mqtt, "/my/topic") sensor = Swi(mqtt=mqtt, state_topic=state, availability_topic=True) with sensor: sensor.available_set(True) state.publish() mqtt.assert_messages('homeassistant/switch/test_name/config', [{'availability_topic': 'homeassistant/switch/test_name/available', 'command_topic': 'homeassistant/switch/test_name/cmd', 'name': 'Test Name', 'payload_available': 'online', 'payload_not_available': 'offline', 'payload_off': 'off', 'payload_on': 'on', 'state_off': 'off', 'state_on': 'on', 'state_topic': '/my/topic', 'value_template': '{{ value_json.test_name }}', 'unique_id': 'test_name'}, None]) mqtt.assert_messages('/my/topic', [{"test_name": "off"}]) mqtt.assert_messages('homeassistant/switch/test_name/available', ["online"])
def test_print(self): mqtt = MockMqtt(self) state = MqttSharedTopic(mqtt, "/my/topic") registry = ComponentRegistry() therm = lambda: 1 registry.add_component(Cli(name='Test 1', mqtt=mqtt, thermometer=therm)) registry.add_component( Cli(name='Test 2', mqtt=mqtt, thermometer=therm, availability_topic=True)) registry.add_component( Cli(name='Test 3', mqtt=mqtt, thermometer=therm, state_topic=state)) registry.add_component( Cli(name='Test 4', mqtt=mqtt, thermometer=therm, state_topic=state, availability_topic=True)) file = open('config_climates.yaml', mode='r') config = file.read() file.close() self.assertEqual(config, registry.create_config())
def test_print(self): mqtt = MockMqtt(self) state = MqttSharedTopic(mqtt, "/my/topic") registry = ComponentRegistry() registry.add_component(Sen(name='Test 1', mqtt=mqtt)) registry.add_component( Sen(name='Test 2', mqtt=mqtt, availability_topic=True)) registry.add_component(Sen(name='Test 3', mqtt=mqtt, state_topic=state)) registry.add_component( Sen(name='Test 4', mqtt=mqtt, state_topic=state, availability_topic=True)) registry.add_component(SetSen(name='Test 5', mqtt=mqtt)) registry.add_component( SetSen(name='Test 6', mqtt=mqtt, availability_topic=True)) registry.add_component( SetSen(name='Test 7', mqtt=mqtt, state_topic=state)) registry.add_component( SetSen(name='Test 8', mqtt=mqtt, state_topic=state, availability_topic=True)) registry.add_component(ErrorSensor('errors', mqtt=mqtt)) file = open('config_sensors.yaml', mode='r') config = file.read() file.close() self.assertEqual(config, registry.create_config())
def test_shared_topic(self): mqtt = MockMqtt(self) state = MqttSharedTopic(mqtt, "/my/topic") sensor = SetSen(mqtt=mqtt, state_topic=state, availability_topic=True) with sensor: sensor.available_set(True) state.publish() mqtt.publish('homeassistant/sensor/test_name/cmd', '3.68') state.publish() mqtt.assert_messages( 'homeassistant/sensor/test_name/config', [{ 'availability_topic': 'homeassistant/sensor/test_name/available', 'name': 'Test Name', 'payload_available': 'online', 'payload_not_available': 'offline', 'state_topic': '/my/topic', 'unit_of_measurement': '°C', 'value_template': '{{ value_json.test_name }}', 'unique_id': 'test_name' }, None]) mqtt.assert_messages('/my/topic', [{ "test_name": "7.50" }, { "test_name": "3.68" }]) mqtt.assert_messages('homeassistant/sensor/test_name/available', ["online"])
def create_components(mqtt): availability_topic = False auto_discovery = False registry = ComponentRegistry() state = MqttSharedTopic(mqtt, "/my/topic") registry.add_shared_topic(state) standard_config = { 'mqtt': mqtt, 'state_topic': state, 'availability_topic': availability_topic, 'auto_discovery': auto_discovery } errors = ErrorSensor('Errors', **standard_config) registry.add_component(errors) last_update = create_last_update_sensor('Last Update', **standard_config) registry.add_component(last_update) func_limiter = create_func_call_limiter() sen1 = Sensor('Temp 1', '°C', state_func=create_temp_func(func_limiter, errors), **standard_config) sen2 = Sensor('Temp 2', '°C', state_func=create_temp_func(func_limiter, errors), **standard_config) registry.add_component(sen1) registry.add_component(sen2) avg = create_average_sensor('Temp Average', '°C', [sen1, sen2], icon='mdi:thermometer-lines', **standard_config) registry.add_component(avg) wavg, weights = create_weighted_average_sensor('Temp Weight Average', '°C', 0, 200, 1, 100, [sen1, sen2], icon='mdi:thermometer-lines', **standard_config) registry.add_component(wavg) for w in weights: registry.add_component(w, send_updates=False) climate = Climate('Boiler', wavg, state_change_func=climate_state_change, mqtt=mqtt, availability_topic=availability_topic, auto_discovery=auto_discovery) registry.add_component(climate) if not auto_discovery: logging.info("HA config:\n{}".format(registry.create_config())) return registry, func_limiter
def test_shared_topic(self): mqtt = MockMqtt(self) state = MqttSharedTopic(mqtt, "/my/topic") climate = Cli(mqtt=mqtt, thermometer=fake_temp2, state_topic=state, availability_topic=True) with climate: climate.available_set(True) self.assertEqual(climate.is_on(), False) state.publish() mqtt.publish('homeassistant/climate/test_name/cmdMode', 'heat') self.assertEqual(climate.is_on(), True) mqtt.publish('homeassistant/climate/test_name/cmdTargetTemp', '80') state.publish() mqtt.assert_messages( 'homeassistant/climate/test_name/config', [{ 'availability_topic': 'homeassistant/climate/test_name/available', 'current_temperature_template': '{{ value_json.test_name_curr_temp }}', 'current_temperature_topic': '/my/topic', 'max_temp': '100', 'min_temp': '0', 'mode_command_topic': 'homeassistant/climate/test_name/cmdMode', 'mode_state_template': '{{ value_json.test_name_mode }}', 'mode_state_topic': '/my/topic', 'modes': ['off', 'heat'], 'name': 'Test Name', 'payload_available': 'online', 'payload_not_available': 'offline', 'temp_step': '1', 'temperature_command_topic': 'homeassistant/climate/test_name/cmdTargetTemp', 'temperature_state_template': '{{ value_json.test_name_target_temp }}', 'temperature_state_topic': '/my/topic', 'unique_id': 'test_name' }, None]) mqtt.assert_messages('homeassistant/climate/test_name/available', ['online']) mqtt.assert_messages('homeassistant/climate/test_name/cmdMode', ["heat"]) mqtt.assert_messages('homeassistant/climate/test_name/cmdTargetTemp', ["80"]) mqtt.assert_messages('/my/topic', [{ 'test_name_curr_temp': '7.50', 'test_name_mode': 'off', 'test_name_target_temp': '6.50' }, { 'test_name_curr_temp': '10.50', 'test_name_mode': 'heat', 'test_name_target_temp': '80.00' }])
def test_manager(self): mqtt = MockMqtt(self) registry = ComponentRegistry() state = MqttSharedTopic(mqtt, "/my/topic") sensor1 = ErrorSensor('Error 1', mqtt=mqtt) sensor2 = ErrorSensor('Error 2', mqtt=mqtt, availability_topic=True) sensor3 = ErrorSensor('Error 3', mqtt=mqtt, state_topic=state) sensor4 = ErrorSensor('Error 4', mqtt=mqtt, state_topic=state, availability_topic=True) registry.add_shared_topic(state) registry.add_component(sensor1) registry.add_component(sensor2) registry.add_component(sensor3) registry.add_component(sensor4) with registry: registry.send_updates() mqtt.assert_messages( 'homeassistant/sensor/error_1/config', [{ 'icon': 'mdi:alarm-light', 'name': 'Error 1', 'state_topic': 'homeassistant/sensor/error_1/state', 'unit_of_measurement': 'errors', 'unique_id': 'error_1' }, None]) mqtt.assert_messages( 'homeassistant/sensor/error_2/config', [{ 'availability_topic': 'homeassistant/sensor/error_2/available', 'icon': 'mdi:alarm-light', 'name': 'Error 2', 'payload_available': 'online', 'payload_not_available': 'offline', 'state_topic': 'homeassistant/sensor/error_2/state', 'unit_of_measurement': 'errors', 'unique_id': 'error_2' }, None]) mqtt.assert_messages('homeassistant/sensor/error_3/config', [{ 'icon': 'mdi:alarm-light', 'name': 'Error 3', 'state_topic': '/my/topic', 'unit_of_measurement': 'errors', 'value_template': '{{ value_json.error_3 }}', 'unique_id': 'error_3' }, None]) mqtt.assert_messages( 'homeassistant/sensor/error_4/config', [{ 'availability_topic': 'homeassistant/sensor/error_4/available', 'icon': 'mdi:alarm-light', 'name': 'Error 4', 'payload_available': 'online', 'payload_not_available': 'offline', 'state_topic': '/my/topic', 'unit_of_measurement': 'errors', 'value_template': '{{ value_json.error_4 }}', 'unique_id': 'error_4' }, None]) mqtt.assert_messages('homeassistant/sensor/error_1/available', None) mqtt.assert_messages('homeassistant/sensor/error_2/available', ['online']) mqtt.assert_messages('homeassistant/sensor/error_3/available', None) mqtt.assert_messages('homeassistant/sensor/error_4/available', ['online']) mqtt.assert_messages('homeassistant/sensor/error_1/state', ['0']) mqtt.assert_messages('homeassistant/sensor/error_2/state', ['0']) mqtt.assert_messages('/my/topic', [{'error_3': '0', 'error_4': '0'}])
def test_average(self): mqtt = MockMqtt(self) registry = ComponentRegistry() state = MqttSharedTopic(mqtt, "/my/topic") sensor1 = Sen(name='S 1', mqtt=mqtt, state_topic=state, state_func=lambda: 1) sensor2 = Sen(name='S 2', mqtt=mqtt, state_topic=state, state_func=lambda: 4) sensor3 = Sen(name='S 3', mqtt=mqtt, state_topic=state, state_func=lambda: 7) sensors = [sensor1, sensor2, sensor3] s1 = create_average_sensor('Test 1', '°C', sensors, mqtt=mqtt, state_topic=state) s2, weights = create_weighted_average_sensor('Test 2', '°C', 0, 100, 1, 50, sensors, mqtt=mqtt, state_topic=state) registry.add_component(sensor1) registry.add_component(sensor2) registry.add_component(sensor3) registry.add_component(s1) registry.add_component(s2) registry.add_shared_topic(state) for w in weights: registry.add_component(w) with registry: registry.send_updates() mqtt.publish('homeassistant/sensor/s_1_weight/cmd', '20') mqtt.publish('homeassistant/sensor/s_2_weight/cmd', '40') mqtt.publish('homeassistant/sensor/s_3_weight/cmd', '80') registry.send_updates() mqtt.assert_messages('homeassistant/sensor/s_1/config', [{ 'name': 'S 1', 'state_topic': '/my/topic', 'unit_of_measurement': '°C', 'value_template': '{{ value_json.s_1 }}', 'unique_id': 's_1' }, None]) mqtt.assert_messages('homeassistant/sensor/s_2/config', [{ 'name': 'S 2', 'state_topic': '/my/topic', 'unit_of_measurement': '°C', 'value_template': '{{ value_json.s_2 }}', 'unique_id': 's_2' }, None]) mqtt.assert_messages('homeassistant/sensor/s_3/config', [{ 'name': 'S 3', 'state_topic': '/my/topic', 'unit_of_measurement': '°C', 'value_template': '{{ value_json.s_3 }}', 'unique_id': 's_3' }, None]) mqtt.assert_messages('homeassistant/sensor/test_1/config', [{ 'name': 'Test 1', 'state_topic': '/my/topic', 'unit_of_measurement': '°C', 'value_template': '{{ value_json.test_1 }}', 'unique_id': 'test_1' }, None]) mqtt.assert_messages('homeassistant/sensor/test_2/config', [{ 'name': 'Test 2', 'state_topic': '/my/topic', 'unit_of_measurement': '°C', 'value_template': '{{ value_json.test_2 }}', 'unique_id': 'test_2' }, None]) mqtt.assert_messages( 'homeassistant/sensor/s_1_weight/config', [{ 'icon': 'mdi:weight', 'name': 'S 1 weight', 'state_topic': 'homeassistant/sensor/s_1_weight/state', 'unit_of_measurement': '', 'unique_id': 's_1_weight' }, None]) mqtt.assert_messages( 'homeassistant/sensor/s_2_weight/config', [{ 'icon': 'mdi:weight', 'name': 'S 2 weight', 'state_topic': 'homeassistant/sensor/s_2_weight/state', 'unit_of_measurement': '', 'unique_id': 's_2_weight' }, None]) mqtt.assert_messages( 'homeassistant/sensor/s_3_weight/config', [{ 'icon': 'mdi:weight', 'name': 'S 3 weight', 'state_topic': 'homeassistant/sensor/s_3_weight/state', 'unit_of_measurement': '', 'unique_id': 's_3_weight' }, None]) mqtt.assert_messages('/my/topic', [{ 's_1': '1.00', 's_2': '4.00', 's_3': '7.00', 'test_1': '4.00', 'test_2': '4.00' }, { 's_1': '1.00', 's_2': '4.00', 's_3': '7.00', 'test_1': '4.00', 'test_2': '5.29' }]) mqtt.assert_messages('homeassistant/sensor/s_1_weight/state', ['50.00', '20.00', '20.00']) mqtt.assert_messages('homeassistant/sensor/s_2_weight/state', ['50.00', '40.00', '40.00']) mqtt.assert_messages('homeassistant/sensor/s_3_weight/state', ['50.00', '80.00', '80.00'])