Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully Kiosk: Truncate long URLs #92347

Merged
merged 2 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/fully_kiosk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
coordinator.async_update_listeners()

await async_setup_services(hass)

Expand Down
51 changes: 43 additions & 8 deletions homeassistant/components/fully_kiosk/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfInformation
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType

Expand Down Expand Up @@ -52,11 +52,6 @@ class FullySensorEntityDescription(SensorEntityDescription):
name="Foreground app",
entity_category=EntityCategory.DIAGNOSTIC,
),
FullySensorEntityDescription(
key="currentPage",
name="Current page",
entity_category=EntityCategory.DIAGNOSTIC,
),
FullySensorEntityDescription(
key="internalStorageFreeSpace",
name="Internal storage free space",
Expand Down Expand Up @@ -95,6 +90,14 @@ class FullySensorEntityDescription(SensorEntityDescription):
),
)

URL_SENSORS: tuple[FullySensorEntityDescription, ...] = (
FullySensorEntityDescription(
key="currentPage",
name="Current page",
entity_category=EntityCategory.DIAGNOSTIC,
),
)


async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -110,6 +113,11 @@ async def async_setup_entry(
for description in SENSORS
if description.key in coordinator.data
)
async_add_entities(
FullyUrlSensor(coordinator, description)
for description in URL_SENSORS
if description.key in coordinator.data
)


class FullySensor(FullyKioskEntity, SensorEntity):
Expand All @@ -129,8 +137,12 @@ def __init__(

super().__init__(coordinator)

@property
def native_value(self) -> StateType:
@callback
def _handle_coordinator_update(self) -> None:
self._attr_native_value = self._value()
self.async_write_ha_state()

def _value(self) -> StateType:
"""Return the state of the sensor."""
if (value := self.coordinator.data.get(self.entity_description.key)) is None:
return None
Expand All @@ -139,3 +151,26 @@ def native_value(self) -> StateType:
return self.entity_description.state_fn(value)

return value # type: ignore[no-any-return]


class FullyUrlSensor(FullySensor):
"""Representation of a Fully Kiosk Browser URL sensor."""
mheath marked this conversation as resolved.
Show resolved Hide resolved

@callback
def _handle_coordinator_update(self) -> None:
value = self._value()
if value is None:
truncated = False
self._attr_native_value = None
else:
value = str(value)
truncated = len(value) > 256
if truncated:
self._attr_native_value = value[0:255]
else:
self._attr_native_value = value
self._attr_extra_state_attributes = {
"full_url": value,
"truncated": truncated,
}
self.async_write_ha_state()
30 changes: 30 additions & 0 deletions tests/components/fully_kiosk/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ async def test_sensors_sensors(
assert state.state == "https://homeassistant.local"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Amazon Fire Current page"
assert state.attributes.get("full_url") == "https://homeassistant.local"
assert not state.attributes.get("truncated")

entry = entity_registry.async_get("sensor.amazon_fire_current_page")
assert entry
Expand Down Expand Up @@ -154,3 +156,31 @@ async def test_sensors_sensors(
state = hass.states.get("sensor.amazon_fire_internal_storage_free_space")
assert state
assert state.state == STATE_UNAVAILABLE


async def test_url_sensor_truncating(
hass: HomeAssistant,
mock_fully_kiosk: MagicMock,
init_integration: MockConfigEntry,
) -> None:
"""Test that long URLs get truncated."""
state = hass.states.get("sensor.amazon_fire_current_page")
assert state
assert state.state == "https://homeassistant.local"
assert state.attributes.get("full_url") == "https://homeassistant.local"
assert not state.attributes.get("truncated")

long_url = "https://01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
assert len(long_url) > 256

mock_fully_kiosk.getDeviceInfo.return_value = {
"currentPage": long_url,
}
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()

state = hass.states.get("sensor.amazon_fire_current_page")
assert state
assert state.state == long_url[0:255]
assert state.attributes.get("full_url") == long_url
assert state.attributes.get("truncated")