Skip to content

Commit

Permalink
Add discussions sensors to GitHub (home-assistant#66937)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 authored Feb 20, 2022
1 parent 2d52aca commit 5b28e2d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions homeassistant/components/github/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@
watchers(first: 1) {
total: totalCount
}
discussion: discussions(
first: 1
orderBy: {field: CREATED_AT, direction: DESC}
) {
total: totalCount
discussions: nodes {
title
url
number
}
}
issue: issues(
first: 1
states: OPEN
Expand Down
18 changes: 18 additions & 0 deletions homeassistant/components/github/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class GitHubSensorEntityDescription(BaseEntityDescription, BaseEntityDescription


SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
GitHubSensorEntityDescription(
key="discussions_count",
name="Discussions",
native_unit_of_measurement="Discussions",
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["discussion"]["total"],
),
GitHubSensorEntityDescription(
key="stargazers_count",
name="Stars",
Expand Down Expand Up @@ -96,6 +104,16 @@ class GitHubSensorEntityDescription(BaseEntityDescription, BaseEntityDescription
"url": data["default_branch_ref"]["commit"]["url"],
},
),
GitHubSensorEntityDescription(
key="latest_discussion",
name="Latest Discussion",
avabl_fn=lambda data: data["discussion"]["discussions"],
value_fn=lambda data: data["discussion"]["discussions"][0]["title"][:255],
attr_fn=lambda data: {
"url": data["discussion"]["discussions"][0]["url"],
"number": data["discussion"]["discussions"][0]["number"],
},
),
GitHubSensorEntityDescription(
key="latest_release",
name="Latest Release",
Expand Down
10 changes: 10 additions & 0 deletions tests/components/github/fixtures/graphql.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"watchers": {
"total": 9
},
"discussion": {
"total": 1,
"discussions": [
{
"title": "First discussion",
"url": "https://github.com/octocat/Hello-World/discussions/1347",
"number": 1347
}
]
},
"issue": {
"total": 1,
"issues": [
Expand Down

0 comments on commit 5b28e2d

Please sign in to comment.