How to use homeassistant - 10 common examples

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 / common.py View on Github external
@ha.callback
def ensure_auth_manager_loaded(auth_mgr):
    """Ensure an auth manager is considered loaded."""
    store = auth_mgr._store
    if store._users is None:
        store._set_defaults()
github home-assistant / home-assistant / tests / components / sun / test_init.py View on Github external
    @ha.callback
    def state_change_listener(event):
        if event.data.get("entity_id") == "sun.sun":
            events.append(event)
github home-assistant / home-assistant / tests / test_core.py View on Github external
def test_create_timer(mock_monotonic, loop):
    """Test create timer."""
    hass = MagicMock()
    funcs = []
    orig_callback = ha.callback

    def mock_callback(func):
        funcs.append(func)
        return orig_callback(func)

    mock_monotonic.side_effect = 10.2, 10.8, 11.3

    with patch.object(ha, "callback", mock_callback), patch(
        "homeassistant.core.dt_util.utcnow",
        return_value=datetime(2018, 12, 31, 3, 4, 5, 333333),
    ):
        ha._async_create_timer(hass)

    assert len(funcs) == 2
    fire_time_event, stop_timer = funcs
github home-assistant / home-assistant / tests / components / directv / test_media_player.py View on Github external
"major": 202,
    "minor": 65535,
    "offset": 1,
    "programId": "102454523",
    "rating": "No Rating",
    "startTime": 1541876400,
    "stationId": 3900947,
    "title": "Using Home Assistant to automate your home",
    "uniqueId": "12345",
    "episodeTitle": "Configure DirecTV platform.",
}

WORKING_CONFIG = {
    "media_player": {
        "platform": "directv",
        CONF_HOST: IP_ADDRESS,
        CONF_NAME: "Main DVR",
        CONF_PORT: DEFAULT_PORT,
        CONF_DEVICE: DEFAULT_DEVICE,
    }
}


@pytest.fixture
def client_dtv():
    """Fixture for a client device."""
    mocked_dtv = MockDirectvClass("mock_ip")
    mocked_dtv.attributes = RECORDING
    mocked_dtv._standby = False
    return mocked_dtv
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