-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f516159
commit 1003a5f
Showing
5 changed files
with
118 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Binary sensor platform for Unfolded Circle.""" | ||
|
||
from homeassistant.components.binary_sensor import BinarySensorEntity | ||
from homeassistant.core import HomeAssistant, callback | ||
|
||
from .const import DOMAIN, PSN_COORDINATOR | ||
from .entity import PSNEntity | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entities): | ||
"""Add sensors for passed config_entry in HA.""" | ||
coordinator = hass.data[DOMAIN][config_entry.entry_id][PSN_COORDINATOR] | ||
|
||
async_add_entities([PlaystationPlusBinarySensor(coordinator)]) | ||
|
||
|
||
class PlaystationPlusBinarySensor(PSNEntity, BinarySensorEntity): | ||
"""Sensor indicating if user is subscribed to PlayStation Plus""" | ||
|
||
def __init__(self, coordinator) -> None: | ||
"""Initialize Binary Sensor.""" | ||
super().__init__(coordinator) | ||
self.coordinator = coordinator | ||
|
||
# As per the sensor, this must be a unique value within this domain. | ||
self._attr_unique_id = ( | ||
f"{coordinator.data.get("username").lower()}_has_playstation_plus" | ||
) | ||
|
||
# The name of the entity | ||
self._attr_has_entity_name = True | ||
self._attr_name = "Playstation Plus" | ||
self._attr_native_value = self.coordinator.data.get("profile").get("isPlus") | ||
self._attr_icon = "mdi:gamepad-outline" | ||
|
||
@property | ||
def is_on(self): | ||
"""Return the state of the binary sensor.""" | ||
return self.coordinator.data.get("profile", {}).get("isPlus") | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
self.async_write_ha_state() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Image sensor platform for the PlayStation Network.""" | ||
|
||
from homeassistant.components.image import ImageEntity | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.util import dt as dt_util | ||
|
||
from .const import DOMAIN, PSN_COORDINATOR | ||
from .entity import PSNEntity | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entities): | ||
"""Add sensors for passed config_entry in HA.""" | ||
coordinator = hass.data[DOMAIN][config_entry.entry_id][PSN_COORDINATOR] | ||
|
||
async_add_entities([PlaystationProfileImage(coordinator)]) | ||
|
||
|
||
class PlaystationProfileImage(PSNEntity, ImageEntity): | ||
"""Image representing your PlayStation Avatar""" | ||
|
||
def __init__(self, coordinator) -> None: | ||
"""Initialize Image.""" | ||
super().__init__(coordinator) | ||
self._attr_extra_state_attributes = {} | ||
self.coordinator = coordinator | ||
|
||
self._attr_unique_id = ( | ||
f"{self.coordinator.data.get("username").lower()}_playstation_avatar" | ||
) | ||
|
||
ImageEntity.__init__(self, coordinator.hass) | ||
self._attr_has_entity_name = True | ||
self._attr_name = "Playstation Avatar" | ||
self._attr_icon = "mdi:face-man-profile" | ||
self._attr_image_url = self.get_avatar() | ||
self._attr_image_last_updated = dt_util.utcnow() | ||
|
||
def get_avatar(self) -> str: | ||
"""Return Avatar URL""" | ||
previous_image = self._attr_image_url | ||
current_image = None | ||
for avatar in self.coordinator.data.get("profile", {}).get("avatars", []): | ||
if avatar.get("size") == "xl": | ||
current_image = avatar.get("url", None) | ||
|
||
if previous_image != current_image: | ||
self._cached_image = None | ||
self._attr_image_last_updated = dt_util.utcnow() | ||
|
||
return current_image | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
self._attr_image_url = self.get_avatar() | ||
self.async_write_ha_state() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters