1 """Component providing HA sensor support for Travis CI framework."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from travispy
import TravisPy
9 from travispy.errors
import TravisError
10 import voluptuous
as vol
14 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
16 SensorEntityDescription,
20 CONF_MONITORED_CONDITIONS,
29 _LOGGER = logging.getLogger(__name__)
31 CONF_BRANCH =
"branch"
32 CONF_REPOSITORY =
"repository"
34 DEFAULT_BRANCH_NAME =
"master"
38 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
42 icon=
"mdi:card-account-details",
45 key=
"last_build_duration",
46 name=
"Last Build Duration",
47 native_unit_of_measurement=UnitOfTime.SECONDS,
51 key=
"last_build_finished_at",
52 name=
"Last Build Finished At",
56 key=
"last_build_started_at",
57 name=
"Last Build Started At",
61 key=
"last_build_state",
62 name=
"Last Build State",
72 SENSOR_KEYS: list[str] = [desc.key
for desc
in SENSOR_TYPES]
74 NOTIFICATION_ID =
"travisci"
75 NOTIFICATION_TITLE =
"Travis CI Sensor Setup"
77 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
79 vol.Required(CONF_API_KEY): cv.string,
80 vol.Required(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
81 cv.ensure_list, [vol.In(SENSOR_KEYS)]
83 vol.Required(CONF_BRANCH, default=DEFAULT_BRANCH_NAME): cv.string,
84 vol.Optional(CONF_REPOSITORY, default=[]): vol.All(cv.ensure_list, [cv.string]),
85 vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
93 add_entities: AddEntitiesCallback,
94 discovery_info: DiscoveryInfoType |
None =
None,
96 """Set up the Travis CI sensor."""
98 token = config[CONF_API_KEY]
99 repositories = config[CONF_REPOSITORY]
100 branch = config[CONF_BRANCH]
103 travis = TravisPy.github_auth(token)
106 except TravisError
as ex:
107 _LOGGER.error(
"Unable to connect to Travis CI service: %s",
str(ex))
108 persistent_notification.create(
110 f
"Error: {ex}<br />You will need to restart hass after fixing.",
111 title=NOTIFICATION_TITLE,
112 notification_id=NOTIFICATION_ID,
118 all_repos = travis.repos(member=user.login)
119 repositories = [repo.slug
for repo
in all_repos]
122 monitored_conditions = config[CONF_MONITORED_CONDITIONS]
123 for repo
in repositories:
125 repo = f
"{user.login}/{repo}"
130 for description
in SENSOR_TYPES
131 if description.key
in monitored_conditions
139 """Representation of a Travis CI sensor."""
141 _attr_attribution =
"Information provided by https://travis-ci.org/"
144 self, data, repo_name, user, branch, description: SensorEntityDescription
146 """Initialize the sensor."""
154 self.
_attr_name_attr_name = f
"{repo_name} {description.name}"
158 """Return the state attributes."""
163 attrs[
"Owner Name"] = self.
_user_user.name
164 attrs[
"Owner Email"] = self.
_user_user.email
166 attrs[
"Committer Name"] = self.
_build_build.commit.committer_name
167 attrs[
"Committer Email"] = self.
_build_build.commit.committer_email
168 attrs[
"Commit Branch"] = self.
_build_build.commit.branch
169 attrs[
"Committed Date"] = self.
_build_build.commit.committed_at
170 attrs[
"Commit SHA"] = self.
_build_build.commit.sha
175 """Get the latest data and updates the states."""
176 _LOGGER.debug(
"Updating sensor %s", self.
namename)
187 param = sensor_type.replace(
"last_build_",
"")
def extra_state_attributes(self)
None __init__(self, data, repo_name, user, branch, SensorEntityDescription description)
str|UndefinedType|None name(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)