Use entity class attributes for azure_devops (#52698)

This commit is contained in:
Robert Hillis 2021-07-11 16:42:52 -04:00 committed by GitHub
parent 5849a97150
commit 9b577e830d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 51 deletions

View file

@ -55,38 +55,22 @@ class AzureDevOpsEntity(Entity):
def __init__(self, organization: str, project: str, name: str, icon: str) -> None: def __init__(self, organization: str, project: str, name: str, icon: str) -> None:
"""Initialize the Azure DevOps entity.""" """Initialize the Azure DevOps entity."""
self._name = name self._attr_name = name
self._icon = icon self._attr_icon = icon
self._available = True
self.organization = organization self.organization = organization
self.project = project self.project = project
@property
def name(self) -> str:
"""Return the name of the entity."""
return self._name
@property
def icon(self) -> str:
"""Return the mdi icon of the entity."""
return self._icon
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
async def async_update(self) -> None: async def async_update(self) -> None:
"""Update Azure DevOps entity.""" """Update Azure DevOps entity."""
if await self._azure_devops_update(): if await self._azure_devops_update():
self._available = True self._attr_available = True
else: else:
if self._available: if self._attr_available:
_LOGGER.debug( _LOGGER.debug(
"An error occurred while updating Azure DevOps sensor", "An error occurred while updating Azure DevOps sensor",
exc_info=True, exc_info=True,
) )
self._available = False self._attr_available = False
async def _azure_devops_update(self) -> None: async def _azure_devops_update(self) -> None:
"""Update Azure DevOps entity.""" """Update Azure DevOps entity."""

View file

@ -71,38 +71,14 @@ class AzureDevOpsSensor(AzureDevOpsDeviceEntity, SensorEntity):
unit_of_measurement: str = "", unit_of_measurement: str = "",
) -> None: ) -> None:
"""Initialize Azure DevOps sensor.""" """Initialize Azure DevOps sensor."""
self._state = None self._attr_unit_of_measurement = unit_of_measurement
self._attributes = None
self._available = False
self._unit_of_measurement = unit_of_measurement
self.measurement = measurement
self.client = client self.client = client
self.organization = organization self.organization = organization
self.project = project self.project = project
self.key = key self._attr_unique_id = "_".join([organization, key])
super().__init__(organization, project, name, icon) super().__init__(organization, project, name, icon)
@property
def unique_id(self) -> str:
"""Return the unique ID for this sensor."""
return "_".join([self.organization, self.key])
@property
def state(self) -> str:
"""Return the state of the sensor."""
return self._state
@property
def extra_state_attributes(self) -> object:
"""Return the attributes of the sensor."""
return self._attributes
@property
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
return self._unit_of_measurement
class AzureDevOpsLatestBuildSensor(AzureDevOpsSensor): class AzureDevOpsLatestBuildSensor(AzureDevOpsSensor):
"""Defines a Azure DevOps card count sensor.""" """Defines a Azure DevOps card count sensor."""
@ -129,10 +105,10 @@ class AzureDevOpsLatestBuildSensor(AzureDevOpsSensor):
) )
except aiohttp.ClientError as exception: except aiohttp.ClientError as exception:
_LOGGER.warning(exception) _LOGGER.warning(exception)
self._available = False self._attr_available = False
return False return False
self._state = build.build_number self._attr_state = build.build_number
self._attributes = { self._attr_extra_state_attributes = {
"definition_id": build.definition.id, "definition_id": build.definition.id,
"definition_name": build.definition.name, "definition_name": build.definition.name,
"id": build.id, "id": build.id,
@ -146,5 +122,5 @@ class AzureDevOpsLatestBuildSensor(AzureDevOpsSensor):
"start_time": build.start_time, "start_time": build.start_time,
"finish_time": build.finish_time, "finish_time": build.finish_time,
} }
self._available = True self._attr_available = True
return True return True