2017-02-12 19:31:46 +00:00
|
|
|
"""Component to interact with Hassbian tools."""
|
|
|
|
|
2022-10-19 02:15:55 +00:00
|
|
|
from typing import Any
|
|
|
|
|
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
|
2023-08-08 10:19:20 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView, require_admin
|
2023-02-03 15:30:50 +00:00
|
|
|
from homeassistant.components.sensor import async_update_suggested_units
|
2017-02-12 19:31:46 +00:00
|
|
|
from homeassistant.config import async_check_ha_config_file
|
2022-10-19 02:15:55 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-05-23 00:24:46 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2022-01-11 16:33:50 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2022-10-14 14:50:04 +00:00
|
|
|
from homeassistant.util import location, unit_system
|
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
|
|
|
|
2023-08-08 10:19:20 +00:00
|
|
|
@require_admin
|
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
|
2019-07-31 19:25:30 +00:00
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
|
|
|
"type": "config/core/update",
|
2023-02-03 15:30:50 +00:00
|
|
|
vol.Optional("country"): cv.country,
|
|
|
|
vol.Optional("currency"): cv.currency,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional("elevation"): int,
|
2021-08-09 07:38:09 +00:00
|
|
|
vol.Optional("external_url"): vol.Any(cv.url_no_path, None),
|
|
|
|
vol.Optional("internal_url"): vol.Any(cv.url_no_path, None),
|
2022-11-24 22:25:50 +00:00
|
|
|
vol.Optional("language"): cv.language,
|
2023-02-03 15:30:50 +00:00
|
|
|
vol.Optional("latitude"): cv.latitude,
|
|
|
|
vol.Optional("location_name"): str,
|
|
|
|
vol.Optional("longitude"): cv.longitude,
|
|
|
|
vol.Optional("time_zone"): cv.time_zone,
|
|
|
|
vol.Optional("update_units"): bool,
|
|
|
|
vol.Optional("unit_system"): unit_system.validate_unit_system,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2022-02-09 18:09:55 +00:00
|
|
|
@websocket_api.async_response
|
2022-10-19 02:15:55 +00:00
|
|
|
async def websocket_update_config(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
connection: websocket_api.ActiveConnection,
|
|
|
|
msg: dict[str, Any],
|
|
|
|
) -> None:
|
2019-05-23 00:24:46 +00:00
|
|
|
"""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
|
|
|
|
2023-02-03 15:30:50 +00:00
|
|
|
update_units = data.pop("update_units", False)
|
|
|
|
|
2019-05-23 00:24:46 +00:00
|
|
|
try:
|
2019-06-01 06:03:45 +00:00
|
|
|
await hass.config.async_update(**data)
|
2023-02-03 15:30:50 +00:00
|
|
|
if update_units:
|
|
|
|
async_update_suggested_units(hass)
|
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
|
2019-07-31 19:25:30 +00:00
|
|
|
@websocket_api.websocket_command({"type": "config/core/detect"})
|
2022-02-09 18:09:55 +00:00
|
|
|
@websocket_api.async_response
|
2022-10-19 02:15:55 +00:00
|
|
|
async def websocket_detect_config(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
connection: websocket_api.ActiveConnection,
|
|
|
|
msg: dict[str, Any],
|
|
|
|
) -> None:
|
2019-05-23 00:24:46 +00:00
|
|
|
"""Detect core config."""
|
2022-01-11 16:33:50 +00:00
|
|
|
session = async_get_clientsession(hass)
|
2019-05-23 00:24:46 +00:00
|
|
|
location_info = await location.async_detect_location_info(session)
|
|
|
|
|
2022-10-19 02:15:55 +00:00
|
|
|
info: dict[str, Any] = {}
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-10-14 14:50:04 +00:00
|
|
|
# We don't want any integrations to use the name of the unit system
|
|
|
|
# so we are using the private attribute here
|
2019-05-23 00:24:46 +00:00
|
|
|
if location_info.use_metric:
|
2022-10-14 14:50:04 +00:00
|
|
|
# pylint: disable-next=protected-access
|
|
|
|
info["unit_system"] = unit_system._CONF_UNIT_SYSTEM_METRIC
|
2019-05-23 00:24:46 +00:00
|
|
|
else:
|
2022-10-14 14:50:04 +00:00
|
|
|
# pylint: disable-next=protected-access
|
2022-10-19 11:31:08 +00:00
|
|
|
info["unit_system"] = unit_system._CONF_UNIT_SYSTEM_US_CUSTOMARY
|
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
|
|
|
|
2021-07-28 04:05:16 +00:00
|
|
|
if location_info.currency:
|
|
|
|
info["currency"] = location_info.currency
|
|
|
|
|
2022-11-29 18:52:49 +00:00
|
|
|
if location_info.country_code:
|
|
|
|
info["country"] = location_info.country_code
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
connection.send_result(msg["id"], info)
|