-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
Add config flow for Time & Date #104183
Merged
Merged
Add config flow for Time & Date #104183
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5a58f1c
Add config flow for Time & Date
gjohansson-ST cc3b6ff
feedback
gjohansson-ST b754388
Address review comments
emontnemery 4f18dce
Don't allow beat in user flow
emontnemery b1a9249
Address review comment
emontnemery 17500ba
Make issue fixable
gjohansson-ST 48d961b
Fix cleanup of unused entities
emontnemery d0395c1
Add preview
emontnemery b371447
Fix rebase mistake
emontnemery 6c65b1e
coverage
gjohansson-ST 4a75628
Change to one type per setup
gjohansson-ST 6e81610
Change to helper
gjohansson-ST b27a60d
Sensor
gjohansson-ST 07d6d85
Preview
gjohansson-ST 82424ce
Fix tests
gjohansson-ST 33392ab
Cleanup not needed
gjohansson-ST 0ddce7c
clean
gjohansson-ST ca1ee78
Change to integrtion type service
gjohansson-ST d466430
Add translatable title
gjohansson-ST 20e27a3
Fix strings
gjohansson-ST 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 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,82 @@ | ||
{ | ||
"config": { | ||
"abort": { | ||
"already_configured": "Time & Date option type has already been configured" | ||
}, | ||
"step": { | ||
"user": { | ||
"description": "Select your type below", | ||
"data": { | ||
"display_options": "Sensors" | ||
} | ||
} | ||
}, | ||
"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.
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.
This title should be translated, but maybe that's not possible?
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.
Not possible to translate the config entry title as far as I know