-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Component for custom frontend cards #11866
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e954b70
Component for custom frontend cards
87fef66
Improve code
21d2852
Fix Hound
7720351
Fix Hound
174da22
Update based on feedback
dac1609
Hound
2736fca
Lint
ac915aa
Added tests, need help here
53f7ff0
Improvenents for config
5b1abbe
Hound
56e40b4
Update
7fd87f5
Fix component
179c364
Fix tests
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 hidden or 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,102 @@ | ||
| """ | ||
| Show custom ha-cards and sate-cards on Home Assistant frontend | ||
|
|
||
| For more details about this component, please refer to the documentation | ||
| at https://home-assistant.io/components/custom_card/ | ||
| """ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.helpers.entity_component import EntityComponent | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| DOMAIN = 'custom_card' | ||
|
|
||
| ENTITY_ID_FORMAT_HA_CARD = 'custom_ha_card' + '.{}' | ||
| ENTITY_ID_FORMAT_STATE_CARD = 'custom_state_card' + '.{}' | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_HA_CARD = 'ha_card' | ||
| CONF_STATE_CARD = 'state_card' | ||
| CONF_MORE_INFO_CARD = 'more_info_card' | ||
| CONF_CONFIG = 'config' | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| cv.slug: vol.Any({ | ||
| vol.Optional(CONF_HA_CARD): cv.string, | ||
| vol.Optional(CONF_STATE_CARD): cv.string, | ||
| vol.Optional(CONF_MORE_INFO_CARD): cv.string, | ||
| vol.Optional(CONF_CONFIG): cv.match_all, | ||
| }, None) | ||
| }) | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
|
|
||
| @asyncio.coroutine | ||
| def async_setup(hass, config): | ||
| """Initialize custom card.""" | ||
| component = EntityComponent(_LOGGER, DOMAIN, hass) | ||
|
|
||
| entities = [] | ||
|
|
||
| for object_id, cfg in config[DOMAIN].items(): | ||
| if not cfg: | ||
| cfg = {} | ||
|
|
||
| ha_card = cfg.get(CONF_HA_CARD) | ||
| state_card = cfg.get(CONF_STATE_CARD) | ||
| more_info_card = cfg.get(CONF_MORE_INFO_CARD) | ||
| config = cfg.get(CONF_CONFIG) | ||
|
|
||
| if ha_card is None and state_card is None: | ||
| _LOGGER.error("Entity config must contain ha_card " + | ||
| "and/or state_card (%s)", object_id) | ||
| return False | ||
|
|
||
| entities.append(CustomCard(object_id, ha_card, state_card, | ||
| more_info_card, config)) | ||
|
|
||
| if not entities: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too many blank lines (2) |
||
| return False | ||
|
|
||
| yield from component.async_add_entities(entities) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| class CustomCard(Entity): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 2 blank lines, found 1 |
||
| """Representation of a custom card.""" | ||
|
|
||
| def __init__(self, object_id, ha_card, state_card, more_info_card, config): | ||
| """Initialize a custom card.""" | ||
| if ha_card: | ||
| self.entity_id = ENTITY_ID_FORMAT_HA_CARD.format(object_id) | ||
| self._state = ha_card | ||
| else: | ||
| self.entity_id = ENTITY_ID_FORMAT_STATE_CARD.format(object_id) | ||
| self._state = state_card | ||
|
|
||
| self._attributes = {} | ||
| if ha_card: | ||
| self._attributes['ha_card'] = ha_card | ||
| if state_card: | ||
| self._attributes['state_card'] = state_card | ||
| if more_info_card: | ||
| self._attributes['more_info_card'] = more_info_card | ||
| if config: | ||
| self._attributes['config'] = config | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return card as state.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def device_state_attributes(self): | ||
| """Return the state attributes.""" | ||
| return self._attributes | ||
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.
Components are only allowed to create entities with the domain of the entity equal to the component name.
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.
So I should create 2 components instead? The problem is that cards on frontend are domain specific
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.
Hmm.. let me check the code again...