diff --git a/.gitignore b/.gitignore index 9fa46f4c80..6bfc09e27f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,9 @@ lib bin dist +# vscode +.vscode + # Secrets .lokalise_token yarn-error.log diff --git a/src/common/util/parse-aspect-ratio.js b/src/common/util/parse-aspect-ratio.js new file mode 100644 index 0000000000..ea94a46cae --- /dev/null +++ b/src/common/util/parse-aspect-ratio.js @@ -0,0 +1,24 @@ +export default function parseAspectRatio(input) { + // Handle 16x9, 16:9, 1.78x1, 1.78:1, 1.78 + // Ignore everything else + function parseOrThrow(number) { + const parsed = parseFloat(number); + if (isNaN(parsed)) throw new Error(`${number} is not a number`); + return parsed; + } + try { + if (input) { + const arr = input.replace(':', 'x').split('x'); + if (arr.length === 0) { + return null; + } + + return arr.length === 1 ? + { w: parseOrThrow(arr[0]), h: 1 } : + { w: parseOrThrow(arr[0]), h: parseOrThrow(arr[1]) }; + } + } catch (err) { + // Ignore the error + } + return null; +} diff --git a/src/panels/lovelace/cards/hui-picture-entity-card.js b/src/panels/lovelace/cards/hui-picture-entity-card.js index e7f864297e..a9f1611435 100644 --- a/src/panels/lovelace/cards/hui-picture-entity-card.js +++ b/src/panels/lovelace/cards/hui-picture-entity-card.js @@ -58,6 +58,7 @@ class HuiPictureEntityCard extends EventsMixin(LocalizeMixin(PolymerElement)) { state-image="[[_config.state_image]]" camera-image="[[_getCameraImage(_config)]]" entity="[[_config.entity]]" + aspect-ratio="[[_config.aspect_ratio]]" >