The purpose of this topic is to present one possible use-case for executing a shell script inside a docker container.
The homekit_controller
integration reports a sensor’s light level as a float number.
For example: 27.6
lux
As a result, it can lead to reporting numerous state-changes when the light level varies ever so slightly (27.6, 27.9, 27.1, etc). For most practical purposes, there’s no need for fractional lux values. I would prefer if it reports the value as an integer.
Here’s the relevant line in the source code that reports light level:
return self.service.value(CharacteristicsTypes.LIGHT_LEVEL_CURRENT)
To make it return an integer all I need to do is add int()
like this:
return int(self.service.value(CharacteristicsTypes.LIGHT_LEVEL_CURRENT))
Of course, every time I upgrade Home Assistant I will need to ‘patch’ the source code again. To simplify this step, I can use a shell script. However, I am using Home Assistant Supervised so Home Assistant runs inside a docker container. The challenge becomes to call a shell script and have it run inside the homeassistant
docker container.
I’m using two shell scripts, one that performs the actual source-code editing (using sed
) and the other calls the first script and executes it inside the docker container.
Here’s the shell script that modifies the sensor.py
file. This shell script is located in the /usr/share/hassio
directory (if you use Home Assistant OS then the directory would be /config
).
homekit_light_level.sh
sed -i 's/return self.service.value(CharacteristicsTypes.LIGHT_LEVEL_CURRENT)/return int(self.service.value(CharacteristicsTypes.LIGHT_LEVEL_CURRENT))/' /usr/src/homeassistant/homeassistant/components/homekit_controller/sensor.py
Here’s the shell script that executes the previous one. It is also located in /usr/share/hassio
.
fix_homekit.sh
docker exec -i homeassistant bash < /usr/share/hassio/homeassistant/homekit_light_level.sh
Both shell scripts have been assigned executable permissions:
chmod u+x ./fix_homekit.sh
chmod u+x ./homekit_light_level.sh
To use it, I simply call ./fix_homekit.sh
from the command line in the usr/share/hassio
directory then restart Home Assistant.
The result is that all light levels reported by the homekit_controller
integration are integer values.