forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config flow for Time & Date (home-assistant#104183)
Co-authored-by: Erik <[email protected]>
- Loading branch information
Showing
15 changed files
with
574 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1,18 @@ | ||
"""The time_date component.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
from .const import PLATFORMS | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up Time & Date from a config entry.""" | ||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
return True | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Unload Time & Date config entry.""" | ||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
"""Adds config flow for Time & Date integration.""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Mapping | ||
from datetime import timedelta | ||
import logging | ||
from typing import Any | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.components import websocket_api | ||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity_platform import EntityPlatform | ||
from homeassistant.helpers.schema_config_entry_flow import ( | ||
SchemaCommonFlowHandler, | ||
SchemaConfigFlowHandler, | ||
SchemaFlowError, | ||
SchemaFlowFormStep, | ||
) | ||
from homeassistant.helpers.selector import ( | ||
SelectSelector, | ||
SelectSelectorConfig, | ||
SelectSelectorMode, | ||
) | ||
from homeassistant.setup import async_prepare_setup_platform | ||
|
||
from .const import CONF_DISPLAY_OPTIONS, DOMAIN, OPTION_TYPES | ||
from .sensor import TimeDateSensor | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
USER_SCHEMA = vol.Schema( | ||
{ | ||
vol.Required(CONF_DISPLAY_OPTIONS): SelectSelector( | ||
SelectSelectorConfig( | ||
options=[option for option in OPTION_TYPES if option != "beat"], | ||
mode=SelectSelectorMode.DROPDOWN, | ||
translation_key="display_options", | ||
) | ||
), | ||
} | ||
) | ||
|
||
|
||
async def validate_input( | ||
handler: SchemaCommonFlowHandler, user_input: dict[str, Any] | ||
) -> dict[str, Any]: | ||
"""Validate rest setup.""" | ||
hass = handler.parent_handler.hass | ||
if hass.config.time_zone is None: | ||
raise SchemaFlowError("timezone_not_exist") | ||
return user_input | ||
|
||
|
||
CONFIG_FLOW = { | ||
"user": SchemaFlowFormStep( | ||
schema=USER_SCHEMA, | ||
preview=DOMAIN, | ||
validate_user_input=validate_input, | ||
) | ||
} | ||
|
||
|
||
class TimeDateConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN): | ||
"""Handle a config flow for Time & Date.""" | ||
|
||
config_flow = CONFIG_FLOW | ||
|
||
def async_config_entry_title(self, options: Mapping[str, Any]) -> str: | ||
"""Return config entry title.""" | ||
return f"Time & Date {options[CONF_DISPLAY_OPTIONS]}" | ||
|
||
def async_config_flow_finished(self, options: Mapping[str, Any]) -> None: | ||
"""Abort if instance already exist.""" | ||
self._async_abort_entries_match(dict(options)) | ||
|
||
@staticmethod | ||
async def async_setup_preview(hass: HomeAssistant) -> None: | ||
"""Set up preview WS API.""" | ||
websocket_api.async_register_command(hass, ws_start_preview) | ||
|
||
|
||
@websocket_api.websocket_command( | ||
{ | ||
vol.Required("type"): "time_date/start_preview", | ||
vol.Required("flow_id"): str, | ||
vol.Required("flow_type"): vol.Any("config_flow"), | ||
vol.Required("user_input"): dict, | ||
} | ||
) | ||
@websocket_api.async_response | ||
async def ws_start_preview( | ||
hass: HomeAssistant, | ||
connection: websocket_api.ActiveConnection, | ||
msg: dict[str, Any], | ||
) -> None: | ||
"""Generate a preview.""" | ||
validated = USER_SCHEMA(msg["user_input"]) | ||
|
||
# Create an EntityPlatform, needed for name translations | ||
platform = await async_prepare_setup_platform(hass, {}, SENSOR_DOMAIN, DOMAIN) | ||
entity_platform = EntityPlatform( | ||
hass=hass, | ||
logger=_LOGGER, | ||
domain=SENSOR_DOMAIN, | ||
platform_name=DOMAIN, | ||
platform=platform, | ||
scan_interval=timedelta(seconds=3600), | ||
entity_namespace=None, | ||
) | ||
await entity_platform.async_load_translations() | ||
|
||
@callback | ||
def async_preview_updated(state: str, attributes: Mapping[str, Any]) -> None: | ||
"""Forward config entry state events to websocket.""" | ||
connection.send_message( | ||
websocket_api.event_message( | ||
msg["id"], {"attributes": attributes, "state": state} | ||
) | ||
) | ||
|
||
preview_entity = TimeDateSensor(validated[CONF_DISPLAY_OPTIONS]) | ||
preview_entity.hass = hass | ||
preview_entity.platform = entity_platform | ||
|
||
connection.send_result(msg["id"]) | ||
connection.subscriptions[msg["id"]] = preview_entity.async_start_preview( | ||
async_preview_updated | ||
) |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,8 +1,83 @@ | ||
{ | ||
"title": "Time & Date", | ||
"config": { | ||
"abort": { | ||
"already_configured": "The chosen Time & Date sensor has already been configured" | ||
}, | ||
"step": { | ||
"user": { | ||
"description": "Select from the sensor options below", | ||
"data": { | ||
"display_options": "Sensor type" | ||
} | ||
} | ||
}, | ||
"error": { | ||
"timezone_not_exist": "Timezone is not set in Home Assistant configuration" | ||
} | ||
}, | ||
"options": { | ||
"step": { | ||
"init": { | ||
"data": { | ||
"display_options": "[%key:component::time_date::config::step::user::data::display_options%]" | ||
} | ||
} | ||
} | ||
}, | ||
"selector": { | ||
"display_options": { | ||
"options": { | ||
"time": "Time", | ||
"date": "Date", | ||
"date_time": "Date & Time", | ||
"date_time_utc": "Date & Time (UTC)", | ||
"date_time_iso": "Date & Time (ISO)", | ||
"time_date": "Time & Date", | ||
"beat": "Internet time", | ||
"time_utc": "Time (UTC)" | ||
} | ||
} | ||
}, | ||
"entity": { | ||
"sensor": { | ||
"time": { | ||
"name": "[%key:component::time_date::selector::display_options::options::time%]" | ||
}, | ||
"date": { | ||
"name": "[%key:component::time_date::selector::display_options::options::date%]" | ||
}, | ||
"date_time": { | ||
"name": "[%key:component::time_date::selector::display_options::options::date_time%]" | ||
}, | ||
"date_time_utc": { | ||
"name": "[%key:component::time_date::selector::display_options::options::date_time_utc%]" | ||
}, | ||
"date_time_iso": { | ||
"name": "[%key:component::time_date::selector::display_options::options::date_time_iso%]" | ||
}, | ||
"time_date": { | ||
"name": "[%key:component::time_date::selector::display_options::options::time_date%]" | ||
}, | ||
"beat": { | ||
"name": "[%key:component::time_date::selector::display_options::options::beat%]" | ||
}, | ||
"time_utc": { | ||
"name": "[%key:component::time_date::selector::display_options::options::time_utc%]" | ||
} | ||
} | ||
}, | ||
"issues": { | ||
"deprecated_beat": { | ||
"title": "The `{config_key}` Time & Date sensor is being removed", | ||
"description": "Please remove the `{config_key}` key from the `{display_options}` for the {integration} entry in your configuration.yaml file and restart Home Assistant to fix this issue." | ||
"fix_flow": { | ||
"step": { | ||
"confirm": { | ||
"title": "[%key:component::time_date::issues::deprecated_beat::title%]", | ||
"description": "Please remove the `{config_key}` key from the {integration} config entry options and click submit to fix this issue." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.