Skip to content

Commit

Permalink
Merge pull request #42 from bentekkie/fix/imagecontenttype
Browse files Browse the repository at this point in the history
Guess content type if it is not image/*
  • Loading branch information
bentekkie authored Aug 13, 2023
2 parents 7851126 + a795fa0 commit 435425e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
20 changes: 19 additions & 1 deletion custom_components/generac/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Sample API Client."""
import json
import logging
from typing import Any
from typing import Mapping

import aiohttp
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -29,7 +31,7 @@ class SessionExpiredException(Exception):
pass


def get_setting_json(page: str):
def get_setting_json(page: str) -> Mapping[str, Any] | None:
for line in page.splitlines():
if line.startswith("var SETTINGS = ") and line.endswith(";"):
return json.loads(line.removeprefix("var SETTINGS = ").removesuffix(";"))
Expand Down Expand Up @@ -62,6 +64,8 @@ async def get_generator_data(self):
if apparatuses is None:
_LOGGER.debug("Could not decode apparatuses response")
return None
if not isinstance(apparatuses, list):
_LOGGER.error("Expected list from /v2/Apparatus/list got %s", apparatuses)

data: dict[str, Item] = {}
for apparatus in apparatuses:
Expand All @@ -74,6 +78,11 @@ async def get_generator_data(self):
detail_json = await self.get_endpoint(
f"/v1/Apparatus/details/{apparatus.apparatusId}"
)
if detail_json is None:
_LOGGER.debug(
f"Could not decode respose from /v1/Apparatus/details/{apparatus.apparatusId}"
)
continue
detail = from_dict(ApparatusDetail, detail_json)
data[str(apparatus.apparatusId)] = Item(apparatus, detail)
return data
Expand Down Expand Up @@ -112,12 +121,21 @@ async def login(self) -> None:
return

parse_settings = get_setting_json(login_response)
if parse_settings is None:
_LOGGER.debug(
"Unable to find csrf token in login page:\n%s", login_response
)
raise IOError("Unable to find csrf token in login page")
sign_in_config = from_dict(SignInConfig, parse_settings)

form_data = aiohttp.FormData()
form_data.add_field("request_type", "RESPONSE")
form_data.add_field("signInName", self._username)
form_data.add_field("password", self._passeword)
if sign_in_config.csrf is None or sign_in_config.transId is None:
raise IOError(
"Missing csrf and/or transId in sign in config %s", sign_in_config
)
self.csrf = sign_in_config.csrf

self_asserted_response = await self._session.post(
Expand Down
16 changes: 16 additions & 0 deletions custom_components/generac/image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Image platform for generac."""
import mimetypes

import httpx
from homeassistant.components.image import ImageEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -52,3 +55,16 @@ def image_url(self):
def available(self):
"""Return True if entity is available."""
return super().available and self.aparatus_detail.heroImageUrl is not None

async def _fetch_url(self, url: str) -> httpx.Response | None:
"""Fetch a URL."""
resp = await super()._fetch_url(url)
if (
resp is not None
and "image" not in resp.headers.get("content-type")
and self.aparatus_detail.heroImageUrl
):
guess = mimetypes.guess_type(self.aparatus_detail.heroImageUrl)[0]
if guess is not None:
resp.headers["content-type"] = guess
return resp

0 comments on commit 435425e

Please sign in to comment.