Identifying switches and sensors (for home automation config)

I have an idea for your "end of light switches" device. The device could be a simple resistor, say 220K, connected between the pin and ground. This is a higher value and therefore a weaker pull-down than the Arduino's internal pull-up resistor, which is around 20~50K. With this connected, if you read the pin having set pinMode(pin, INPUT) it would read LOW because of the 220K. If you then read it again having set pinMode(pin, INPUT_PULLUP) it will read HIGH because the internal pull-up resistor would overcome the external pull-down resistor.

Where real switches are connected, they would have to be wired to connect to either ground or 5V at all times. Reading that signal, using either INPUT or INPUT_PULLUP would make no difference, the same value would be read, and the Mega would know a real switch was connected to the pin.

To configure itself, the Arduino would need to scan the switches starting with the real switches, until it hit the "end of light switches" device and then stop. If it read an unconnected pin, that input would be floating, and reading it would not reliably indicate if it was a real switch or the end device.

As for i2c chips to read inputs or control relay modules, the cheapest around is pcf8574. It has 8 input/output pins. However, its internal pull-ups cannot be controlled in the same way as an Arduino pin, so it would not be able to detect the "end of light switches" device.

The two other common types are mcp23008 (8 input/outputs) and mcp23017 (16 input/outputs). Their internal pull-ups can be controlled like that of an Arduino pin, so the "end of light switches" device idea might work with them.

As a bonus, all i2c devices share the same 2 Arduino pins. So you might not need a Mega in some circumstances, an Uno or Nano would be ok.