diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..2ac2151 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,92 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + schedule: + - cron: '38 7 * * 3' + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: python + build-mode: none + # CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - if: matrix.build-mode == 'manual' + shell: bash + run: | + echo 'If you are using a "manual" build mode for one or more of the' \ + 'languages you are analyzing, replace this with the commands to build' \ + 'your code, for example:' + echo ' make bootstrap' + echo ' make release' + exit 1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 + with: + category: "/language:${{matrix.language}}" diff --git a/custom_components/sleepme_thermostat/__init__.py b/custom_components/sleepme_thermostat/__init__.py index 7b00c33..be352ca 100755 --- a/custom_components/sleepme_thermostat/__init__.py +++ b/custom_components/sleepme_thermostat/__init__.py @@ -61,7 +61,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: _LOGGER.debug(f"SleepMeClient and Update Manager initialized and stored in hass.data for device {device_id}.") # Forward the entry setup to the specific platforms, including the sensors platform - await hass.config_entries.async_forward_entry_setups(entry, ["climate", "binary_sensor"]) + await hass.config_entries.async_forward_entry_setups(entry, ["climate", "binary_sensor", "sensor"]) _LOGGER.info("SleepMe Thermostat component initialized successfully.") return True diff --git a/custom_components/sleepme_thermostat/manifest.json b/custom_components/sleepme_thermostat/manifest.json index 5c9ecb6..895846c 100755 --- a/custom_components/sleepme_thermostat/manifest.json +++ b/custom_components/sleepme_thermostat/manifest.json @@ -1,7 +1,7 @@ { "domain": "sleepme_thermostat", "name": "SleepMe Thermostat", - "version": "2.0.1", + "version": "2.1.1", "documentation": "https://github.com/rsampayo/sleepme_thermostat", "dependencies": [], "codeowners": ["@rsampayo"], diff --git a/custom_components/sleepme_thermostat/sensor.py b/custom_components/sleepme_thermostat/sensor.py new file mode 100644 index 0000000..7083ffc --- /dev/null +++ b/custom_components/sleepme_thermostat/sensor.py @@ -0,0 +1,142 @@ +import logging +from homeassistant.components.sensor import SensorEntity +from homeassistant.helpers.entity import EntityCategory +from homeassistant.helpers.update_coordinator import CoordinatorEntity +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +async def async_setup_entry(hass, entry, async_add_entities): + """Set up SleepMe Thermostat sensors from a config entry.""" + device_id = entry.data.get("device_id") + name = entry.data.get("name") + coordinator = hass.data[DOMAIN][f"{device_id}_update_manager"] + + _LOGGER.debug(f"[Device {device_id}] Setting up sensor platform from config entry.") + + thermostat = hass.data[DOMAIN].get(device_id) + + if thermostat is None: + _LOGGER.error(f"[Device {device_id}] Thermostat entity not found!") + hass.components.persistent_notification.create( + f"The SleepMe Thermostat entity for device {device_id} was not found. Please check the configuration.", + title="SleepMe Thermostat Error" + ) + return + + # Create the sensors and add them + ip_address_sensor = IPAddressSensor(coordinator, thermostat, device_id, name) + lan_address_sensor = LANAddressSensor(coordinator, thermostat, device_id, name) + brightness_level_sensor = BrightnessLevelSensor(coordinator, thermostat, device_id, name) + display_temp_unit_sensor = DisplayTemperatureUnitSensor(coordinator, thermostat, device_id, name) + time_zone_sensor = TimeZoneSensor(coordinator, thermostat, device_id, name) + + async_add_entities([ + ip_address_sensor, + lan_address_sensor, + brightness_level_sensor, + display_temp_unit_sensor, + time_zone_sensor + ]) + +class IPAddressSensor(CoordinatorEntity, SensorEntity): + """Representation of a sensor that indicates the IP address.""" + + def __init__(self, coordinator, thermostat, device_id, name): + super().__init__(coordinator) + self._thermostat = thermostat + self._device_id = device_id + self._attr_name = f"Dock Pro {name} IP Address" + self._attr_unique_id = f"{DOMAIN}_{device_id}_ip_address" + self._attr_icon = "mdi:ip" + self._attr_entity_category = EntityCategory.DIAGNOSTIC + + # Reuse the device info from the thermostat entity + self._attr_device_info = thermostat.device_info + + @property + def state(self): + """Return the IP address of the device.""" + return self.coordinator.data["about"].get("ip_address") + +class LANAddressSensor(CoordinatorEntity, SensorEntity): + """Representation of a sensor that indicates the LAN address.""" + + def __init__(self, coordinator, thermostat, device_id, name): + super().__init__(coordinator) + self._thermostat = thermostat + self._device_id = device_id + self._attr_name = f"Dock Pro {name} LAN Address" + self._attr_unique_id = f"{DOMAIN}_{device_id}_lan_address" + self._attr_icon = "mdi:lan" + self._attr_entity_category = EntityCategory.DIAGNOSTIC + + # Reuse the device info from the thermostat entity + self._attr_device_info = thermostat.device_info + + @property + def state(self): + """Return the LAN address of the device.""" + return self.coordinator.data["about"].get("lan_address") + +class BrightnessLevelSensor(CoordinatorEntity, SensorEntity): + """Representation of a sensor that indicates the brightness level.""" + + def __init__(self, coordinator, thermostat, device_id, name): + super().__init__(coordinator) + self._thermostat = thermostat + self._device_id = device_id + self._attr_name = f"Dock Pro {name} Brightness Level" + self._attr_unique_id = f"{DOMAIN}_{device_id}_brightness_level" + self._attr_icon = "mdi:brightness-6" + self._attr_entity_category = EntityCategory.DIAGNOSTIC + self._attr_native_unit_of_measurement = "%" # Set unit to percentage + + # Reuse the device info from the thermostat entity + self._attr_device_info = thermostat.device_info + + @property + def state(self): + """Return the brightness level of the device.""" + return self.coordinator.data["control"].get("brightness_level") + +class DisplayTemperatureUnitSensor(CoordinatorEntity, SensorEntity): + """Representation of a sensor that indicates the display temperature unit.""" + + def __init__(self, coordinator, thermostat, device_id, name): + super().__init__(coordinator) + self._thermostat = thermostat + self._device_id = device_id + self._attr_name = f"Dock Pro {name} Display Temperature Unit" + self._attr_unique_id = f"{DOMAIN}_{device_id}_display_temperature_unit" + self._attr_icon = "mdi:thermometer" + self._attr_entity_category = EntityCategory.DIAGNOSTIC + + # Reuse the device info from the thermostat entity + self._attr_device_info = thermostat.device_info + + @property + def state(self): + """Return the display temperature unit of the device in uppercase.""" + temp_unit = self.coordinator.data["control"].get("display_temperature_unit") + return temp_unit.upper() if temp_unit else None + +class TimeZoneSensor(CoordinatorEntity, SensorEntity): + """Representation of a sensor that indicates the time zone.""" + + def __init__(self, coordinator, thermostat, device_id, name): + super().__init__(coordinator) + self._thermostat = thermostat + self._device_id = device_id + self._attr_name = f"Dock Pro {name} Time Zone" + self._attr_unique_id = f"{DOMAIN}_{device_id}_time_zone" + self._attr_icon = "mdi:earth" + self._attr_entity_category = EntityCategory.DIAGNOSTIC + + # Reuse the device info from the thermostat entity + self._attr_device_info = thermostat.device_info + + @property + def state(self): + """Return the time zone of the device.""" + return self.coordinator.data["control"].get("time_zone") \ No newline at end of file