Adjust type hints in gitlab_ci (#77493)

pull/77561/head
epenet 2022-08-30 21:05:49 +02:00 committed by GitHub
parent c8c9a4b09f
commit 809aa6b30f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 54 deletions

View File

@ -55,7 +55,7 @@ def setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the GitLab sensor platform.""" """Set up the GitLab sensor platform."""
_name = config.get(CONF_NAME) _name = config.get(CONF_NAME, DEFAULT_NAME)
_interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL) _interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL)
_url = config.get(CONF_URL) _url = config.get(CONF_URL)
@ -74,55 +74,18 @@ class GitLabSensor(SensorEntity):
_attr_attribution = ATTRIBUTION _attr_attribution = ATTRIBUTION
def __init__(self, gitlab_data, name): def __init__(self, gitlab_data: GitLabData, name: str) -> None:
"""Initialize the GitLab sensor.""" """Initialize the GitLab sensor."""
self._available = False self._attr_available = False
self._state = None
self._started_at = None
self._finished_at = None
self._duration = None
self._commit_id = None
self._commit_date = None
self._build_id = None
self._branch = None
self._gitlab_data = gitlab_data self._gitlab_data = gitlab_data
self._name = name self._attr_name = name
@property @property
def name(self): def icon(self) -> str:
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@property
def available(self):
"""Return True if entity is available."""
return self._available
@property
def extra_state_attributes(self):
"""Return the state attributes."""
return {
ATTR_BUILD_STATUS: self._state,
ATTR_BUILD_STARTED: self._started_at,
ATTR_BUILD_FINISHED: self._finished_at,
ATTR_BUILD_DURATION: self._duration,
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,
}
@property
def icon(self):
"""Return the icon to use in the frontend.""" """Return the icon to use in the frontend."""
if self._state == "success": if self.native_value == "success":
return ICON_HAPPY return ICON_HAPPY
if self._state == "failed": if self.native_value == "failed":
return ICON_SAD return ICON_SAD
return ICON_OTHER return ICON_OTHER
@ -130,15 +93,18 @@ class GitLabSensor(SensorEntity):
"""Collect updated data from GitLab API.""" """Collect updated data from GitLab API."""
self._gitlab_data.update() self._gitlab_data.update()
self._state = self._gitlab_data.status self._attr_native_value = self._gitlab_data.status
self._started_at = self._gitlab_data.started_at self._attr_extra_state_attributes = {
self._finished_at = self._gitlab_data.finished_at ATTR_BUILD_STATUS: self._gitlab_data.status,
self._duration = self._gitlab_data.duration ATTR_BUILD_STARTED: self._gitlab_data.started_at,
self._commit_id = self._gitlab_data.commit_id ATTR_BUILD_FINISHED: self._gitlab_data.finished_at,
self._commit_date = self._gitlab_data.commit_date ATTR_BUILD_DURATION: self._gitlab_data.duration,
self._build_id = self._gitlab_data.build_id ATTR_BUILD_COMMIT_ID: self._gitlab_data.commit_id,
self._branch = self._gitlab_data.branch ATTR_BUILD_COMMIT_DATE: self._gitlab_data.commit_date,
self._available = self._gitlab_data.available ATTR_BUILD_ID: self._gitlab_data.build_id,
ATTR_BUILD_BRANCH: self._gitlab_data.branch,
}
self._attr_available = self._gitlab_data.available
class GitLabData: class GitLabData:
@ -162,7 +128,7 @@ class GitLabData:
self.build_id = None self.build_id = None
self.branch = None self.branch = None
def _update(self): def _update(self) -> None:
try: try:
_projects = self._gitlab.projects.get(self._gitlab_id) _projects = self._gitlab.projects.get(self._gitlab_id)
_last_pipeline = _projects.pipelines.list(page=1)[0] _last_pipeline = _projects.pipelines.list(page=1)[0]