-
-
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
Changes from 2 commits
e954b70
87fef66
21d2852
7720351
174da22
dac1609
2736fca
ac915aa
53f7ff0
5b1abbe
56e40b4
7fd87f5
179c364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """ | ||
| 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 == None and state_card == None: | ||
|
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. comparison to None should be 'if cond is None:' |
||
| _LOGGER.error("Entity config must contain ha_card and/or state_card ({}).".format(object_id)) | ||
|
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. line too long (105 > 79 characters) |
||
| return False | ||
|
|
||
| entities.append(CustomCard(object_id, ha_card, state_card, | ||
| more_info_card, config)) | ||
|
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. continuation line over-indented for visual indent |
||
|
|
||
| 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.""" | ||
|
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. trailing whitespace |
||
| 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 | ||
|
|
||
|
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. blank line contains whitespace |
||
| 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 | ||
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...