How to use the homeassistant.setup.setup_component function in homeassistant

To help you get started, we’ve selected a few homeassistant examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github home-assistant / home-assistant / tests / components / tts / test_init.py View on Github external
def test_setup_component_and_test_service_clear_cache(self):
        """Set up the demo platform and call service clear cache."""
        calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)

        config = {tts.DOMAIN: {"platform": "demo"}}

        with assert_setup_component(1, tts.DOMAIN):
            setup_component(self.hass, tts.DOMAIN, config)

        self.hass.services.call(
            tts.DOMAIN,
            "demo_say",
            {tts.ATTR_MESSAGE: "I person is on front of your door."},
        )
        self.hass.block_till_done()

        assert len(calls) == 1
        assert os.path.isfile(
            os.path.join(
                self.default_tts_cache,
                "265944c108cbb00b2a621be5930513e03a0bb2cd_en_-_demo.mp3",
            )
        )
github home-assistant / home-assistant / tests / components / sensor / test_history_stats.py View on Github external
'history': {
            },
            'sensor': {
                'platform': 'history_stats',
                'entity_id': 'binary_sensor.test_id',
                'name': 'Test',
                'state': 'on',
                'start': '{{ now() }}',
                'duration': 'TEST',
            }
        }

        setup_component(self.hass, 'sensor', config)
        assert self.hass.states.get('sensor.test')is None
        with pytest.raises(TypeError):
            setup_component(self.hass, 'sensor', config)()
github home-assistant / home-assistant / tests / components / history_stats / test_sensor.py View on Github external
"""Test config when too many arguments provided."""
        self.init_recorder()
        config = {
            "history": {},
            "sensor": {
                "platform": "history_stats",
                "entity_id": "binary_sensor.test_id",
                "name": "Test",
                "state": "on",
                "start": "{{ as_timestamp(now()) - 3600 }}",
                "end": "{{ now() }}",
                "duration": "01:00",
            },
        }

        setup_component(self.hass, "sensor", config)
        assert self.hass.states.get("sensor.test") is None
        with pytest.raises(TypeError):
            setup_component(self.hass, "sensor", config)()
github home-assistant / home-assistant / tests / components / dte_energy_bridge / test_sensor.py View on Github external
def test_setup_correct_reading(self, mock_req):
        """Test DTE Energy bridge returns a correct value."""
        mock_req.get(
            "http://{}/instantaneousdemand".format(DTE_ENERGY_BRIDGE_CONFIG["ip"]),
            text=".411 kW",
        )
        assert setup_component(
            self.hass, "sensor", {"sensor": DTE_ENERGY_BRIDGE_CONFIG}
        )
        assert "0.411" == self.hass.states.get("sensor.current_energy_usage").state
github home-assistant / home-assistant / tests / components / shell_command / test_init.py View on Github external
def test_template_render(self, mock_call):
        """Ensure shell_commands with templates get rendered properly."""
        self.hass.states.set("sensor.test_state", "Works")
        mock_call.return_value = mock_process_creator(error=False)
        assert setup_component(
            self.hass,
            shell_command.DOMAIN,
            {
                shell_command.DOMAIN: {
                    "test_service": ("ls /bin {{ states.sensor" ".test_state.state }}")
                }
            },
        )

        self.hass.services.call("shell_command", "test_service", blocking=True)

        self.hass.block_till_done()
        cmd = mock_call.mock_calls[0][1]

        assert 1 == mock_call.call_count
        assert ("ls", "/bin", "Works") == cmd
github home-assistant / home-assistant / tests / components / tod / test_binary_sensor.py View on Github external
"binary_sensor": [
                {
                    "platform": "tod",
                    "name": "Day",
                    "after": "sunrise",
                    "before": "sunset",
                }
            ]
        }
        entity_id = "binary_sensor.day"
        testtime = test_time
        with patch(
            "homeassistant.components.tod.binary_sensor.dt_util.utcnow",
            return_value=testtime,
        ):
            setup_component(self.hass, "binary_sensor", config)

            self.hass.block_till_done()
            state = self.hass.states.get(entity_id)
            assert state.state == STATE_OFF

        testtime = sunrise + timedelta(seconds=-1)
        with patch(
            "homeassistant.components.tod.binary_sensor.dt_util.utcnow",
            return_value=testtime,
        ):

            self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
            self.hass.block_till_done()

            state = self.hass.states.get(entity_id)
            assert state.state == STATE_OFF
github home-assistant / home-assistant / tests / components / manual_mqtt / test_alarm_control_panel.py View on Github external
def test_trigger_with_disarm_after_trigger(self):
        """Test disarm after trigger."""
        assert setup_component(
            self.hass,
            alarm_control_panel.DOMAIN,
            {
                "alarm_control_panel": {
                    "platform": "manual_mqtt",
                    "name": "test",
                    "trigger_time": 5,
                    "pending_time": 0,
                    "disarm_after_trigger": True,
                    "command_topic": "alarm/command",
                    "state_topic": "alarm/state",
                }
            },
        )

        entity_id = "alarm_control_panel.test"
github home-assistant / home-assistant / tests / components / datadog / test_init.py View on Github external
def test_logbook_entry(self, mock_connection, mock_client):
        """Test event listener."""
        self.hass.bus.listen = mock.MagicMock()

        assert setup_component(
            self.hass,
            datadog.DOMAIN,
            {datadog.DOMAIN: {"host": "host", "rate": datadog.DEFAULT_RATE}},
        )

        assert self.hass.bus.listen.called
        handler_method = self.hass.bus.listen.call_args_list[0][0][1]

        event = {
            "domain": "automation",
            "entity_id": "sensor.foo.bar",
            "message": "foo bar biz",
            "name": "triggered something",
        }
        handler_method(mock.MagicMock(data=event))
github home-assistant / home-assistant / tests / components / ffmpeg / test_sensor.py View on Github external
def test_setup_component_start(self, mock_start):
        """Set up ffmpeg component."""
        with assert_setup_component(1, "binary_sensor"):
            setup_component(self.hass, "binary_sensor", self.config)
        self.hass.block_till_done()

        assert self.hass.data["ffmpeg"].binary == "ffmpeg"
        assert self.hass.states.get("binary_sensor.ffmpeg_motion") is not None

        self.hass.start()
        assert mock_start.called

        entity = self.hass.states.get("binary_sensor.ffmpeg_motion")
        assert entity.state == "unavailable"
github home-assistant / home-assistant / tests / components / sensor / test_uptime.py View on Github external
def test_uptime_sensor_name_change(self):
        """Test uptime sensor with different name."""
        config = {
            'sensor': {
                'platform': 'uptime',
                'name': 'foobar',
            }
        }
        assert setup_component(self.hass, 'sensor', config)