Home Assistant Unofficial Reference 2024.12.1
core.py
Go to the documentation of this file.
1 """Component to interact with Hassbian tools."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiohttp import web
8 import voluptuous as vol
9 
10 from homeassistant.components import websocket_api
11 from homeassistant.components.http import KEY_HASS, HomeAssistantView, require_admin
12 from homeassistant.components.sensor import async_update_suggested_units
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers import check_config, config_validation as cv
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 from homeassistant.util import location, unit_system
17 
18 
19 @callback
20 def async_setup(hass: HomeAssistant) -> bool:
21  """Set up the Hassbian config."""
22  hass.http.register_view(CheckConfigView)
23  websocket_api.async_register_command(hass, websocket_update_config)
24  websocket_api.async_register_command(hass, websocket_detect_config)
25  return True
26 
27 
28 class CheckConfigView(HomeAssistantView):
29  """Hassbian packages endpoint."""
30 
31  url = "/api/config/core/check_config"
32  name = "api:config:core:check_config"
33 
34  @require_admin
35  async def post(self, request: web.Request) -> web.Response:
36  """Validate configuration and return results."""
37 
38  res = await check_config.async_check_ha_config_file(request.app[KEY_HASS])
39 
40  state = "invalid" if res.errors else "valid"
41 
42  return self.json(
43  {
44  "result": state,
45  "errors": res.error_str or None,
46  "warnings": res.warning_str or None,
47  }
48  )
49 
50 
51 @websocket_api.require_admin
52 @websocket_api.websocket_command( { "type": "config/core/update", vol.Optional("country"): cv.country,
53  vol.Optional("currency"): cv.currency,
54  vol.Optional("elevation"): int,
55  vol.Optional("external_url"): vol.Any(cv.url_no_path, None),
56  vol.Optional("internal_url"): vol.Any(cv.url_no_path, None),
57  vol.Optional("language"): cv.language,
58  vol.Optional("latitude"): cv.latitude,
59  vol.Optional("location_name"): str,
60  vol.Optional("longitude"): cv.longitude,
61  vol.Optional("radius"): cv.positive_int,
62  vol.Optional("time_zone"): cv.time_zone,
63  vol.Optional("update_units"): bool,
64  vol.Optional("unit_system"): unit_system.validate_unit_system,
65  }
66 )
67 @websocket_api.async_response
68 async def websocket_update_config(
69  hass: HomeAssistant,
71  msg: dict[str, Any],
72 ) -> None:
73  """Handle update core config command."""
74  data = dict(msg)
75  data.pop("id")
76  data.pop("type")
77 
78  update_units = data.pop("update_units", False)
79 
80  try:
81  await hass.config.async_update(**data)
82  if update_units:
84  connection.send_result(msg["id"])
85  except ValueError as err:
86  connection.send_error(msg["id"], "invalid_info", str(err))
87 
88 
89 @websocket_api.require_admin
90 @websocket_api.websocket_command({"type": "config/core/detect"})
91 @websocket_api.async_response
92 async def websocket_detect_config(
93  hass: HomeAssistant,
95  msg: dict[str, Any],
96 ) -> None:
97  """Detect core config."""
98  session = async_get_clientsession(hass)
99  location_info = await location.async_detect_location_info(session)
100 
101  info: dict[str, Any] = {}
102 
103  if location_info is None:
104  connection.send_result(msg["id"], info)
105  return
106 
107  # We don't want any integrations to use the name of the unit system
108  # so we are using the private attribute here
109  if location_info.use_metric:
110  info["unit_system"] = unit_system._CONF_UNIT_SYSTEM_METRIC # noqa: SLF001
111  else:
112  info["unit_system"] = unit_system._CONF_UNIT_SYSTEM_US_CUSTOMARY # noqa: SLF001
113 
114  if location_info.latitude:
115  info["latitude"] = location_info.latitude
116 
117  if location_info.longitude:
118  info["longitude"] = location_info.longitude
119 
120  if location_info.time_zone:
121  info["time_zone"] = location_info.time_zone
122 
123  if location_info.currency:
124  info["currency"] = location_info.currency
125 
126  if location_info.country_code:
127  info["country"] = location_info.country_code
128 
129  connection.send_result(msg["id"], info)
130 
web.Response post(self, web.Request request)
Definition: core.py:35
None websocket_update_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: core.py:75
None websocket_detect_config(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
Definition: core.py:99
bool async_setup(HomeAssistant hass)
Definition: core.py:20
None async_update_suggested_units(HomeAssistant hass)
Definition: __init__.py:942
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)