Use _attr_*
for the GitHub integration (#56419)
This commit is contained in:
parent
88e42a540e
commit
713d294627
1 changed files with 33 additions and 61 deletions
|
@ -74,12 +74,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||
class GitHubSensor(SensorEntity):
|
||||
"""Representation of a GitHub sensor."""
|
||||
|
||||
_attr_icon = "mdi:github"
|
||||
|
||||
def __init__(self, github_data):
|
||||
"""Initialize the GitHub sensor."""
|
||||
self._unique_id = github_data.repository_path
|
||||
self._name = None
|
||||
self._state = None
|
||||
self._available = False
|
||||
self._attr_unique_id = github_data.repository_path
|
||||
self._repository_path = None
|
||||
self._latest_commit_message = None
|
||||
self._latest_commit_sha = None
|
||||
|
@ -97,68 +96,15 @@ class GitHubSensor(SensorEntity):
|
|||
self._views_unique = None
|
||||
self._github_data = github_data
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return unique ID for the sensor."""
|
||||
return self._unique_id
|
||||
|
||||
@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."""
|
||||
attrs = {
|
||||
ATTR_PATH: self._github_data.repository_path,
|
||||
ATTR_NAME: self._name,
|
||||
ATTR_LATEST_COMMIT_MESSAGE: self._latest_commit_message,
|
||||
ATTR_LATEST_COMMIT_SHA: self._latest_commit_sha,
|
||||
ATTR_LATEST_RELEASE_URL: self._latest_release_url,
|
||||
ATTR_LATEST_OPEN_ISSUE_URL: self._latest_open_issue_url,
|
||||
ATTR_OPEN_ISSUES: self._open_issue_count,
|
||||
ATTR_LATEST_OPEN_PULL_REQUEST_URL: self._latest_open_pr_url,
|
||||
ATTR_OPEN_PULL_REQUESTS: self._pull_request_count,
|
||||
ATTR_STARGAZERS: self._stargazers,
|
||||
ATTR_FORKS: self._forks,
|
||||
}
|
||||
if self._latest_release_tag is not None:
|
||||
attrs[ATTR_LATEST_RELEASE_TAG] = self._latest_release_tag
|
||||
if self._clones is not None:
|
||||
attrs[ATTR_CLONES] = self._clones
|
||||
if self._clones_unique is not None:
|
||||
attrs[ATTR_CLONES_UNIQUE] = self._clones_unique
|
||||
if self._views is not None:
|
||||
attrs[ATTR_VIEWS] = self._views
|
||||
if self._views_unique is not None:
|
||||
attrs[ATTR_VIEWS_UNIQUE] = self._views_unique
|
||||
return attrs
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon to use in the frontend."""
|
||||
return "mdi:github"
|
||||
|
||||
async def async_update(self):
|
||||
"""Collect updated data from GitHub API."""
|
||||
await self._github_data.async_update()
|
||||
self._available = self._github_data.available
|
||||
if not self._available:
|
||||
self._attr_available = self._github_data.available
|
||||
if not self.available:
|
||||
return
|
||||
|
||||
self._name = self._github_data.name
|
||||
self._state = self._github_data.last_commit.sha[0:7]
|
||||
self._attr_name = self._github_data.name
|
||||
self._attr_native_value = self._github_data.last_commit.sha[0:7]
|
||||
|
||||
self._latest_commit_message = self._github_data.last_commit.commit.message
|
||||
self._latest_commit_sha = self._github_data.last_commit.sha
|
||||
|
@ -188,6 +134,32 @@ class GitHubSensor(SensorEntity):
|
|||
self._views = self._github_data.views_response.data.count
|
||||
self._views_unique = self._github_data.views_response.data.uniques
|
||||
|
||||
self._attr_extra_state_attributes = {
|
||||
ATTR_PATH: self._github_data.repository_path,
|
||||
ATTR_NAME: self.name,
|
||||
ATTR_LATEST_COMMIT_MESSAGE: self._latest_commit_message,
|
||||
ATTR_LATEST_COMMIT_SHA: self._latest_commit_sha,
|
||||
ATTR_LATEST_RELEASE_URL: self._latest_release_url,
|
||||
ATTR_LATEST_OPEN_ISSUE_URL: self._latest_open_issue_url,
|
||||
ATTR_OPEN_ISSUES: self._open_issue_count,
|
||||
ATTR_LATEST_OPEN_PULL_REQUEST_URL: self._latest_open_pr_url,
|
||||
ATTR_OPEN_PULL_REQUESTS: self._pull_request_count,
|
||||
ATTR_STARGAZERS: self._stargazers,
|
||||
ATTR_FORKS: self._forks,
|
||||
}
|
||||
if self._latest_release_tag is not None:
|
||||
self._attr_extra_state_attributes[
|
||||
ATTR_LATEST_RELEASE_TAG
|
||||
] = self._latest_release_tag
|
||||
if self._clones is not None:
|
||||
self._attr_extra_state_attributes[ATTR_CLONES] = self._clones
|
||||
if self._clones_unique is not None:
|
||||
self._attr_extra_state_attributes[ATTR_CLONES_UNIQUE] = self._clones_unique
|
||||
if self._views is not None:
|
||||
self._attr_extra_state_attributes[ATTR_VIEWS] = self._views
|
||||
if self._views_unique is not None:
|
||||
self._attr_extra_state_attributes[ATTR_VIEWS_UNIQUE] = self._views_unique
|
||||
|
||||
|
||||
class GitHubData:
|
||||
"""GitHub Data object."""
|
||||
|
|
Loading…
Add table
Reference in a new issue