Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions homeassistant/components/custom_card.py
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' + '.{}'

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Author

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...

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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line over-indented for visual indent


if not entities:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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."""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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