2017-02-12 19:31:46 +00:00
|
|
|
"""Component to interact with Hassbian tools."""
|
|
|
|
|
2019-05-21 05:21:31 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-08 16:57:28 +00:00
|
|
|
from homeassistant.components import websocket_api
|
2017-02-12 19:31:46 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
|
|
|
from homeassistant.config import async_check_ha_config_file
|
2019-12-08 16:57:28 +00:00
|
|
|
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC
|
2019-05-23 00:24:46 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.util import location
|
2017-02-12 19:31:46 +00:00
|
|
|
|
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_setup(hass):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Hassbian config."""
|
2017-02-12 19:31:46 +00:00
|
|
|
hass.http.register_view(CheckConfigView)
|
2019-05-23 00:24:46 +00:00
|
|
|
websocket_api.async_register_command(hass, websocket_update_config)
|
|
|
|
websocket_api.async_register_command(hass, websocket_detect_config)
|
2017-02-12 19:31:46 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class CheckConfigView(HomeAssistantView):
|
|
|
|
"""Hassbian packages endpoint."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/config/core/check_config"
|
|
|
|
name = "api:config:core:check_config"
|
2017-02-12 19:31:46 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def post(self, request):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Validate configuration and return results."""
|
2019-07-31 19:25:30 +00:00
|
|
|
errors = await async_check_ha_config_file(request.app["hass"])
|
2017-02-12 19:31:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = "invalid" if errors else "valid"
|
2017-02-12 19:31:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json({"result": state, "errors": errors})
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.async_response
|
2019-07-31 19:25:30 +00:00
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
|
|
|
"type": "config/core/update",
|
|
|
|
vol.Optional("latitude"): cv.latitude,
|
|
|
|
vol.Optional("longitude"): cv.longitude,
|
|
|
|
vol.Optional("elevation"): int,
|
|
|
|
vol.Optional("unit_system"): cv.unit_system,
|
|
|
|
vol.Optional("location_name"): str,
|
|
|
|
vol.Optional("time_zone"): cv.time_zone,
|
2020-05-08 00:29:47 +00:00
|
|
|
vol.Optional("external_url"): vol.Any(cv.url, None),
|
|
|
|
vol.Optional("internal_url"): vol.Any(cv.url, None),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2019-05-23 00:24:46 +00:00
|
|
|
async def websocket_update_config(hass, connection, msg):
|
|
|
|
"""Handle update core config command."""
|
2019-05-21 05:21:31 +00:00
|
|
|
data = dict(msg)
|
2019-07-31 19:25:30 +00:00
|
|
|
data.pop("id")
|
|
|
|
data.pop("type")
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
try:
|
2019-06-01 06:03:45 +00:00
|
|
|
await hass.config.async_update(**data)
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_result(msg["id"])
|
2019-05-23 00:24:46 +00:00
|
|
|
except ValueError as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_error(msg["id"], "invalid_info", str(err))
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.async_response
|
2019-07-31 19:25:30 +00:00
|
|
|
@websocket_api.websocket_command({"type": "config/core/detect"})
|
2019-05-23 00:24:46 +00:00
|
|
|
async def websocket_detect_config(hass, connection, msg):
|
|
|
|
"""Detect core config."""
|
|
|
|
session = hass.helpers.aiohttp_client.async_get_clientsession()
|
|
|
|
location_info = await location.async_detect_location_info(session)
|
|
|
|
|
|
|
|
info = {}
|
|
|
|
|
|
|
|
if location_info is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_result(msg["id"], info)
|
2019-05-23 00:24:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if location_info.use_metric:
|
2019-07-31 19:25:30 +00:00
|
|
|
info["unit_system"] = CONF_UNIT_SYSTEM_METRIC
|
2019-05-23 00:24:46 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
info["unit_system"] = CONF_UNIT_SYSTEM_IMPERIAL
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
if location_info.latitude:
|
2019-07-31 19:25:30 +00:00
|
|
|
info["latitude"] = location_info.latitude
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
if location_info.longitude:
|
2019-07-31 19:25:30 +00:00
|
|
|
info["longitude"] = location_info.longitude
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
if location_info.time_zone:
|
2019-07-31 19:25:30 +00:00
|
|
|
info["time_zone"] = location_info.time_zone
|
2019-05-23 00:24:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_result(msg["id"], info)
|