Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -6,40 +6,47 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
ATTR_ATTRIBUTION, CONF_NAME, CONF_SCAN_INTERVAL, CONF_TOKEN, CONF_URL)
|
||||
ATTR_ATTRIBUTION,
|
||||
CONF_NAME,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_TOKEN,
|
||||
CONF_URL,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_BUILD_BRANCH = 'build branch'
|
||||
ATTR_BUILD_COMMIT_DATE = 'commit date'
|
||||
ATTR_BUILD_COMMIT_ID = 'commit id'
|
||||
ATTR_BUILD_DURATION = 'build_duration'
|
||||
ATTR_BUILD_FINISHED = 'build_finished'
|
||||
ATTR_BUILD_ID = 'build id'
|
||||
ATTR_BUILD_STARTED = 'build_started'
|
||||
ATTR_BUILD_STATUS = 'build_status'
|
||||
ATTR_BUILD_BRANCH = "build branch"
|
||||
ATTR_BUILD_COMMIT_DATE = "commit date"
|
||||
ATTR_BUILD_COMMIT_ID = "commit id"
|
||||
ATTR_BUILD_DURATION = "build_duration"
|
||||
ATTR_BUILD_FINISHED = "build_finished"
|
||||
ATTR_BUILD_ID = "build id"
|
||||
ATTR_BUILD_STARTED = "build_started"
|
||||
ATTR_BUILD_STATUS = "build_status"
|
||||
ATTRIBUTION = "Information provided by https://gitlab.com/"
|
||||
|
||||
CONF_GITLAB_ID = 'gitlab_id'
|
||||
CONF_GITLAB_ID = "gitlab_id"
|
||||
|
||||
DEFAULT_NAME = 'GitLab CI Status'
|
||||
DEFAULT_URL = 'https://gitlab.com'
|
||||
DEFAULT_NAME = "GitLab CI Status"
|
||||
DEFAULT_URL = "https://gitlab.com"
|
||||
|
||||
ICON_HAPPY = 'mdi:emoticon-happy'
|
||||
ICON_OTHER = 'mdi:git'
|
||||
ICON_SAD = 'mdi:emoticon-sad'
|
||||
ICON_HAPPY = "mdi:emoticon-happy"
|
||||
ICON_OTHER = "mdi:git"
|
||||
ICON_SAD = "mdi:emoticon-sad"
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=300)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_GITLAB_ID): cv.string,
|
||||
vol.Required(CONF_TOKEN): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_URL, default=DEFAULT_URL): cv.string
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_GITLAB_ID): cv.string,
|
||||
vol.Required(CONF_TOKEN): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_URL, default=DEFAULT_URL): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
@@ -52,7 +59,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
priv_token=config[CONF_TOKEN],
|
||||
gitlab_id=config[CONF_GITLAB_ID],
|
||||
interval=_interval,
|
||||
url=_url
|
||||
url=_url,
|
||||
)
|
||||
|
||||
add_entities([GitLabSensor(_gitlab_data, _name)], True)
|
||||
@@ -102,15 +109,15 @@ class GitLabSensor(Entity):
|
||||
ATTR_BUILD_COMMIT_ID: self._commit_id,
|
||||
ATTR_BUILD_COMMIT_DATE: self._commit_date,
|
||||
ATTR_BUILD_ID: self._build_id,
|
||||
ATTR_BUILD_BRANCH: self._branch
|
||||
ATTR_BUILD_BRANCH: self._branch,
|
||||
}
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon to use in the frontend."""
|
||||
if self._state == 'success':
|
||||
if self._state == "success":
|
||||
return ICON_HAPPY
|
||||
if self._state == 'failed':
|
||||
if self._state == "failed":
|
||||
return ICON_SAD
|
||||
return ICON_OTHER
|
||||
|
||||
@@ -129,15 +136,15 @@ class GitLabSensor(Entity):
|
||||
self._available = self._gitlab_data.available
|
||||
|
||||
|
||||
class GitLabData():
|
||||
class GitLabData:
|
||||
"""GitLab Data object."""
|
||||
|
||||
def __init__(self, gitlab_id, priv_token, interval, url):
|
||||
"""Fetch data from GitLab API for most recent CI job."""
|
||||
import gitlab
|
||||
|
||||
self._gitlab_id = gitlab_id
|
||||
self._gitlab = gitlab.Gitlab(
|
||||
url, private_token=priv_token, per_page=1)
|
||||
self._gitlab = gitlab.Gitlab(url, private_token=priv_token, per_page=1)
|
||||
self._gitlab.auth()
|
||||
self._gitlab_exceptions = gitlab.exceptions
|
||||
self.update = Throttle(interval)(self._update)
|
||||
@@ -157,15 +164,15 @@ class GitLabData():
|
||||
_projects = self._gitlab.projects.get(self._gitlab_id)
|
||||
_last_pipeline = _projects.pipelines.list(page=1)[0]
|
||||
_last_job = _last_pipeline.jobs.list(page=1)[0]
|
||||
self.status = _last_pipeline.attributes.get('status')
|
||||
self.started_at = _last_job.attributes.get('started_at')
|
||||
self.finished_at = _last_job.attributes.get('finished_at')
|
||||
self.duration = _last_job.attributes.get('duration')
|
||||
_commit = _last_job.attributes.get('commit')
|
||||
self.commit_id = _commit.get('id')
|
||||
self.commit_date = _commit.get('committed_date')
|
||||
self.build_id = _last_job.attributes.get('id')
|
||||
self.branch = _last_job.attributes.get('ref')
|
||||
self.status = _last_pipeline.attributes.get("status")
|
||||
self.started_at = _last_job.attributes.get("started_at")
|
||||
self.finished_at = _last_job.attributes.get("finished_at")
|
||||
self.duration = _last_job.attributes.get("duration")
|
||||
_commit = _last_job.attributes.get("commit")
|
||||
self.commit_id = _commit.get("id")
|
||||
self.commit_date = _commit.get("committed_date")
|
||||
self.build_id = _last_job.attributes.get("id")
|
||||
self.branch = _last_job.attributes.get("ref")
|
||||
self.available = True
|
||||
except self._gitlab_exceptions.GitlabAuthenticationError as erra:
|
||||
_LOGGER.error("Authentication Error: %s", erra)
|
||||
|
||||
Reference in new issue
Block a user