Component for custom frontend cards#11866
Component for custom frontend cards#11866c727 wants to merge 13 commits intohome-assistant:devfrom c727:patch-1
Conversation
|
|
||
| return True | ||
|
|
||
| class CustomCard(Entity): |
|
|
||
|
|
||
| if not entities: | ||
| return False |
| entities.append(CustomCard(object_id, ha_card, state_card, more_info_card, config)) | ||
|
|
||
|
|
||
| if not entities: |
| state_card = cfg.get(CONF_STATE_CARD) | ||
| more_info_card = cfg.get(CONF_MORE_INFO_CARD) | ||
| config = cfg.get(CONF_CONFIG) | ||
| entities.append(CustomCard(object_id, ha_card, state_card, more_info_card, config)) |
There was a problem hiding this comment.
line too long (91 > 79 characters)
| else: | ||
| self.entity_id = ENTITY_ID_FORMAT_STATE_CARD.format(object_id) | ||
| self._state = state_card | ||
|
|
| """Representation of a custom card.""" | ||
|
|
||
| def __init__(self, object_id, ha_card, state_card, more_info_card, config): | ||
| """Initialize a custom card.""" |
| return False | ||
|
|
||
| entities.append(CustomCard(object_id, ha_card, state_card, | ||
| more_info_card, config)) |
There was a problem hiding this comment.
continuation line over-indented for visual indent
| config = cfg.get(CONF_CONFIG) | ||
|
|
||
| if ha_card == None and state_card == None: | ||
| _LOGGER.error("Entity config must contain ha_card and/or state_card ({}).".format(object_id)) |
There was a problem hiding this comment.
line too long (105 > 79 characters)
| 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.
comparison to None should be 'if cond is None:'
| return False | ||
|
|
||
| entities.append(CustomCard(object_id, ha_card, state_card, | ||
| more_info_card, config)) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
|
|
||
| if ha_card is None and state_card is None: | ||
| _LOGGER.error("Entity config must contain ha_card " + | ||
| "and/or state_card ({}).".format(object_id)) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
|
|
||
| DOMAIN = 'custom_card' | ||
|
|
||
| ENTITY_ID_FORMAT_HA_CARD = 'custom_ha_card' + '.{}' |
There was a problem hiding this comment.
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.
So I should create 2 components instead? The problem is that cards on frontend are domain specific
There was a problem hiding this comment.
Hmm.. let me check the code again...
|
Great idea! Don't forget to add tests. |
|
I think this solution is too partial. There are a few issue that we want to solve:
My proposal (all steps independent):
|
|
thanks for the feedback and linking your RFC, I will check it :) |
@balloob |
|
Note that there is already an API to create/update "dummy" entities. It's the StateMachine set_state API. It's however lower layer and thus shouldn't be linked to services (it's available via Rest API though). You can always do a custom component like this: @asyncio.coroutine
def async_setup(hass, config):
hass.states.async_set('custom_card.bla', 'yay', { 'custom_card': 'slim-camera' }) |
| return web.Response( | ||
| body = str(res), | ||
| status = state, | ||
| content_type = 'application/json', |
There was a problem hiding this comment.
unexpected spaces around keyword / parameter equals
|
|
||
| return web.Response( | ||
| body = str(res), | ||
| status = state, |
There was a problem hiding this comment.
unexpected spaces around keyword / parameter equals
| state = 200 | ||
|
|
||
| return web.Response( | ||
| body = str(res), |
There was a problem hiding this comment.
unexpected spaces around keyword / parameter equals
| res = { "config": None } | ||
| state = 400 | ||
| else: | ||
| res = { "config": config } |
There was a problem hiding this comment.
whitespace after '{'
whitespace before '}'
| try: | ||
| config = self._card_configs[data['entity_id']] | ||
| except KeyError: | ||
| res = { "config": None } |
There was a problem hiding this comment.
whitespace after '{'
whitespace before '}'
| vol.Required('entity_id'): str, | ||
| })) | ||
|
|
||
| @asyncio.coroutine |
There was a problem hiding this comment.
blank lines found after function decorator
| card_configs[entity_id] = config | ||
|
|
||
| hass.states.async_set(entity_id, state, attributes) | ||
|
|
| self.assertEqual('full-card', | ||
| state_3.attributes.get('full_card')) | ||
| self.assertEqual('state-card', | ||
| state_3.attributes.get('state_card')) |
| 'full_card': 'full-card', | ||
| }, | ||
| 'test_2': { | ||
| 'state_card': 'state-card', |
| setup_component(self.hass, DOMAIN, {DOMAIN: cfg})) | ||
|
|
||
|
|
||
| def test_config_options(self): |
| import logging | ||
|
|
||
| from homeassistant.core import CoreState, State | ||
| from homeassistant.setup import setup_component, async_setup_component |
There was a problem hiding this comment.
'homeassistant.setup.async_setup_component' imported but unused
| import unittest | ||
| import logging | ||
|
|
||
| from homeassistant.core import CoreState, State |
There was a problem hiding this comment.
'homeassistant.core.CoreState' imported but unused
'homeassistant.core.State' imported but unused
| @@ -0,0 +1,86 @@ | |||
| """The tests the custom cards component.""" | |||
| # pylint: disable=protected-access | |||
| import asyncio | |||
| try: | ||
| config = self._card_configs[data['entity_id']] | ||
| except KeyError: | ||
| return self.json_message('For this entity is no config available.', HTTP_BAD_REQUEST) |
There was a problem hiding this comment.
line too long (97 > 79 characters)
| config = self._card_configs[data['entity_id']] | ||
| except KeyError: | ||
| return self.json_message('For this entity is no config available.', | ||
| HTTP_BAD_REQUEST) |
There was a problem hiding this comment.
Should be 404 (not found) instead of 400 (bad request)
| more_info_card = cfg.get(CONF_MORE_INFO_CARD) | ||
| config = cfg.get(CONF_CONFIG) | ||
|
|
||
| if full_card is None and state_card is None: |
| if config: | ||
| card_configs[entity_id] = json.dumps(config) | ||
|
|
||
| hass.states.async_set(entity_id, state, attributes) |
There was a problem hiding this comment.
We will need 2 components. We can't have 1 component create entities that are under another domain.
|
functionality will be included in frontend entity registry |
Description:
Show custom ha-cards and sate-cards on Home Assistant frontend (custom ui)
For more details or discussion please check the frontend PR.
Frontend PR: home-assistant/frontend#831
Docs PR: home-assistant/home-assistant.io#4552
Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass