New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix starting of remote slaves. #3681

Closed
wants to merge 2 commits into
base: dev
from

Conversation

Projects
None yet
5 participants
@michaelkuty
Contributor

michaelkuty commented Oct 3, 2016

Description:

Related to my issue #3669

I cannot start my slave with state sharing

Related issue (if applicable): fixes #

Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#

Example entry for configuration.yaml (if applicable):

Checklist:

If user exposed functionality or configuration variables are added/changed:

If code communicates with devices, web services, or a:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

#! /Users/michaelkuty/projects/hass/bin/python3
import logging


import homeassistant.bootstrap as bootstrap
import homeassistant.config as config
import homeassistant.remote as remote
from homeassistant.const import EVENT_HOMEASSISTANT_START

# setup logging
logging.basicConfig(level=logging.DEBUG)

# Location of the Master API: host, password, port.
# Password and port are optional.
remote_api = remote.API("localhost", port=8124)

# Initialize slave
hass = remote.HomeAssistant(remote_api)
hass.config.config_dir = "/Users/michaelkuty/projects/hass/.config"
myconfig = config.load_yaml_config_file(
    "/Users/michaelkuty/projects/hass/conf.yaml")

# To add an interface to the slave on localhost:8123
#bootstrap.setup_component(hass, 'frontend', myconfig)

def setup_platforms(event):
    """Setup platforms."""
    bootstrap.setup_component(hass, 'api', myconfig)
    bootstrap.setup_component(hass, 'http', myconfig)
    bootstrap.setup_component(hass, 'switch', myconfig)
    bootstrap.setup_component(hass, 'sensor', myconfig)

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, setup_platforms)

hass.start()

@michaelkuty

This comment has been minimized.

Show comment
Hide comment
@michaelkuty

michaelkuty Oct 4, 2016

Contributor

Ok this was a naive try, but i cannot understand that i'm only one who using remote instances..
I will continue experiment with remote slaves, now we have running slave, but event forwarding not works from slave to master but from master to slave yes.

Contributor

michaelkuty commented Oct 4, 2016

Ok this was a naive try, but i cannot understand that i'm only one who using remote instances..
I will continue experiment with remote slaves, now we have running slave, but event forwarding not works from slave to master but from master to slave yes.

@@ -131,13 +131,13 @@ def start_wsgi_server(event):
"""Start the WSGI server."""
server.start()
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_wsgi_server)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_wsgi_server)

This comment has been minimized.

@balloob

balloob Oct 5, 2016

Member

setup is not called from within the event loop and thus should not call async_* methods.

@balloob

balloob Oct 5, 2016

Member

setup is not called from within the event loop and thus should not call async_* methods.

This comment has been minimized.

@michaelkuty

michaelkuty Oct 5, 2016

Contributor

If I use sync version, get freeze in this method

(hass) Michaels-MacBook:hass michaelkuty$ ./hass-slave.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/ HTTP/1.1" 200 27
DEBUG:asyncio:Using selector: KqueueSelector
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/states HTTP/1.1" 200 2
INFO:homeassistant.loader:Loaded api from homeassistant.components.api
INFO:homeassistant.loader:Loaded http from homeassistant.components.http
@michaelkuty

michaelkuty Oct 5, 2016

Contributor

If I use sync version, get freeze in this method

(hass) Michaels-MacBook:hass michaelkuty$ ./hass-slave.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/ HTTP/1.1" 200 27
DEBUG:asyncio:Using selector: KqueueSelector
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/states HTTP/1.1" 200 2
INFO:homeassistant.loader:Loaded api from homeassistant.components.api
INFO:homeassistant.loader:Loaded http from homeassistant.components.http

This comment has been minimized.

@vetegrodd

vetegrodd Oct 6, 2016

Does the slave work when you add the async listener? When I setup a remote connection I get a lock in remote.py:StateMachine:__init__ the line bus.listen(ha.EVENT_STATE_CHANGED, self._state_changed_listener).

Changing it to an async_listen makes the slave start, but it does not connect the master, which I guess is due to the fact that the EVENT_STATE_CHANGED is not fired.

@vetegrodd

vetegrodd Oct 6, 2016

Does the slave work when you add the async listener? When I setup a remote connection I get a lock in remote.py:StateMachine:__init__ the line bus.listen(ha.EVENT_STATE_CHANGED, self._state_changed_listener).

Changing it to an async_listen makes the slave start, but it does not connect the master, which I guess is due to the fact that the EVENT_STATE_CHANGED is not fired.

This comment has been minimized.

@michaelkuty

michaelkuty Oct 17, 2016

Contributor

State was changed but is not set to remote API as you can see fix in last commit

@michaelkuty

michaelkuty Oct 17, 2016

Contributor

State was changed but is not set to remote API as you can see fix in last commit

def stop_wsgi_server(event):
"""Stop the WSGI server."""
server.stop()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_wsgi_server)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_wsgi_server)

This comment has been minimized.

@balloob

balloob Oct 5, 2016

Member

Same

@balloob

michaelkuty added some commits Oct 3, 2016

Fix starting of remote slaves.
#! /Users/michaelkuty/projects/hass/bin/python3
import logging

import homeassistant.bootstrap as bootstrap
import homeassistant.config as config
import homeassistant.remote as remote
from homeassistant.const import EVENT_HOMEASSISTANT_START

# setup logging
logging.basicConfig(level=logging.DEBUG)

# Location of the Master API: host, password, port.
# Password and port are optional.
remote_api = remote.API("localhost", port=8124)

# Initialize slave
hass = remote.HomeAssistant(remote_api)
hass.config.config_dir = "/Users/michaelkuty/projects/hass/.config"
myconfig = config.load_yaml_config_file(
    "/Users/michaelkuty/projects/hass/conf.yaml")

# To add an interface to the slave on localhost:8123
#bootstrap.setup_component(hass, 'frontend', myconfig)

def setup_platforms(event):
    """Stop the WSGI server."""
    bootstrap.setup_component(hass, 'api', myconfig)
    bootstrap.setup_component(hass, 'http', myconfig)
    bootstrap.setup_component(hass, 'switch', myconfig)
    bootstrap.setup_component(hass, 'sensor', myconfig)

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, setup_platforms)

hass.start()
@michaelkuty

This comment has been minimized.

Show comment
Hide comment
@michaelkuty

michaelkuty Oct 17, 2016

Contributor
(hass) Michaels-MacBook:hass michaelkuty$ python3.5 hass-slave.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/ HTTP/1.1" 200 27
DEBUG:asyncio:Using selector: KqueueSelector
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/states HTTP/1.1" 200 2
INFO:homeassistant.loader:Loaded api from homeassistant.components.api
INFO:homeassistant.loader:Loaded http from homeassistant.components.http
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:homeassistant.core:Starting Home Assistant (4 threads)
INFO:homeassistant.core:Starting Home Assistant core loop
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=homeassistant, service=stop>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=homeassistant, service=restart>
INFO:homeassistant.core:Bus:Handling <Event homeassistant_start[R]>
DEBUG:homeassistant.bootstrap:Component api already set up.
INFO:homeassistant.core:Timer:starting
DEBUG:homeassistant.bootstrap:Component http already set up.
ERROR:homeassistant.loader:Unable to find component fronetend
INFO:homeassistant.loader:Loaded switch from homeassistant.components.switch
INFO:homeassistant.loader:Loaded switch.command_line from homeassistant.components.switch.command_line
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=switch.common_power, new_state=<state switch.common_power=off; friendly_name=Common power @ 2016-10-17T14:52:27.464086+00:00>, old_state=None>
INFO:homeassistant.loader:Loaded group from homeassistant.components.group
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.all_switches, new_state=<state group.all_switches=off; entity_id=('switch.common_power',), order=0, hidden=True, auto=True, friendly_name=all switches @ 2016-10-17T14:52:27.469578+00:00>, old_state=None>
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/states/switch.common_power HTTP/1.1" 201 205
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/states/group.all_switches HTTP/1.1" 201 284
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=switch, service=turn_off>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=switch, service=turn_on>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=switch, service=toggle>
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:homeassistant.loader:Loaded sensor from homeassistant.components.sensor
INFO:homeassistant.loader:Loaded sensor.command_line from homeassistant.components.sensor.command_line
INFO:homeassistant.components.sensor.command_line:Running command: date
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=sensor.command_sensor_22222, new_state=<state sensor.command_sensor_22222=10; unit_of_measurement=h, friendly_name=Command sensor 22222 @ 2016-10-17T14:52:27.721958+00:00>, old_state=None>
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/states/sensor.command_sensor_22222 HTTP/1.1" 201 248
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:homeassistant.components.switch.command_line:Running state command: date
Contributor

michaelkuty commented Oct 17, 2016

(hass) Michaels-MacBook:hass michaelkuty$ python3.5 hass-slave.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/ HTTP/1.1" 200 27
DEBUG:asyncio:Using selector: KqueueSelector
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/states HTTP/1.1" 200 2
INFO:homeassistant.loader:Loaded api from homeassistant.components.api
INFO:homeassistant.loader:Loaded http from homeassistant.components.http
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:homeassistant.core:Starting Home Assistant (4 threads)
INFO:homeassistant.core:Starting Home Assistant core loop
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=homeassistant, service=stop>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=homeassistant, service=restart>
INFO:homeassistant.core:Bus:Handling <Event homeassistant_start[R]>
DEBUG:homeassistant.bootstrap:Component api already set up.
INFO:homeassistant.core:Timer:starting
DEBUG:homeassistant.bootstrap:Component http already set up.
ERROR:homeassistant.loader:Unable to find component fronetend
INFO:homeassistant.loader:Loaded switch from homeassistant.components.switch
INFO:homeassistant.loader:Loaded switch.command_line from homeassistant.components.switch.command_line
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=switch.common_power, new_state=<state switch.common_power=off; friendly_name=Common power @ 2016-10-17T14:52:27.464086+00:00>, old_state=None>
INFO:homeassistant.loader:Loaded group from homeassistant.components.group
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=group.all_switches, new_state=<state group.all_switches=off; entity_id=('switch.common_power',), order=0, hidden=True, auto=True, friendly_name=all switches @ 2016-10-17T14:52:27.469578+00:00>, old_state=None>
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/states/switch.common_power HTTP/1.1" 201 205
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/states/group.all_switches HTTP/1.1" 201 284
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=switch, service=turn_off>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=switch, service=turn_on>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=switch, service=toggle>
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:homeassistant.loader:Loaded sensor from homeassistant.components.sensor
INFO:homeassistant.loader:Loaded sensor.command_line from homeassistant.components.sensor.command_line
INFO:homeassistant.components.sensor.command_line:Running command: date
INFO:homeassistant.core:Bus:Handling <Event state_changed[L]: entity_id=sensor.command_sensor_22222, new_state=<state sensor.command_sensor_22222=10; unit_of_measurement=h, friendly_name=Command sensor 22222 @ 2016-10-17T14:52:27.721958+00:00>, old_state=None>
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/states/sensor.command_sensor_22222 HTTP/1.1" 201 248
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"POST /api/events/component_loaded HTTP/1.1" 200 44
INFO:homeassistant.components.switch.command_line:Running state command: date
@balloob

This comment has been minimized.

Show comment
Hide comment
@balloob

balloob Oct 18, 2016

Member

The remote pieces of Home Assistant have not gotten the same amount of attention as the rest of the codebase. Some are thinking about a revamp based on websockets once we have switched our HTTP stack over to aiohttp (#3914)

Member

balloob commented Oct 18, 2016

The remote pieces of Home Assistant have not gotten the same amount of attention as the rest of the codebase. Some are thinking about a revamp based on websockets once we have switched our HTTP stack over to aiohttp (#3914)

@michaelkuty

This comment has been minimized.

Show comment
Hide comment
@michaelkuty

michaelkuty Oct 18, 2016

Contributor

I agree this, but I think that for you is only some minutes, but for me is about hours of debugging. @balloob Please can you fix this just for basic functions now ?

My implementation works but tests failing..

Contributor

michaelkuty commented Oct 18, 2016

I agree this, but I think that for you is only some minutes, but for me is about hours of debugging. @balloob Please can you fix this just for basic functions now ?

My implementation works but tests failing..

@Spartan-II-117

This comment has been minimized.

Show comment
Hide comment
@Spartan-II-117

Spartan-II-117 Nov 7, 2016

How that aiohttp has been implemented, is it possible we could see some progress from this? What is now required to get slaves working again?

Spartan-II-117 commented Nov 7, 2016

How that aiohttp has been implemented, is it possible we could see some progress from this? What is now required to get slaves working again?

@balloob

This comment has been minimized.

Show comment
Hide comment
@balloob

balloob Nov 9, 2016

Member

This PR seems to have gone stale. Closing it. You can reopen it when you're ready to finish it.

Member

balloob commented Nov 9, 2016

This PR seems to have gone stale. Closing it. You can reopen it when you're ready to finish it.

@balloob balloob closed this Nov 9, 2016

@home-assistant home-assistant locked and limited conversation to collaborators Mar 17, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.