-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Move core service from core to components #5787
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
Merged
+165
−102
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6b452d2
Move core servcie from core to components
pvizeli e1f57e7
add new handler for signals/exception
pvizeli 6291142
Static persistent id
pvizeli 385c2e3
Move unittest
pvizeli a0d17ac
fix coro/callback
pvizeli 6a31ee8
Add more unittest for new services
pvizeli 11f3afc
Address comments
pvizeli ffb555f
Update __init__.py
balloob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,8 +26,7 @@ | |
| ATTR_SERVICE_CALL_ID, ATTR_SERVICE_DATA, EVENT_CALL_SERVICE, | ||
| EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, | ||
| EVENT_SERVICE_EXECUTED, EVENT_SERVICE_REGISTERED, EVENT_STATE_CHANGED, | ||
| EVENT_TIME_CHANGED, MATCH_ALL, RESTART_EXIT_CODE, | ||
| SERVICE_HOMEASSISTANT_RESTART, SERVICE_HOMEASSISTANT_STOP, __version__) | ||
| EVENT_TIME_CHANGED, MATCH_ALL, RESTART_EXIT_CODE, __version__) | ||
| from homeassistant.exceptions import ( | ||
| HomeAssistantError, InvalidEntityFormatError, ShuttingDown) | ||
| from homeassistant.util.async import ( | ||
|
|
@@ -137,7 +136,7 @@ def start(self) -> None: | |
| _LOGGER.info("Starting Home Assistant core loop") | ||
| self.loop.run_forever() | ||
| except KeyboardInterrupt: | ||
| self.loop.call_soon(self._async_stop_handler) | ||
| self.loop.create_task(self.async_stop()) | ||
| self.loop.run_forever() | ||
| finally: | ||
| self.loop.close() | ||
|
|
@@ -149,26 +148,23 @@ def async_start(self): | |
| This method is a coroutine. | ||
| """ | ||
| _LOGGER.info("Starting Home Assistant") | ||
|
|
||
| self.state = CoreState.starting | ||
|
|
||
| # Register the restart/stop event | ||
| self.services.async_register( | ||
| DOMAIN, SERVICE_HOMEASSISTANT_STOP, self._async_stop_handler) | ||
| self.services.async_register( | ||
| DOMAIN, SERVICE_HOMEASSISTANT_RESTART, self._async_restart_handler) | ||
|
|
||
| # Setup signal handling | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we should also extract the signal handling of the core.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, make a new PR for that |
||
| if sys.platform != 'win32': | ||
| def _async_signal_handle(exit_code): | ||
| """Wrap signal handling.""" | ||
| self.async_add_job(self.async_stop(exit_code)) | ||
|
|
||
| try: | ||
| self.loop.add_signal_handler( | ||
| signal.SIGTERM, self._async_stop_handler) | ||
| signal.SIGTERM, _async_signal_handle, 0) | ||
| except ValueError: | ||
| _LOGGER.warning("Could not bind to SIGTERM") | ||
|
|
||
| try: | ||
| self.loop.add_signal_handler( | ||
| signal.SIGHUP, self._async_restart_handler) | ||
| signal.SIGHUP, _async_signal_handle, RESTART_EXIT_CODE) | ||
| except ValueError: | ||
| _LOGGER.warning("Could not bind to SIGHUP") | ||
|
|
||
|
|
@@ -283,7 +279,7 @@ def stop(self) -> None: | |
| run_coroutine_threadsafe(self.async_stop(), self.loop) | ||
|
|
||
| @asyncio.coroutine | ||
| def async_stop(self) -> None: | ||
| def async_stop(self, exit_code=0) -> None: | ||
| """Stop Home Assistant and shuts down all threads. | ||
|
|
||
| This method is a coroutine. | ||
|
|
@@ -306,6 +302,7 @@ def async_stop(self) -> None: | |
| logging.getLogger('').removeHandler(handler) | ||
| yield from handler.async_close(blocking=True) | ||
|
|
||
| self.exit_code = exit_code | ||
| self.loop.stop() | ||
|
|
||
| # pylint: disable=no-self-use | ||
|
|
@@ -324,47 +321,6 @@ def _async_exception_handler(self, loop, context): | |
|
|
||
| _LOGGER.error("Error doing job: %s", context['message'], **kwargs) | ||
|
|
||
| @callback | ||
| def _async_stop_handler(self, *args): | ||
| """Stop Home Assistant.""" | ||
| self.exit_code = 0 | ||
| self.loop.create_task(self.async_stop()) | ||
|
|
||
| @asyncio.coroutine | ||
| def _async_check_config_and_restart(self): | ||
| """Restart Home Assistant if config is valid. | ||
|
|
||
| This method is a coroutine. | ||
| """ | ||
| proc = yield from asyncio.create_subprocess_exec( | ||
| sys.argv[0], | ||
| '--script', | ||
| 'check_config', | ||
| stdout=asyncio.subprocess.PIPE) | ||
| # Wait for the subprocess exit | ||
| (stdout_data, dummy) = yield from proc.communicate() | ||
| result = yield from proc.wait() | ||
| if result: | ||
| _LOGGER.error("check_config failed. Not restarting.") | ||
| content = re.sub(r'\033\[[^m]*m', '', str(stdout_data, 'utf-8')) | ||
| # Put error cleaned from color codes in the error log so it | ||
| # will be visible at the UI. | ||
| _LOGGER.error(content) | ||
| yield from self.services.async_call( | ||
| 'persistent_notification', 'create', { | ||
| 'message': 'Config error. See dev-info panel for details.', | ||
| 'title': 'Restarting', | ||
| 'notification_id': '{}.restart'.format(DOMAIN)}) | ||
| return | ||
|
|
||
| self.exit_code = RESTART_EXIT_CODE | ||
| yield from self.async_stop() | ||
|
|
||
| @callback | ||
| def _async_restart_handler(self, *args): | ||
| """Restart Home Assistant.""" | ||
| self.loop.create_task(self._async_check_config_and_restart()) | ||
|
|
||
|
|
||
| class EventOrigin(enum.Enum): | ||
| """Represent the origin of an event.""" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think such a dependency is a hack.
homeassistant.config shouldn't depend on homeassistant.components.*
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This depense will exists without I Import this or not... Bootstrap work in same way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could call persistence notification service then there will be no dependency - i.e. hass would still come up if homeassistant.components.persistent_notification was removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LIke in homeassistant.core you call a service and not call homeassistant.components.stop()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it will become the same if I import this or I need to do:
from ...persistent_notification Import DOMAIN, SERVICE_XYfor calling the service later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, so I propose not to import anything and just use strings to call the service.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a hack but one that we use in other places in this file too, see line 31. However, line 31 does execute it a bit more classy, as it allows for people to overwrite that component.
I prefer to use the function over spelling out the service, because that's the suggested way for Python code.